Transferred from: http://blog.csdn.net/lifan5/article/details/7588529
Http://www.cnblogs.com/kunhu/p/3608589.html
Objective:
Message Queuing is an internal linked list in the kernel address space through which content is passed between processes through the Linux kernel, messages are sent sequentially to the message queue, and in several different ways
Obtained from the queue, each message queue can be uniquely identified with an IPC identifier, the message queue in the kernel is distinguished by the IPC identifier, and the different message queues are
Independently of each other, the messages in each message queue form a separate list.
Function:1. Message buffer Structurewhen you send a message to a message queue, you must make a reasonable data structure. The Linux system defines a template data structure MSGBUF:#include <linux/msg.h>struct msgbuf{long type;Char mtext[1];}where type represents the type of message, expressed as a positive number. Mtext is the data for this message and is not necessarily a char type and can be any type. The above is the standard, in fact the type is not necessary, if you have only one type in the queue, then there can be no message type.
2. Create a new message queue or get an existing message queue
Prototype: int msgget (key_t key, int msgflg);
Parameters:
Key: Can be thought of as a port number, or it can be generated by the function Ftok.
The Msgflg:ipc_creat value, which, if there is no queue, creates one and returns a new identifier, or returns the original identifier if it already exists.
A IPC_EXCL value that returns 1 if there is no queue, or 0 if it already exists.
3. Read/write messages to the queue
Prototype:
MSGRCV messages from the queue: ssize_t msgrcv (int msqid, void *msgp, size_t msgsz, long Msgtyp, int msgflg);
MSGSND put data into Message Queuing: int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg);
Parameters:
MSQID: Message Queue identification code
MSGP: A pointer to the message buffer, which is used to temporarily store messages sent and received, is a generic, user-definable structure, in the following form:
structmsgstru{ long mtype; //大于0 charmtext[512];}; |
MSGSZ: The size of the message, (the standard refers to the length of the message in the structure, but the length of the entire structure can also be run, of course, other values as long as the length of the transmission and acceptance of the same can be, the proposed structure to lift the length of simple)
Msgtyp: The type of message read from within the message queue. A value of zero indicates that the first in the message queue is read, and the other type represents the specified type in the Read message queue.
MSGFLG: Used to indicate the action that the core program should take if the queue has no data. If the MSGFLG and constant ipc_nowait are pooled, then if the message queue is full at msgsnd () execution, then MSGSND () will not block, and will immediately return-1, if the MSGRCV () is performed, then when the message queue is empty, do not wait for the immediate return-1, and set the error code to enomsg. When MSGFLG is 0 o'clock, msgsnd () and MSGRCV () take a blocking wait processing mode when the queue is full or empty.
4. Set Message Queuing properties
Prototype: int msgctl (int msgqid, int cmd, struct msqid_ds *buf);
Parameters: The MSGCTL system invokes a CMD operation on the message queue identified by the msgqid, and the system defines 3 cmd operations: Ipc_stat, Ipc_set, Ipc_rmid
Ipc_stat: This command is used to obtain the MSQID_DS data structure for the message queue and save it to the BUF specified address space.
Ipc_set: The command is used to set the properties of the message queue, and the properties to be set are stored in BUF.
Ipc_rmid: Removes the MSQID identity message queue from the kernel.
Instance:
Message Send side: SEND.C
1/*Send.c*/2 #include <stdio.h>3 #include <sys/types.h>4 #include <sys/ipc.h>5 #include <sys/msg.h>6 #include <errno.h>78#define MSGKEY 1024910structMsgstru11{12LongMsgtype;13Char msgtext[2048];14};1516Main ()17{18structMsgstru msgs;19IntMsg_type;20Char str[256];21stIntRet_value;22IntMsqid;23Msqid=msgget (MSGKEY,IPC_EXCL);/*Check if Message Queuing exists*/25if (Msqid <0){Msqid = Msgget (msgkey,ipc_creat|0666);/*Creating message Queues*/27if (Msqid <0){printf ("Failed to create MSQ | errno=%d [%s]\n", Errno,strerror (errno));Exit (-1);30}31}3233while (1){printf ("Input message type (end:0):");scanf ("%d",&Msg_type);36if (Msg_type = =0)37Break;* printf ("Input message to be sent:");scanf ("%s", str);Msgs.msgtype =Msg_type;41strcpy (Msgs.msgtext, str);42/*Send Message Queue*/Ret_value = msgsnd (Msqid,&msgs,sizeofstructMsgstru), ipc_nowait);44 0< Span style= "color: #000000;" ) {45 printf (msgsnd ( ) Write msg failed,errno=%d[%s]\n ",errno,strerror ( errno)); 46 exit (-147 48 49 Msgctl (Msqid,ipc_rmid,0); // delete message queue 50}
Message Receiving end RECEIVE.C
1/*Receive.c*/2 #include <stdio.h>3 #include <sys/types.h>4 #include <sys/ipc.h>5 #include <sys/msg.h>6 #include <errno.h>78#define MSGKEY 1024910structMsgstru11{12LongMsgtype;13Char msgtext[2048];14};1516/*Child processes, listening for Message Queuing*/17voidChildproc () {18structMsgstru msgs;19IntMsgid,ret_value;20Char str[512];21st22While1){MsgId = Msgget (MSGKEY,IPC_EXCL);/*Check if Message Queuing exists*/24if (MsgId <0){printf ("MSQ not existed! errno=%d [%s]\n", Errno,strerror (errno));Sleep (2);27Continue;28}29/*Receiving Message Queuing*/Ret_value = MSGRCV (Msgid,&msgs,sizeofstruct Msgstru),0,0);printf ("text=[%s] Pid=[%d]\n", Msgs.msgtext,getpid ());32}33Return;34}3536voidMain ()37{38IntI,cpid;3940/*Create 5 child process*/41for (i=0;i<5;i++){Cpid =Fork ();43 0) 44 printf (fork Failed\n "); 45 else if (Cpid ==0) /*child process*/ Span style= "color: #008080;" >46 Childproc (); 47 48 } 49
Linux inter-process Message Queuing communication