Linux Message Queuing

Source: Internet
Author: User

1#include <stdio.h>2#include <sys/types.h>3#include <sys/ipc.h>4#include <sys/msg.h>5#include <errno.h>6#include <string.h>7#include <stdlib.h>8 9 intMsg_key =1234;Ten  One structMsgnode A { -     LongNmsgtype; -     Charsbuf[ the]; the }; -  - intMainintargcChar**argv) - { +     intNmsgid =0; -     if(ARGC = =2) && (strcmp (argv[1],"C") ==0)) +     { ANmsgid =Msgget (MSG_KEY,IPC_EXCL); at         if(Nmsgid <0) -         { -Nmsgid = Msgget (Msg_key,0750|ipc_creat); -             if(Nmsgid <0) -             { -Perror ("Msgget"); inprintf"Msgget failed\n"); -Exit (-1); to             } +         } -  the          for(inti =0; I <Ten; ++i) *         { $ msgnode node;Panax NotoginsengNode.nmsgtype =1; -sprintf (Node.sbuf,"This is the %d msg from client.\n", i); the  +             if(i = =9) A             { thestrcpy (Node.sbuf,"end\n"); +             } -  $printf"%s", node.sbuf); $  -             intret = msgsnd (Nmsgid,&node,sizeof(NODE.SBUF), ipc_nowait); -             if(Ret <0) the             { -Perror ("Msgsend");Wuyiprintf"Msgsend failed"); theExit (-1); -             } Wu         } -     } About     Else $     { -Nmsgid =Msgget (MSG_KEY,IPC_EXCL); -         if(Nmsgid <0) -         { Aprintf"Msgget failed\n"); +Exit (-1); the         } -  $         BOOLFlag =true; the          while(flag) the         { the msgnode node; the             intret = MSGRCV (Nmsgid,&node,sizeof(NODE.SBUF),1, ipc_nowait); -             if(ret = =-1) in             { thePerror ("Msgrecv"); theprintf"msgrecv failed\n"); AboutExit (-1); the             } theprintf"Receive msg:%s", node.sbuf); the             //if (strncmp (Node.sbuf, "End", 3) = = 0) +             //{ -             //flag = false; the             //};Bayi         } the  the         intret = Msgctl (Nmsgid,ipc_rmid,0); -         if(ret = =-1) -         { theprintf"Msgctl failed\n"); theExit (-1); the         } the     } -  the     return 0; the}

It is important to note that

int msgsend (intconstvoidint msgflg);

And

int MSGRCV (intvoidlongintint msgflg);  

Where size_t Msg_st is not the size of the message structure, but the size of the message.
If the structure is defined in the code

1 struct Msgnode 2 {3     Long Nmsgtype; 4     Char sbuf[]; 5 };

Then size_t Msg_st refers to sizeof (MSGNODE.SBUF), which does not include the length of the message type in the structure.
When a message is received/sent if it is not set to ipc_nowait, it is blocked if the message queue is full when it is sent, or if Message Queuing is empty when it is received.

Message Queuing is much the same as named Pipes, where the process of communicating Message Queuing, like a named pipe, can be unrelated processes, and they all pass data in a way that is sent and received. In a named pipe, send data with write, receive data with read, then in message queue, send data with MSGSND, receive data with MSGRCV. And they have a maximum length limit for each data. The advantage of Message Queuing over named Pipes is that 1, Message Queuing can also exist independently of the send and receive processes, eliminating the difficulties that can arise when synchronizing named Pipes for opening and closing. 2. By sending messages, you can also avoid the synchronization and blocking problems of named pipes, and do not need to provide synchronization methods by the process itself. 3. The receiving program can selectively receive data through the message type, instead of being received only by default, as in a named pipe.

Linux Message Queuing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.