Linux Message Queue operations)

Source: Internet
Author: User

There are three types of Message Queue operations:

1. Open or create a message queue
The kernel persistence of Message Queue requires that each Message Queue corresponds to a unique key value within the system range. Therefore, you must obtain a Message Queue description, you only need to provide the key value of the message queue;

Note: The Message Queue description is generated by a unique key value within the system, and the key value can be seen as a path corresponding to the system.

2. read/write operations

Message read/write operations are very simple. for developers, each message is similar to the following data structure:

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

Mtype members represent the message type. An important basis for reading a message from a message queue is the message type. mtext is the message content, and the length is not necessarily set to 1. Therefore, for sending a message, you can first preset a msgbuf buffer and write the message type and content, and call the corresponding sending function. For reading a message, first assign such a msgbuf buffer, then, read the message into the buffer.

3. Get or set Message Queue attributes:

Message Queue information is basically stored in the message queue header. Therefore, you can assign a structure similar to the Message Queue header (struct msqid_ds, see appendix 2) To return the Message Queue attributes; you can also set the data structure.

Message Queue 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 corresponding to the path pathname. This function does not directly operate on message queues, but calls IPC (msgget ,...) This function is often called before msgget () is used to obtain the Message Queue description. The typical Call code is:

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

2. System V Message Queue API
There are four System V Message Queue APIs, which must contain several header files:

#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 obtained by ftok, And the msgflg parameter is a flag. This call returns the Message Queue description corresponding to the key.

In either of the following cases, this call creates a new message queue:

  • If no message queue corresponds to the key, and msgflg contains the ipc_creat flag;
  • The key parameter is ipc_private;

The msgflg parameter can be set to the following: ipc_creat, ipc_excl, ipc_nowait, or the result.

Call return:The message queue description is returned. Otherwise,-1 is returned.

Note: Setting the parameter key to the constant ipc_private does not mean that other processes cannot access the message queue. It only means that a new message queue will be created.

2) int msgrcv (INT msqid, struct msgbuf * msgp, int msgsz, long msgtyp, int msgflg );
The system calls to read a message from the Message Queue represented by msgid, and store the message in the msgbuf structure pointed by msgp.

Msqid is the description word of the Message Queue. After a message is returned, it is stored in the address pointed to by msgp. msgsz specifies the length of the mtext member of msgbuf (that is, the length of the message content ), msgtyp is the type of message to be read by the request. The message reading Mark msgflg can be of the following common values or:

  • Ipc_nowait if no message meets the condition, the call returns immediately. At this time, errno = enomsg
  • If ipc_except and msgtyp> 0 are used together, messages of the first type not msgtyp in the queue are returned.
  • Ipc_noerror if the content of the message in the queue that meets the condition is greater than the requested msgsz byte, the message is truncated and the truncation part is lost.

The msgrcv manual details the message type when different values are obtained (> 0; <0; = 0). The call will return the message in the message queue.

Msgrcv () has three conditions to remove blocking:

  1. The message queue contains messages that meet the conditions;
  2. Message Queue represented by msqid is deleted;
  3. The process that calls msgrcv () is interrupted by the signal;

Call return:The actual number of bytes of the read message is returned. Otherwise,-1 is returned.

3) int msgsnd (INT msqid, struct msgbuf * msgp, int msgsz, int msgflg );
Send a message to the Message Queue represented by msgid. The message to be sent is stored in the msgbuf structure pointed to by msgp. The message size is specified by msgze.

For sending a message, the meaningful msgflg flag is ipc_nowait, indicating whether msgsnd waits when the message queue does not have enough space to accommodate the message to be sent. There are two conditions that cause msgsnd () 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 bytes) in the current message queue is no less than the total capacity (in bytes) of the Message Queue. At this time, although the number of messages in the message queue is large, however, there is only one byte.

There are three conditions for msgsnd () to remove blocking:

  1. The preceding two conditions are not met, that is, the message queue has a space to accommodate the message;
  2. Message Queue represented by msqid is deleted;
  3. The process that calls msgsnd () is interrupted by the signal;

Call return:0 is returned. Otherwise,-1 is returned.

4) int msgctl (INT msqid, int cmd, struct msqid_ds * BUF );
The system calls the CMD operation on the message queue identified by msqid. There are three kinds of CMD operations: ipc_stat, ipc_set, and ipc_rmid.

  1. Ipc_stat: this command is used to obtain Message Queue information. The returned information is stored in the msqid structure pointed to by the Buf;
  2. Ipc_set: this command is used to set the attributes of a message queue. The attributes to be set are stored in the msqid structure pointed to by the Buf. The attributes can be set to msg_perm.uid, msg_perm.gid, msg_perm.mode, and msg_qbytes, it also affects msg_ctime members.
  3. Ipc_rmid: deletes the Message Queue identified by msqid;

Call return:0 is returned. Otherwise,-1 is returned.

Bytes ----------------------------------------------------------------------------------------------------------------------

/* 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 deleted. We need to use the ipcrm command to delete it */
/* 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 );
}
Http://blog.csdn.net/zhsp1029/article/details/2171462

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.