Linux interprocess communication (POSIX Message Queuing simple) instance details see: http://www.linuxidc.com/Linux/2011-10/44828.htmCompiling: gcc-O Consumer Consumer.c-LRTGCC-O producer Producer.c-LRT/** * filename:producer.c * * Description: Producer Process * * version:1.0 * created:09/30/2011 04: 52:23 PM * Revision:none * COMPILER:GCC (g++) * Author: | Zhenghe Zhang|, | [Email protected]| * Company: | Shenzhen XXX technology Co., ltd.| * */#include<stdio.h>#include<mqueue.h>#include<sys/stat.h>#include<stdlib.h>#include<unistd.h>#include<time.h>#include<string.h>#defineMAXSIZE 10//define BUF size#defineBUFFER 8192//define the MSG sizestruct msgtype{int len; Char Buf[maxsize]; char x; Short y;}; You can pass parameters in this formintMain () {/*Message Queuing*/mqd_t msgq_id; structMsgtype msg; unsigned int prio = 1; unsignedintSend_size =BUFFER; structmq_attr msgq_attr; Const Char*file ="/posix"; /*Mq_open () for creating a new queue (using default attributes)*/ /*Mq_open () Create a new POSIX message queue or open an existing queue*/msgq_id= Mq_open (file, O_RDWR | O_creat, S_irwxu |S_irwxg, NULL); if(msgq_id = = (mqd_t)-1) {perror ("Mq_open"); Exit (1); } /*getting the attributes from the queue--mq_getattr ()*/ if(Mq_getattr (msgq_id, &msgq_attr) = =-1) {perror ("mq_getattr"); Exit (1); } printf ("Queue \ "%s\": \n\t-stores at the most%ld messages\n\t-\Large at the most%ld bytes each\n\t-currently holds%ld messages\n", file, msgq_attr.mq_maxmsg, Msgq_attr.mq_msgsize, MSGQ_ATTR.MQ_CURMSGS); /*setting the attributes of the queue--mq_setattr ()*/ /*mq_setattr () sets the properties of the message queue and sets the information using the MQ_ATTR structure pointed to by the newattr pointer. */ /*only the O_nonblock flag in the Mq_flasgs property can be changed, and the other fields within the newattr are ignored .*/ if(Mq_setattr (msgq_id, &msgq_attr, NULL) = =-1) {perror ("mq_setattr"); Exit (1); } inti =0; while(I <Ten) {Msg.len=i; memset (Msg.buf,0, MAXSIZE); sprintf (Msg.buf,"0x%x", i); msg.x= (Char) (i +'a'); Msg.y= ( Short) (i + -); printf ("Msg.len =%d, Msg.buf =%s, msg.x =%c, msg.y =%d\n", Msg.len, Msg.buf, msg.x, MSG.Y); /*sending the message--mq_send ()*/ /*Mq_send () joins the message that the msg_ptr points to in the message queue referenced by Mqdes. */ /*parameter Msg_len Specifies the length of the message msg_ptr: This length must be less than or equal to the value of the Queue Mq_msgsize property. 0 The length of the message is allowed. */ if(Mq_send (msgq_id,(char*) &msg, sizeof (struct msgtype), prio) = =-1) {perror ("Mq_send"); Exit (1); } I++; Sleep (1); } Sleep ( 30); Waiting for the consumer process to exit this means that the producer executes first, but the consumer must execute within 30 seconds, or it will go wrong. /*closing the queue--mq_close ()*/ /*Mq_close () closes the message queue descriptor mqdes. If the calling process binds the notification request in Message Queuing mqdes,*/ /*the request is then deleted, and other processes can then bind the notification request to this message queue. */ if(Mq_close (msgq_id) = =-1) {perror ("Mq_close"); Exit (1); } /*Mq_unlink () deletes a message queue named name. The message queue name is deleted directly. */ /*The message queue itself is destroyed when all descriptors referencing this queue are closed. */ if(Mq_unlink (file) = =-1) {perror ("Mq_unlink"); Exit (1); } return 0; } /** * filename:consumer.c * * Description: Consumer Process * * version:1.0 * created:09/30/2011 04: 52:23 PM * Revision:none * COMPILER:GCC (g++) * Author: | Zhenghe Zhang|, | [Email protected]| * Company: | Shenzhen XXX technology Co., ltd.| * */#include<stdio.h>#include<mqueue.h>#include<sys/stat.h>#include<stdlib.h>#include<unistd.h>#include<time.h>#include<string.h>#defineMAXSIZE 10//define BUF size#defineBUFFER 8192//define the MSG sizestructmsgtype{intLen; CharBuf[maxsize]; Charx; Shorty;};intMain () {/*Message Queuing*/mqd_t msgq_id; structMsgtype msg; unsignedintSender; structmq_attr msgq_attr; unsignedintRecv_size =BUFFER; Const Char*file ="/posix"; /*Mq_open () for opening an existing queue*/msgq_id=mq_open (file, O_RDWR); if(msgq_id = = (mqd_t)-1) {perror ("Mq_open"); Exit (1); } /*getting the attributes from the queue--mq_getattr ()*/ if(Mq_getattr (msgq_id, &msgq_attr) = =-1) {perror ("mq_getattr"); Exit (1); } printf ("Queue \ "%s\": \n\t-stores at the most%ld messages\n\t-\Large at the most%ld bytes each\n\t-currently holds%ld messages\n", file, msgq_attr.mq_maxmsg, Msgq_attr.mq_msgsize, MSGQ_ATTR.MQ_CURMSGS); if(Recv_size <msgq_attr.mq_msgsize) Recv_size=msgq_attr.mq_msgsize; inti =0; while(I <Ten)//Run a consumenr of 10, running two consumer processes at the same time, for 5{Msg.len= -1; memset (Msg.buf,0, MAXSIZE); msg.x=' '; Msg.y= -1; /*getting a message*/ /*mq_receive () removes the oldest message of the highest priority from the queue referenced by the descriptor Mqdes and places it in the msg_ptr buffer. */ /*The parameter Msg_len specifies the size of the buffer msg_ptr: it must be greater than the Mq_msgsize property of the queue (parameter mq_getattr). */ /*if Prio is not NULL, then it points to the memory used to return the priority associated with the received message. */ if(Mq_receive (msgq_id,(char*) &msg, recv_size, &sender) == -1) {perror ("mq_receive"); Exit (1); } printf ("Msg.len =%d, Msg.buf =%s, msg.x =%c, msg.y =%d\n", Msg.len, Msg.buf, msg.x, MSG.Y); I++; Sleep (2); } if(Mq_close (msgq_id) = =-1) {perror ("Mq_close"); Exit (1); } return 0; } http://www.linuxidc.com/Linux/2011-10/44829.htm
Linux interprocess communication (POSIX Message Queuing simple) instance