Inter-process communication function of the operating system

A room, the queue process messaging

“消息队列”是在消息的传输过程中保存消息的容器。

对消息队列有写权限的进程可以向消息队列中按照一定的规则添加新消息;对消息队列有读权限的进程则可以从消息队列中读走消息。

消息队列与管道不同的是,消息队列是基于消息的,而管道是基于字节流的,且消息队列的读取不一定是先入先出。

消息队列也有类似管道一样的不足,就是每个消息的最大长度是有上限的(MSGMAX),每个消息队列的总的字节数是有上限的(MSGMNB),系统上消息队列的总数也有一个上限(MSGMNI)。

1.msgget function
Function: Create a new or open an existing message queue

原型:int msgget(key_t key, int msgflag);

2.msgctl function
: The system message queue call operation performed cmd msqid columns identified, i.e., a function of the message control function msgctl queue

原型:int msgctl(int msqid,int cmd,struct msqid_ds *buf);

3.msgsnd Function
Function: a new message is written to queue

原型:int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

4.msgrcv Function
Function: read messages from the message queue

原型:ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
某些参数:
	msqid:消息队列的识别码
	
	msgp:指向消息缓冲区的指针,此位置用来暂时存储发送和接收的消息,是一个用户可定义的通用结构,形态如下:
struct msgbuf {
	long mtype; /* 消息类型,必须 > 0 */
	char mtext[1]; /* 消息文本 */
};

	msgsz:消息的大小
	
	msgtyp:消息类型
		msgtyp等于0 则返回队列的最早的一个消息。
		msgtyp大于0,则返回其类型为msgtyp的第一个消息。
		msgtyp小于0,则返回其类型小于或等于mtype参数的绝对值的最小的一个消息。

Links:
inter-process (1) [] the Linux operating system to achieve communication - Message Queue
https://blog.csdn.net/sofia_m/article/details/79930625
(2) Message Queue function (msgget, msgctl, msgsnd, msgrcv ) and its paradigm
https://blog.csdn.net/guoping16/article/details/6584024
(3) msgctl Detailed
http://support.sas.com/documentation/onlinedoc/sasc/doc700/html/lr2/z2101555. HTM
(4) msgget Detailed
http://support.sas.com/documentation/onlinedoc/sasc/doc700/html/lr2/z2101550.htm
(5) Linux inter-process communication (VII): message Queuing msgget (), msgsend ( ), msgrcv (), the msgctl ()
https://www.cnblogs.com/52php/p/5862114.html
(. 6) Linux study notes ---- C message queue (ftok, msgget, msgsnd, msgrcv , msgctl)
https://lobert.iteye.com/blog/1743256

Inter Second, the process of communication shared memory

共享内存是 Unix下的多进程之间的通信方法 ,这种方法通常用于一个程序的多进程间通信,实际上多个程序间也可以通过共享内存来传递信息。

1.shmget function
function: get a shared memory identifier or create a shared memory object

原型:int shmget(key_t key, size_t size, int shmflg);

2.shmat function
features: the shared memory object is mapped into the address space of the calling process

原型:void *shmat(int shmid, const void *shmaddr, int shmflg);

3.shmctl function
features: Shared Memory Management

原型:int shmctl(int shmid, int cmd, struct shmid_ds *buf);

4.shmdt Function
Function: Disconnect shared memory connection

原型:int shmdt(const void *shmaddr);

Links:
(1) under Linux use semaphores and shared memory function and the C language functions producers and consumers
https://blog.csdn.net/qq_31490151/article/details/78984593
(2) Linux inter-process communication ( f): shared memory shmget (), the shmat (), shmdt (), shmctl ()
https://www.cnblogs.com/52php/p/5861372.html

Among the three, process semaphores

头文件 #include<semaphore.h>

1.sem_init function
anonymous create semaphore semaphores, or initialize a positioning in the sem: Function

原型:int sem_init(sem_t *sem, int pshared, unsigned int value);

2.sem_wait function
features: sem_wait is a function that is an atomic operation, its role is to subtract a "1" from the value of the semaphore, but it will always wait for the semaphore is a non-zero value began to do subtraction

原型:int sem_wait(sem_t * sem);

sem_trywait (sem_t * sem) is a non-blocking version sem_wait function, directly sem semaphore is decremented by 1 and returns an error code.

3.sem_post Function
Function: sem_post semaphore value is to add a "1", it is a "atomic operation", i.e., while doing the same for a semaphore "1" will not operate two threads conflicts ; while the two programs simultaneously on the same file read and write operations is likely to cause conflicts

原型:int sem_post(sem_t *sem);

4.semget function
features: create a new semaphore or acquire an existing semaphore, namely acquiring the semaphore set identifier associated with a key

原型:int semget(key_t key,int nsems,int semflg);

Where semaphore set is established in two ways:
1. If the value of the key is IPC_PRIVATE.
2. IPC_PRIVATE or not the key value, and the key corresponding to the semaphore set does not exist, while the flag specified IPC_CREAT.

5.semop Function
Function: changing the value of the semaphore. When its value is greater than 0, it represents the number of the number of resources currently available; when its value is less than 0, the absolute value of which represents the number of processes waiting to use the resource. Semaphore value can only be changed by operation of PV

原型:int semop(int semid,struct sembuf *sops,size_t nsops);

6.semctl Function
Function: direct control of the amount of information signal, i.e., system calls semctl to perform a control operation on the semaphore set

原型:int semctl(int sem_id, int sem_num, int command, ...);
如果有第四个参数,它通常是一个union semum结构,定义如下:
union semun {
    int val;
    struct semid_ds *buf;
    unsigned short *arry;
};

Links:
(1) linux multithreading semaphore of sem_init
https://blog.csdn.net/qq_19923217/article/details/82902442
(2) Linux inter-process communication (V): semaphore semget (), semop () , semctl ()
https://www.cnblogs.com/52php/p/5851570.html

Guess you like

Origin blog.csdn.net/JxufeCarol/article/details/90298318