Linux process between the IPC (semaphores, message queues, shared memory) of the communication

Copyright: free to reprint, indicate the link address to https://blog.csdn.net/weixin_38812277/article/details/90791185

1, we must first pay attention to a concept: IPC structures are the core of the structure. IPC structure that is maintained by the kernel for each process are public, not part of a specific process. Only in this way, IPC structure to support them. "Interprocess communication" function.

2, the identifier (ID) and key (key)

ID is the name of the internal structure of the IPC. That internal use in internal processes, such identification methods can not support inter-process communication.

key is the name of the external structure of the IPC. When multiple processes, for the same call to get a key function (msgget, etc.), these processes actually get ID identifies the same IPC structure. IPC can communicate through this structure among multiple processes.

3, the advantages and disadvantages

System-wide functions, no access count. Each IPC resources are persistent, release unless the process is displayed, otherwise you will never resident in memory. In contrast, when the last visit of the termination process pipeline, the pipeline will be deleted. For FIFO (named pipe), although the name of any course remain in the system, but which data has been deleted.

IPC structure with no name in the file system , you can not use read / write access. To support that they had to increase the dozens of new system calls (msgget, semop, shmat, etc.). We can not see with the ls command IPC objects, they can not be deleted with the rm command, nor can the chmod command to change their access rights. So, we have to add a new command ipcs (1) and ipcrm (1).

 

4, the semaphore

Compared to the amount of the core signal: (1) each is an IPC semaphores or more signal magnitude set . This means that a resource IPC can protect multiple independent shared data structures. (2) IPC semaphores provide a mechanism for the security failure, when the process quits unexpectedly, the process can not cancel the operation of the signal before execution of the death, IPC semaphores can be restored to the original value.

 

Use semaphores

#include <sys/sem.h>
int semget(key_t key, int nsems, int flag);

Function Description: nsems is the number of signals in the set. If you are creating a new collection (usually in the server process), you must specify nsems. If you reference an existing collection (a client process), then nsems designated as 0.

int semctl(int semid, int semnum, int cmd, ... /* union semun arg */);

Function Description: designation operation which a semaphore in the semaphore set semnum.

cmd: A total of nine kinds of []

GETVAL return semval value of semnum members.

SETVAL set semval value of semnum members. This value is specified by arg.val.

Remove IPC_RMID set the semaphore from the system. This deletion is immediate. When other processes are still using this set of semaphores in this semaphore their next attempt to operate the set, returns an error EIDRM. This command can be executed by the following two processes: one is its effective user ID equal sem_perm.cuid or process sem_perm.uid; the other is a process with superuser privileges.

Reference: https://www.cnblogs.com/nufangrensheng/p/3562046.html

Explanatory Note to create and assign initial values ​​separately.

int semop(int semid, struct sembuf semoparray[], size_t nops);

Function Description: Parameter nops predetermined number (number of elements) of the array (semoparray) in operation. [] The function has the atomic

 

5, the message queue: [new application should not be reused]

msgget used to create a new queue, or open an existing queue . msgsnd new messages added to the queue to the tail end . Each message contains a positive long integer type field, a length and a non-negative real data bytes (corresponding to the length), all of which when added to the queue message transmitted to msgsnd. msgrcv for taking the message from the queue . We do not have to take the message in a first-order, you can also take the message field of the message by type.

Message: Each message consists of three parts, which are: positive long integer type field, a length of non-negative (nbytes) and the actual data byte (corresponding to the length) . The messages are always on the tail end of the queue.

Message queue using:

Create a message queue:

#include <sys/msg.h>
int msgget(key_t key, int flag);
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
int msgsnd(int msqid, const void *ptr, size_t nbytes, int flag);
ssize_t msgrcv(int msqid, void *ptr, size_t nbytes, long type, int flag);

 

Shared Memory: [] the most useful IPC mechanism is different from mmap memory mapping generated (also can be used as inter-process communication)

Shared memory require the user to synchronize.

#include <sys/shm.h>
int shmget(key_t key, size_t size, int flag);
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
void *shmat(int shmid, const void *addr, int flag);
int shmdt(void *addr);

[Supplement] parent-child communication process can use the anonymous memory mapping, inherited his father's son into the process's address space.

 

 

Reference: https://www.cnblogs.com/nufangrensheng/p/3562046.html

Guess you like

Origin blog.csdn.net/weixin_38812277/article/details/90791185