Linux Message Queuing instances

Source: Internet
Author: User

Objective:

A message queue 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 it by a certain rule, and a process that has read access to a message queue can read messages from the message queue

Function:
1. Create a new message queue or get an existing message queue

Prototype: int msgget (key_t key, int msgflg);

Parameters:

Key: Can be thought of as a port number, or it can be generated by the function Ftok.

The Msgflg:ipc_creat value, which, if there is no queue, creates one and returns a new identifier, or returns the original identifier if it already exists.

A IPC_EXCL value that returns 1 if there is no queue, or 0 if it already exists.

2. Read/write messages to the queue

Prototype:

MSGRCV messages from the queue: ssize_t msgrcv (int msqid, void *msgp, size_t msgsz, long Msgtyp, int msgflg);

MSGSND put data into Message Queuing: int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg);

Parameters:

MSQID: Message Queue identification code

MSGP: A pointer to the message buffer, which is used to temporarily store messages sent and received, is a generic, user-definable structure, in the following form:

structmsgstru{    long mtype; //大于0    charmtext[512];};

MSGSZ: The size of the message.

Msgtyp: The message pattern read from within the message queue. A value of zero indicates that all messages in the message queue will be read.

MSGFLG: Used to indicate the action that the core program should take if the queue has no data. If the MSGFLG and constant ipc_nowait are pooled, then if the message queue is full at msgsnd () execution, then MSGSND () will not block, and will immediately return-1, if the MSGRCV () is performed, then when the message queue is empty, do not wait for the immediate return-1, and set the error code to enomsg. When MSGFLG is 0 o'clock, msgsnd () and MSGRCV () take a blocking wait processing mode when the queue is full or empty.

3. Set Message Queuing properties

Prototype: int msgctl (int msgqid, int cmd, struct msqid_ds *buf);

Parameters: The MSGCTL system invokes a CMD operation on the message queue identified by the msgqid, and the system defines 3 cmd operations: Ipc_stat, Ipc_set, Ipc_rmid
Ipc_stat: This command is used to obtain the MSQID_DS data structure for the message queue and save it to the BUF specified address space.
Ipc_set: The command is used to set the properties of the message queue, and the properties to be set are stored in BUF.

Ipc_rmid: Removes the MSQID identity message queue from the kernel.

Instance:

Message Send side: SEND.C

 1/*send.c*/2 #include <stdio.h> 3 #include <sys/types.h> 4 #include <sys/ipc.h> 5 #include <sys/msg.h> 6 #include <errno.h> 7 8 #define Msgkey 9-struct Msgstru Each {a long MS  Gtype   -Char msgtext[2048];  14};  Main () Msgstru msgs;  Msg_type int;  (+ char str[256];  int ret_value;  msqid int;  Msqid=msgget (MSGKEY,IPC_EXCL);  /* Check if Message Queuing exists * * if (Msqid < 0) {msqid = Msgget (msgkey,ipc_creat|0666);/* Create Message Queue * * if (msqid <0) { printf ("Failed to create MSQ |  errno=%d [%s]\n], Errno,strerror (errno));  Exit (-1);  (1) {The "Input message type (end:0):");  scanf ("%d", &msg_type);  if (Msg_type = = 0) PNs break;  * printf ("input message to be sent:");  scanf ("%s", str);  Msgs.msgtype = Msg_type;  strcpy (Msgs.msgtext, str); 42/* Send Message Queue */Ret_value = msgsnd (msqid,&msgs,sizeof (struct msgstru), ipc_nowait);  if (Ret_value < 0) {"msgsnd () write msg failed,errno=%d[%s]\n", Errno,strerror (errno));  (-1); Msgctl (msqid,ipc_rmid,0);   Delete Message Queuing 50}

Message Receiving end RECEIVE.C

 1/*receive.c */2 #include <stdio.h> 3 #include <sys/types.h> 4 #include <sys/ipc.h> 5 #inc Lude <sys/msg.h> 6 #include <errno.h> 7 8 #define Msgkey 9 Msgstru struct  Ong Msgtype;  -Char msgtext[2048];  14};  15 16/* Child process, listener message Queue */void Childproc () {Msgstru msgs;  Msgid,ret_value int;  (+ char str[512]; while (1) {MsgId = Msgget (MSGKEY,IPC_EXCL);/* check if Message Queuing exists */if (MsgId < 0) {pri NTF ("Msq not existed!  errno=%d [%s]\n], Errno,strerror (errno));  Sleep (2);  Continue;  28} 29/* Receive Message Queue */Ret_value = MSGRCV (msgid,&msgs,sizeof (struct msgstru), 0,0);  To printf ("text=[%s] pid=[%d]\n", msgs.msgtext,getpid ());  (+)-return;  () + void Main () PNS {i,cpid int;  Max/* Create 5 child process */i=0;i<5;i++ {cpid = fork (); if (Cpid < 0) * printf ("fork failed\n");  Cpid ==0/*child process*/childproc ();   47}

Linux Message Queuing instance

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.