Message Queuing IPC Principles
Message Queuing is a chained queue of messages, such as a model for Message Queuing. There are two types of data structures for the entire message queue.
1.msqid_ds 消息队列数据结构:描述整个消息队列的属性,主要包括整个消息队列的权限、拥有者、两个重要的指针(分别指向消息队列的第一个消息和最后一个消息)。2.msg 消息队列数据结构:整个消息队列的主体,一个消息队列有若干个消息,每个消息数据结构的基本成员包括消息类型、消息大小、消息内容指针和下一个消息数据结构的位置。
Message Queuing can also be based on type processing, but the FIFO principle of Message Queuing only applies to messages of the same type. In Linux, Message Queuing is specified (the values may differ for different Linux versions):
1.默认情况下,整个系统最多允许有16个消息队列。2.每个消息队列最大为16384字节。3.消息队列中的每个消息最大为8192字节。
The content is defined in/usr/include/linux/msg.h:
#define MSGMNI 16 /* <= IPCMNI */ /* max # of msg queue identifiers */#define MSGMAX 8192 /* <= INT_MAX */ /* max size of message (bytes) */#define MSGMNB 16384 /* <= INT_MAX */ /* default max size of a message queue */
Basic properties of Message Queuing
For/usr/include/linux/msg.h files
Message Queuing Management
1. Create a message queue:
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>/* 第一个参数为key值,一般由ftok()函数获得;第二个参数为访问权限 */int msgget(key_t key, int msgflg);
2. Message Queuing Properties control
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>/* * 第一个参数为消息队列ID,由msgget获得 * 第二个参数为控制指令 * 第三个参数为数据传递的载体 */int msgctl(int msqid, int cmd, struct msqid_ds *buf);
The control directives are as follows:
3. Sending a message queue
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>/* * 第一个参数为消息队列ID * 第二个参数 msgp 指向定义的缓冲区 * 第三个参数为发送消息的大小 * 第四个参数一般高为0,阻塞调用进程 */int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
4. Receiving messages from Message Queuing
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>/* * 第一个参数为消息队列ID * 第二个参数为临时消息数据结构,用于储存消息 * 第三个参数为接收消息的大小 * 第四个参数用于指定请求的消息类型 * 第五个参数一般设置为0,阻塞调用进程 */ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
Where the message data structure should be defined as follows:
/* From /usr/include/linux/msg.h */struct msgbuf { long mtype; /* 消息类型 */ char mtext[1]; /* 存储消息位置,需要重新定义 */};
Message Queuing Application Instance
Create two processes, use Message Queuing for communication, one process to send messages, and another process to receive messages.
Send side:
#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <string.h>struct msgbuf { long mtype; char mtext[50]; };int main(){ int key, msg_id; static struct msgbuf buf; key = ftok(".", 1); msg_id = msgget(key, IPC_CREAT | 0600); /* 设置消息的类型 */ buf.mtype = 1; while(1){ fgets(buf.mtext, 50, stdin); /* 输入quit退出进程,并删除队列 */ if(!strncmp(buf.mtext, "quit", 4)){ msgctl(msg_id, IPC_RMID, 0); exit(0); } /* 将消息发送到队列 */ msgsnd(msg_id, (void *)&buf, sizeof(buf), 0); } return 0;}
Receiving end:
#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>struct msgbuf { long mtype; char mtext[50]; };int main(){ int key, msg_id; static struct msgbuf buf; key = ftok(".", 1); msg_id = msgget(key, IPC_CREAT | 0600); /* 设置消息类型 */ buf.mtype = 1; while(1){ /* 从消息队列接收消息 */ msgrcv(msg_id, (void*)&buf, sizeof(buf), buf.mtype, 0); printf("Recv msg : %s \n", buf.mtext); } return 0;}
Operation Result:
Linux interprocess communication---Message Queuing