Message Queuing for inter-process communication between Linux

Source: Internet
Author: User
Tags message queue sendmsg

The new Year, the heart of all tastes ...

  

One, Message Queuing

1, Concept: "Message Queuing" is a container for saving messages during the transmission of a message

2, Message Queuing is a linked list of messages. You can think of a message as a record, with a specific format and a specific priority.

a process that has write permission to a message queue can add new messages to a message queue in accordance with certain rules;

a process that has read access to a message queue can read messages from the message queue.

Message Queuing is persistent with the kernel.

3, Programming Considerations: When using the data encapsulated into a message, the message into the queue

Programming steps: The use of specific functions can be viewed with the man manual (strongly recommended)

(1) Ftok () Production key

(2) Create/Get Message queue using Msgget (), return value is queue identifier

(3) Send a message using MSGSND ()

Using MSGRCV () to receive messages

(4) Delete Message Queuing using Msgctl ()

4, Example:

SENDMSG.C used to send messages.

//sendmsg.c#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>#include<string.h>structmy_msg{intMtype;//Message Type    Charbuf[ the];} MSG1, MSG2;intMain () {key_t key= Ftok ("./", the); intMsgId = Msgget (Key,0666|ipc_creat); if(-1==MsgId) {Perror ("Msgget failed!!!"); Exit (1); } Msg1.mtype=2; strcpy (Msg1.buf,"Hello, msg2."); Msgsnd (MsgId,&AMP;MSG1,sizeof(MSG1),0);//Blocking//msgsnd (MsgId, &MSG1, sizeof (MSG1), ipc_nowait);//non-blockingMsg2.mtype=1; strcpy (Msg2.buf,"Hello, msg1."); Msgsnd (MsgId,&AMP;MSG2,sizeof(MSG2),0);//Blockingprintf ("message Send complete, press ENTER to destroy message queue \ n");    GetChar (); if(-1==Shmctl (MsgId, Ipc_rmid, NULL)) {Perror ("Shmctl failed"); Exit (2); }    return 0;}

RECVMSG.C used to receive messages.

//recvmsg.c#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>#include<string.h>structmy_msg{intMtype;//Message Type    Charbuf[ the];} msg;intMain () {key_t key= Ftok ("./", the); //Get Message Queuing    intMsgId = Msgget (Key,0); if(-1==MsgId) {Perror ("Msgget failed!!!"); Exit (1); }    intres = MSGRCV (MsgId, &msg,sizeof(msg),2,//a message with a message type of 2 is taken            0); printf ("type:%d, content:%s\n", Msg.mtype, msg.buf); printf ("Message Reception complete, press ENTER to destroy Message Queuing \ n");    GetChar (); if(-1==Shmctl (MsgId, Ipc_rmid, NULL)) {Perror ("Shmctl failed"); Exit (2); }    return 0;}

5, running results

6, get the code

git clone https://github.com/xcywt/xcyipc.git

Some people see this trouble again in the picture of the Bantu try this library is not built. The main lesson is to learn some of the uses of GitHub.

Message Queuing for inter-process communication between Linux

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.