Linux message sequence (inter-process communication)

Source: Internet
Author: User
Linux message sequence (inter-process communication) [cpp] # include & lt; stdlib. h & gt; # include & lt; stdio. h & gt; # include & lt; string. h & gt; # include & lt; errno. h & gt; # include & lt; unistd. h & gt; # include & lt; sys/types
Linux message sequence (inter-process communication) [cpp] # include # Include # Include # Include # Include # Include # Include # Include Www.2cto.com // the message queue structure struct msgStuct {long int msgType; char strMsg [1024] ;}; int main () {struct msgStuct msg_data; int msgid; char buffer [1024]; // create a message queue if (msgid = msgget (key_t) 2234,066 6 | IPC_CREAT) =-1) {www.2cto.com perror ("msgget failed with error: % d \ n"); exit (EXIT_FAILURE);} while (1) {printf ("Send message :"); fgets (buffer, 1024, stdin); // initialize the message type msg_data.msgType = 1; strcpy (msg_data.strMsg, buffer); // send the message if (msgsnd (msgid, (void *) & msg_data, 1024, 0) =-1) {fprintf (stderr, "msgsnd failed \ n"); exit (EXIT_FAILURE);} if (strncmp (buffer, "end", 3) = 0) {break;} exit (EXIT_SUCCESS);} www.2cto.com [cpp] # include # Include # Include # Include # Include # Include # Include # Include // The message queue structure struct msgStuct {long int msgType; char strMsg [1024] ;}; int main () {www.2cto.com int msgid; struct msgStuct msg_data; // receive message priority long int msgPriority = 0; // obtain the first message queue from the queue // Create a Message Queue
If (msgid = msgget (key_t) 2234,066 6 | IPC_CREAT) =-1) // create a file like open () and return its file descriptor, here is the message sequence {perror ("msgget failed with error"); exit (EXIT_FAILURE);} while (1) {// receive the message if (msgrcv (msgid, (void *) & msg_data, 1024, msgPriority, 0) =-1) {perror ("msgrcv failed with error"); exit (EXIT_FAILURE);} printf ("Received message: % s ", msg_data.strMsg); if (strncmp (msg_data.strMsg," end ", 3) = 0) {www.2cto.com break ;}// delete the message queue if (msgctl (msgid, IPC_RMID, 0) =-1) {fprintf (stderr, "delete messagequeue error \ n"); exit (EXIT_FAILURE);} exit (EXIT_SUCCESS);} The first one is send. c. The second is recieve. c
Related Article

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.