Embedded Linux interprocess communication (vii)--Message Queuing

Source: Internet
Author: User

embedded Linux interprocess Communication (vii)--Message Queuing one, Message Queuing

Message Queuing is used for interprocess communication on the same machine, and is a queue that is used to hold messages in the system kernel, which appears as a list of messages in the system kernel. The structure of the nodes in the message list is declared with MSG. Message Queuing is a method of sending chunks of data from one process to another . Each block of data is considered to contain a type, and the receiving process can independently receive data structures that contain different types. Message Queuing avoids synchronization and blocking problems with named pipes, but each data block has a maximum length limit.

1.msgget function

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

int Msgget (key_t key, int msgflg);

according to Key Create and access a message queue , MSGFLG is a permission flag , returns a Key - named Message Queue Identifier (non-zero integer), return 1 on Failure .

2,msgsnd

int msgsnd (int msgid, const void *msgptr, size_t msgsz, int msgflg);

To add a message to the message queue

MsgId is The message queue identifier returned by the Msgget function

msgptr is a pointer to of the message buffer pointers, data structure definitions for messages as follows :

struct MSGBUF {

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

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

};

Msgsz is the length of the message that msg_ptr points to, not the length of the entire struct,MSG_SZ is the length of the member variable that does not include the long integer message type.

The MSGFLG is used to control what will happen when the current message queue is full or the queue message reaches the system-wide limit.

If the call succeeds, a single copy of the message data is placed in the message queue and returns 0, returning 1on Failure.

3. MSGRCV

ssize_t MSGRCV (int msqid, void *msgptr, size_t msgsz, Long msgtyp,int MSGFLG);

Getting messages from a message queue

MsgId is The message queue identifier returned by the Msgget function

msgptr is a pointer to of the message buffer Pointers

Msgsz is the length of the message that msg_ptr points to, not the length of the entire struct,MSG_SZ is the length of the member variable that does not include the long integer message type.

the Msgtype can implement a simple receive priority. If msgtype is 0, it gets the first message in the queue. If its value is greater than 0, it gets the first information with the same message type. If it is less than 0, it gets the first message with a type equal to or less than the absolute value of Msgtype.

The MSGFLG is used to control what happens when there are no corresponding types of messages in the queue that can be received.

When the call succeeds, the function returns the number of bytes placed in the receive buffer, the message is copied to the user-allocated buffer that is pointed to by Msgptr , and the corresponding message in the message queue is deleted. Returns -1on Failure.

4. Msgctl

int msgctl (int msqid, int cmd, struct msqid_ds *buf);

Used to control Message Queuing

Command is the control type of the function, there are three kinds:

Ipc_stat: Sets the data in the MSGID_DS structure to the current associated value of the message queue, which overwrites the value of Msgid_ds with the current association value of the message queue .

Ipc_set: If the process has sufficient permissions, set the current association value for the message queue to the value given in the MSGID_DS structure

Ipc_rmid: Deleting Message Queuing

BUF is a Pointer to the MSGID_DS structure.

the MSGID_DS structure includes at least the following members:

struct MSGID_DS

{

uid_t Shm_perm.uid;

uid_t Shm_perm.gid;

mode_t Shm_perm.mode;

};

Returns 0on Success, 1on Failure.

Second, Message Queuing Programming Instances

Implementing Message Queuing for sending and receiving messages between two processes

Send Message program MSGSND.C:

#include  <stdio.h> #include  <unistd.h> #include  <string.h> #include  <sys /ipc.h> #include  <sys/msg.h>  #define  TEXT_SIZE 1024 typedef struct  msgbuf{    long mtype ;    int  status ;     char mtext[text_size] ;} Message; int main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV) {int msqid;struct msqid_ds  Info;message buf;int flag;int sndlen;key_t key = ftok ("msgsnd.c",  ' K '); Msqid = msgget (key, 0666| Ipc_creat);buf.mtype = 1 ;    buf.status = 9 ;     strcpy (buf.mtext,  "happy new year!")  ;    sndlen = sizeof (Message)  - sizeof (long)  ;   &NBSP;&NBSP;FLAG&NBSP;=&NBSP;MSGSND ( msqid, &buf, sndlen, 0) ;        buf.mtype = 3 ;     buf.status = 9 ;    strcpy (buf.mtext,  "good bye!")  ;    sndlen = sizeof (Message)  - sizeof (long)  ;   &NBSP;&NBSP;FLAG&NBSP;=&NBSP;MSGSND ( msqid, &buf, sndlen, 0)  ;return 0;}

Receive message program Msgrcv.c

#include  <stdio.h> #include  <unistd.h> #include  <string.h> #include  <sys /ipc.h> #include  <sys/msg.h>  #define  TEXT_SIZE 1024 typedef struct  msgbuf{    long mtype ;    int  status ;     char mtext[text_size] ;} Message; int main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV) {int msqid;struct msqid_ds  info;message buf;int flag;int mtype = 1;int rcvlen;key_t key =  Ftok ("msgsnd.c",  ' K '); Msqid = msgget (key, 0);    rcvlen =  sizeof (message)  - sizeof (long)  ;    bzero (&buf, sizeof (BUF)); &NBSP;&NBSP;&NBSP;&NBSP;FLAG&NBSP;=&NBSP;MSGRCV ( msqid, &buf, rcvlen, mtype, 0)  ;    printf ("%s\n",  buf.mtext);    &Nbsp;msgctl (msqid, ipc_rmid, null);     return 0;} 

The above code knowledge as a learning test use, not add error handling, the actual work needs to add a variety of possible errors and exception handling.


This article from "Endless life, Struggle not only" blog, reprint please contact the author!

Embedded Linux interprocess communication (vii)--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.