Linux Message Queuing programming

Source: Internet
Author: User
Tags message queue semaphore

Message Queuing, semaphores, and shared memory are called XSI IPC, and they all come from the IPC capabilities of System V, and therefore have many commonalities.

Keys and identifiers:

  Each IPC structure in the kernel (such as semaphores, message queues, shared memory) is marked with a non-negative integer identifier (such as the shmid of shared memory, the semid of the semaphore, and the msgid of the message queue). Unlike the file descriptor, the IPC identifier is not a small nonnegative integer, it is an int integer, and when an identifier is created and then deleted, the integer continues to add 1, reaching the maximum value of the integer and returning to 0.

But each IPC object in the kernel identifier can only be recognized internally, in order to allow different processes to converge on the same IPC object, an external identifier is required to represent an IPC object, which is the key value. Or it can be understood that an identifier is a descriptor for an open IPC object, and the key value lets the process get the identifier.

Once we have created an IPC object with a key value, we can manipulate the IPC object with the identifier of the IPC object. For example, when a process obtains an identifier for a shared memory with the specified key value Shmid, it is possible to map the shared memory to its own address space through this identifier, and the subsequent operations on the shared memory are based on this identifier as well.

Message Queuing Programming:

 Message Queuing as one of the XSI IPC, many implementations and operations are similar to the semaphore and shared memory described earlier. Let's start by outlining the principle of Message Queuing: the essence of Message Queuing is a list of messages, and each message is structured as follows

struct MSGBUF {

Long Mtype; /* Message type, must be > 0 */

Char mtext[1]; /* Message Data */

};

Mtype: The type of message mtext[]: The data in the message

This means that the message queue is a data field is a linked list of the above structure. Now that you know the essence of a message queue is a list, you can imagine that if the process to communicate through Message Queuing, then the process of sending messages is the main task of building a data domain, and then to the kernel to insert into the list, and the process of receiving the message is through the kernel function to pull the data from the message queue. Message Queuing also has the following features:

* Message Queuing message types can be different, the process takes out the message can specify the message type to take out the required message

* When the process takes out a message, the message is immediately moved out of the message queue

The use of Message Queuing communication is accomplished primarily by:

* Create/Open message queue: int msgget (key_t key, int msgflg);

* Send message: int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg)

* Receive message: ssize_t MSGRCV (int msqid, void *msgp, size_t msgsz, long msgtyp,int MSGFLG)

* Delete Message Queuing: int msgctl (int msqid, int cmd, struct msqid_ds *buf)

These functions can be found in the actual use of the Man command, which is not explained in detail here, here is an example of a test:

Send program:

#include <sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>#include<stdio.h>#defineTEXT_SZ 2048structmsgt{LongMsgtype; CharMSGTEXT[TEXT_SZ];};intMain () {intMsgId;    key_t key; intrunning =1; structMSgt Msg_data; intMsgtype; Key= Ftok ("/home/application/massage_queue",2); //Creating message QueuesMsgId =msgget (key, ipc_creat); //Loops     while(running) {printf ("Please Input msgtype,input 0 to quit!\n"); scanf ("%d",&Msgtype); printf ("Please Input datas!\n"); //reading data from a terminalscanf"%s", Msg_data.msgtext); //writing data to a message queueMsg_data.msgtype =Msgtype; Msgsnd (MsgId,&msg_data,sizeof(Msg_data),0); if(STRNCMP (Msg_data.msgtext,"End",3)==0) {msgsnd (MsgId,"End",3,0); Running=0; }        }    //Delete Message QueuingMsgctl (MsgId, Ipc_rmid,0); return 0; }
View Code

Receive program:

#include <sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>#include<stdio.h>#include<unistd.h>#defineTEXT_SZ 2048structmsgt{LongMsgtype; CharMSGTEXT[TEXT_SZ];};intMsgId;voidchildprocess () {structMSgt msg_d; intrunning =1;  while(running) {//accept data from a message queueMSGRCV (MsgId, &msg_d,sizeof(Msg_d),0,0); //Print Dataprintf"Receive datas from queue:%s", Msg_d.msgtext); if(STRNCMP (Msg_d.msgtext,"End",3)==0) {Running=0; }    }        }intMain () {key_t key;    pid_t pid; inti; Key= Ftok ("/home/application/massage_queue",2); //Open Message QueuingMsgId =msgget (key, IPC_EXCL);  for(i=0;i<3; i++) {PID=Fork (); if(pid<0) {printf ("Fork error!\n"); }        Else if(pid==0) {childprocess (); }    }        return 0; }
View Code

When two programs are running, it can be found that data sent in sequence via send can be received in receive, indicating that the calls to these functions are successful, and that Message Queuing communication is successful.

Linux Message Queuing programming

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.