First, the basic concept
inter-process communication IPC: The process of exchanging data between processes is called interprocess communication
inter-process homosexual approach:
simple inter-process communication:
command line: The parent process creates child processes through the EXEC function to append some data
environment variable table: The parent process creates a child process through the EXEC function by passing an environment variable table
Signals: Parent-child processes can send signals to each other based on process numbers for simple communication
files: One process writes data to a file, another process reads 3 from a file
command line, environment variables can only be passed one way, the signal is too simple, the file can not be real-time.
traditional ways of communicating between processes: pipelines
Second, the pipeline
1. Pipelines are an ancient means of communication (basically no longer used)
2. The early pipeline is a half-duplex, which is now basically full-duplex
3. Well-known pipelines (this is the way the file exists)
To create a pipeline file:
command:mkfifo
function:int mkfifo (const char *pathname, mode_t mode);
Programming mode for pipeline communication:
process a process B
Create Pipeline Mkfifo
Open pipe File opens open pipeline
Write /Read data read/write read/write Data read/write
Close Pipe Close Pipe
4. Nameless Pipe: Created by the kernel, returns only the file descriptor of the pipeline and does not see the pipeline file. However, such a pipeline can only be used between parent-child processes that are created for fork ().
int pipe (int pipefd[2])
PIPEFD[0] used to read data
PIPEFD[1] used to write data
XSI communication mode: X/open computer manufacturer organization.
shared memory, Message Queuing, semaphores
Third, XSI IPC interprocess communication
1.XSI communication is by the IPC object process of the kernel
2. Each IPC object has an IPC identifier (similar to a file descriptor), which is a non-negative integer.
3.IPC objects must be created before they can be acquired, set up, manipulated, deleted.
4. To create an IPC object you must provide a health value (key_t), which is the basis for creating and acquiring an IPC object.
5. Ways to generate health values:
fixed literal character:1980014
Use function Calculation: Health Value = Ftok (project path, Project ID) ftok (const char *pathname,int proj_id);
use macros to randomly assign the operating system: Ipc_privte
must be taken to get to IPC object identifiers are recorded to tell other processes
6.XSI IPC objects that can be created: shared memory, Message Queuing, semaphores
Iv. Shared Memory
1, the kernel maintains a piece of shared memory area, other processes map their own virtual address to this memory, and then can share the memory between multiple processes
2. The benefit of this interprocess communication is that information replication is not required and is the fastest way to communicate between processes.
3, but this communication mode will face the problem of synchronization. Need to cooperate with other communication, the most suitable is the signal.
programming mode for Shared memory:
1. To contract a health value between processes
Process A Process B
Create shared Memory
Load Shared Memory Load Shared Memory
Uninstalling shared Memory Uninstalling shared Memory
Destroying shared memory
int Shmget (key_t key,size_t size,int SHMFLG)
function: Create shared memory
Size: Share size as much as multiples of 4096
Shmflg:ipc_creat| Ipc_excl
return Value: IPC Object identifier
void *shmat (int shmid,const void * Shmaddr,int SHMFLG)
function: Load Shared memory (the virtual address of the process and the shared memory map)
return value of Shmid:shmget
SHMADDR: The virtual address provided by the process, and if NULL, the operating system will automatically select
SHMFLG:
Shm_rdonly: Restrict memory permissions to read-only
Shm_remap: Mapping an existing share
SHM_RND: Automatically assigned when SHMADDR is empty
The value of shmlba:shmaddr cannot be empty
return value: The mapped virtual address
int SHMDT (const void* SHMADDR);
function: Offload shared memory (The virtual address of the process is unmapped from the shared memory)
int shmctl (int shmid,int cmd,struct shmid_ds* buf)
function: Control /Destroy shared memory
Cmd:
Ipc_stat: Gets the properties of the share inside thick
Ipc_set: Setting properties for Shared memory
IPC_RMID: Deleting shared memory
Ipc_info: Get information on contributing memory
shared Memory is one of the fastest processes to communicate, so the data does not have a copy process, but there is no way to know that data is written and read between processes, requiring additional quotas for communication
V. Message Queuing
1. Message Queuing is a data link table that is stored and managed by the system kernel and obtained through the IPC object identifier.
int Msgget (key_t key, int msgflg);
function: Create or get Message Queuing
MSGFLG:
Create: Ipc_creat| ipc_excl|0777
gets:0
return: IPC Object identifier
int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg);
function: Send message to Message queue
return value of Msqid:msgget
MSGP: The first address of the message (message type plus message content)
Msgsz: Length of message content
ssize_t MSGRCV (int msqid, void *msgp, size_t msgsz, long Msgtyp, int msgflg);
Features: Accepting messages from Message Queuing
MSGP: Buffer for storing messages
MSGSZ: Message length to accept
Msgtyp: The type of message that is contained in the first 4 bytes of a message.
MSGFLG:
Msg_noerror: When the actual length of the message is longer than the MSGSZ,
the msgsz length is intercepted and sent. Otherwise, an error is generated.
Msg_nowait: If the message to be received does not exist, return directly
Otherwise, the block waits.
Msg_except: Accept the first one from the message queue
int msgctl (int msqid, int cmd, struct msqid_ds *buf);
function: Control /Destroy message queue
Cmd:
Ipc_stat: Get the properties of a message queue
Ipc_set: Setting properties for Message Queuing
IPC_RMID: Deleting Message Queuing
Six, IPC-Related commands:
IPCS-M Viewing Shared memory
IPCRM-M ID Delete shared memory
IPCS-Q viewing Message Queuing
IPCRM-Q ID Delete Message Queuing
7. Signal Volume
Semaphore (semaphore), which can be used as a global variable shared between processes and processes. Typically used to count shared resources.
How to use the semaphore:
1. Process A, create semaphores, and set initialization (number of resources set)
2. Process B, get the semaphore, query the semaphore (the number of remaining resources is queried). Reduce semaphores (using resources), increase semaphores (resources are used to return)
3. When a process attempts to reduce the semaphore, if it cannot be reduced (the resource is used), the process can enter a wait state.
when the semaphore can be reduced, (other processes return the resource back), the process is awakened.
function
int Semget (key_t key, int nsems, int semflg);
function: Create a semaphore or get a semaphore
Nsems: Amount of semaphore
SEMFLG:
Ipc_creat| ipc_excl|0644
return Value: IPC Object identifier
int semop (int semid, struct sembuf *sops, unsigned nsops);
function: Increase or decrease the amount of signal
struct SEMBUF
{
unsigned short sem_num; Number of the signal
Short Sem_op; The processing of the signal
Short SEM_FLG;
If the semaphore is currently 0, the function returns immediately.
Ipc_nowait: Will block
}
int semctl (int semid,int semnum,int cmd,...);
function: Control or release of signal volume
Semnum: Number of semaphores
Cmd:
Ipc_set setting the properties of semaphores
Ipc_stat gets the properties of the semaphore
Ipc_rmid Delete semaphore
Ipc_info getting the semaphore information
Communication between processes