Linux Message Queuing operations

Source: Internet
Author: User
Tags call back message queue

There are three types of operations on Message Queuing:

1. Open or create a message queue
The kernel persistence of Message Queuing requires that each message queue has a corresponding unique key value within the system, so to obtain a descriptive narrative word for a message queue, only the key value of the message queue can be provided;

Note: Message Queuing descriptive words are generated by a unique key value within the system, and the key value can be regarded as a path within the corresponding system.

2. Read and write operation

Message read and write operations are easy, for developers, each message is similar such as the following data structure:

struct msgbuf{long mtype;char mtext[1];};

The Mtype member represents the message type, and an important basis for reading the message from the message queue is the type of the message; Mtext is the message content, of course, the length is not necessarily 1. Therefore, for sending a message, first preset a msgbuf buffer and write the message type and content, call the corresponding send function can be, to read the message, the first allocation of such a msgbuf buffer, and then read the message into the buffer can be.

3. Get or set Message Queuing properties:

Message Queuing information is essentially stored in the message queue header, so it is possible to assign a structure similar to the message queue header (struct Msqid_ds, see Appendix 2), to return the properties of the message queue, and the same ability to set the data structure.



Message Queuing API

1. File name to key value

#include <sys/types.h>#include <sys/ipc.h>key_t ftok (char*pathname, char proj);

It returns a key value that corresponds to the path pathname. The function does not operate directly on the message queue, but in the call IPC (Msgget,...) or Msgget () to get a message queue to describe the narrative word, often to call the function. The typical calling code is:

   key=ftok(path_ptr, ‘a‘);    ipc_id=ipc(MSGGET, (int)key, flags,0,NULL,0);    …

2. System V Message Queuing API
The System V Message Queuing API has four together and requires several header files to be used:

#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>

1) int msgget (key_t key, int msgflg)

The parameter key is a key value, which is obtained by Ftok; MSGFLG is a number of flag bits. The call returns a description of the message queue that corresponds to the health value key.

In the following two scenarios, the call creates a new message queue:

    • Suppose there is no message queue corresponding to the health value key, and the MSGFLG includes the ipc_creat flag bit;
    • Key parameter is ipc_private;


The number of MSGFLG can be as follows: Ipc_creat, IPC_EXCL, ipc_nowait, or three or the result.

Call Back: successfully returns the message queue descriptive narrative word, otherwise returns-1.

Note: The parameter key set to constant ipc_private does not mean that other processes cannot access the message queue, just means that a new message queue is about to be created.

2) int MSGRCV (int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg);
The system call reads a message from the message queue represented by MsgId and stores the message in the MSGBUF structure pointed to by MSGP.

Msqid describes the narrative word for Message Queuing; When a message is returned, it is stored at the address pointed to by MSGP, MSGSZ specifies the length of the Mtext member of the MSGBUF (that is, the length of the message content), and Msgtyp the type of message read for the request; Read message flag MSGFLG can be the following constant value or:

    • Ipc_nowait assumes that there are no messages that satisfy the condition, the call returns immediately, and at this point, errno=enomsg
    • Ipc_except with msgtyp>0, returns a message with the first type in the queue that is not Msgtyp
    • Ipc_noerror assumes that the message content in the queue satisfies the condition is greater than the requested MSGSZ Byte, then truncates the message and the truncation is lost.


In the MSGRCV manual, when the message type is given a different value (>0; <0; =0), the call returns which message in the message queue.

MSGRCV () There are three conditions for lifting the blockage:

    1. The message queue has the message that satisfies the condition;
    2. The message queue msqid represents is deleted;
    3. The process of calling MSGRCV () is interrupted by the signal;


Call return: returns the actual number of bytes of the read-out message successfully, otherwise 1.

3) int msgsnd (int msqid, struct msgbuf *msgp, int msgsz, int msgflg);
Sends a message to the message queue represented by MsgId, and the message to be sent is stored in the MSGBUF structure that MSGP points to, and the size of the message is specified by Msgze.

For sending messages, the meaningful MSGFLG flag is ipc_nowait, indicating whether MSGSND waits if the message queue does not have enough space to hold the message to be sent. There are two conditions that cause msgsnd () to wait:

    • The sum of the current message size and the number of bytes in the current message queue exceeds the total capacity of the message queue;
    • The number of messages in the current message queue (units ") is not less than the total capacity of the message queue (in bytes), although there is a large number of messages in the message queue, but basically only one byte.


MSGSND () There are three conditions for lifting the blockage:

    1. The above two conditions are not satisfied, that is, the message queue in the space to accommodate the message;
    2. The message queue msqid represents is deleted;
    3. The process of calling msgsnd () is interrupted by the signal;


Call Back: returns 0 successfully, otherwise returns-1.

4) int msgctl (int msqid, int cmd, struct msqid_ds *buf);
The system invokes the CMD operation on the message queue identified by the msqid and has three cmd operations: Ipc_stat, Ipc_set, Ipc_rmid.

    1. Ipc_stat: This command is used to obtain message queue information, and the returned information is stored in the MSQID structure pointed to by BUF;
    2. Ipc_set: The command is used to set the properties of the message queue, the properties to be set are stored in the MSQID structure that buf points to, and the properties that can be set include: Msg_perm.uid, Msg_perm.gid, Msg_perm.mode, and Msg_qbytes, At the same time, it also affects msg_ctime members.
    3. Ipc_rmid: Delete Message queue for msqid identity;


Call Back: returns 0 successfully, otherwise returns-1.

--------------------------------------------------------------------------------------------------------------- -------

/*msgserver.c*/

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>

#define Msg_file "MSGSERVER.C"
#define BUFFER 255
#define PERM s_irusr| S_iwusr
/* The message queue created by the server is not finally deleted, we want to use the IPCRM command to delete the */
/* IPCRM-Q <msqid> * *

struct Msgtype {
Long Mtype;
Char buffer[buffer+1];
};

int main ()
{
struct Msgtype msg;
key_t key;
int msgid;

if ((Key=ftok (Msg_file, ' a ')) ==-1)
{
fprintf (stderr, "creat Key error:%s/n", Strerror (errno));
Exit (1);
}

if (Msgid=msgget (Key, perm| Ipc_creat| IPC_EXCL)) ==-1)
{
fprintf (stderr, "creat Message error:%s/n", Strerror (errno));
Exit (1);
}
printf ("msqid =%d/n", msgid);
while (1)
{
MSGRCV (MsgId, &msg, sizeof (struct msgtype), 1, 0);
fprintf (stderr, "Server receive:%s/n", Msg.buffer);
Msg.mtype = 2;
Msgsnd (MsgId, &msg, sizeof (struct msgtype), 0);
}
Exit (0);
}

/* MSGCLIENT.C */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>

#define Msg_file "MSGSERVER.C"
#define BUFFER 255
#define PERM s_irusr| S_iwusr

struct Msgtype {
Long Mtype;
Char buffer[buffer+1];
};

int main (int argc, char **argv)
{
struct Msgtype msg;
key_t key;
int msgid;

if (argc! = 2)
{
fprintf (stderr, "usage:%s string/n", argv[0]);
Exit (1);
}

if ((Key=ftok (Msg_file, ' a ')) ==-1)
{
fprintf (stderr, "creat Key error:%s/n", Strerror (errno));
Exit (1);
}

if ((Msgid=msgget (Key, PERM)) ==-1)
{
fprintf (stderr, "creat Message error:%s/n", Strerror (errno));
Exit (1);
}

Msg.mtype = 1;
strncpy (Msg.buffer, argv[1], buffer);
Msgsnd (MsgId, &msg, sizeof (struct msgtype), 0);
memset (&msg, '/0 ', sizeof (struct msgtype));
MSGRCV (MsgId, &msg, sizeof (struct msgtype), 2, 0);
fprintf (stderr, "Client receive:%s/n", Msg.buffer);
Exit (0);
}

Linux Message Queuing operations

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.