Linux IPC Practice (5)--system v Message Queuing (2)

Source: Internet
Author: User
Tags message queue

message Send/Receive API

msgsnd function

int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg);

Parameters

MsgId: The message queue identifier returned by the Msgget function, or it can be an ID number of an existing message queue that is queried through the IPCS command

MSGP: is a pointer to a message ready to be sent,

MSGSZ: Is the message length that the MSGP points to, note: This length does not contain the long int that holds the message type

MSGFLG: Controls What will happen if the current message queue is full or reaches the system upper limit , and if Msgflg = ipc_nowait indicates that the queue is full and does not wait, the Eagain error is returned.

The message structure is constrained in two ways: (1) it must be less than the system-specified upper limit (Msgmax); (2) It must start with a long int, and the receiver function will use this long integer to determine the type of the message ;

The message structure reference form is as follows: struct msgbuf{    long mtype;       /* Message type, must be > 0 */    char mtext[1];    /* message data, can be set to more bytes */};
/** Example 1: Test 1: Send a message with a maximum length of 8192 bytes, once this value is exceeded, msgsnd error, prompting Invalid argument error, Test 2: The maximum number of bytes that Message Queuing can receive 16384 bytes, once this length is exceeded, If the MSGFLG is 0 (blocking mode), the process will block until there is a process to take the message away; If MSGFLG is in ipc_nowait mode, a byte is not written to the message queue, and a direct error is returned; **/int Main (int argc, char *argv[]) {    if (argc! = 3)        err_quit (" Usage:./main <type> <length> ");    int type = Atoi (argv[1]);    int len = atoi (argv[2]);    int msgid = Msgget (0x255, 0666| Ipc_creat);    if (MsgId = =-1)        err_exit ("Msgget error");    struct MSGBUF *buf;    BUF = (struct msgbuf *) malloc (len + sizeof (msgbuf::mtype));    Buf->mtype = type;    if (msgsnd (MsgId, buf, Len, ipc_nowait) = =-1)        err_exit ("msgsnd error");}

MSGRCV function

ssize_t MSGRCV (int msqid, void *msgp, size_t msgsz, long Msgtyp, int msgflg);

Parameters

MSGID: Message Queue identification code returned by the Msgget function

MSGP: is a pointer to the message ready to be received;

Msgsz: Is the length of the message that the MSGP points to, which does not contain the long int length integer that holds the message type

Msgtype: It can achieve a simple form of receiving priority (see)

MSGFLG: controls What is going to happen when there are no corresponding types of messages in the queue to receive (see)

return value:

Success--Returns the number of bytes actually put into the receive buffer (Note: This does not contain the length of the mtype in Msgbuf [man-page: MSGRCV () returns the amount of bytes a Ctually copied into the mtext array. ]);

failed-returns-1;

Msgtyp parameters

Msgtyp=0

Returns the first message of the queue

Msgtyp>0

Returns a message with the first type of the queue equal to Msgtype

Msgtyp<0

Returns a message with the first type of the queue that is less than or equal to (<=) Msgtype absolute value, and is the message with the smallest message type that satisfies the condition (receives the message in the order in which the type is sorted)

MSGFLG parameters

Msgflg=ipc_nowait

The queue does not have a readable message and does not wait to return a enomsg error.

Msgflg=msg_noerror

Message size exceeds MSGSZ (the third parameter of the MSGRCV function) is truncated and does not error

Msgtyp>0 and Msgflg=msg_except

The first message that the receive type is not equal to Msgtype

/** Example 2: Message receiving (in conjunction with program usage in Example 1) Description:-T [number], specifying the type of message received, the value of type-N, specifying the receive message in Ipc_nowait mode **/int main (int argc, char *argv[]    ) {/** parse parameter **/int type = 0;    int flag = 0;    int opt;            while (opt = getopt (argc, argv, "NT:"))! =-1) {switch (opt) {case ' n '://Specify ipc_nowait option            Flag |= ipc_nowait;        Break            Case ' t '://Specifies the type to receive, if 0, the description is received in order of type = Atoi (Optarg);        Break        Default:exit (exit_failure);    }} int msgid = Msgget (0x255, 0);    if (MsgId = =-1) err_exit ("Msgget error");    const int Msgmax = 8192;    Specifies the maximum length of a message struct MSGBUF *buf;    BUF = (struct msgbuf *) malloc (Msgmax + sizeof (buf->mtype));    ssize_t NRCV;    if (NRCV = MSGRCV (MsgId, buf, Msgmax, type, flag)) = =-1) err_exit ("MSGRCV error"); cout << "recv" << nrcv << "bytes, type =" << buf->mtype << Endl;}

2. Message reception: The data is constantly taken from the head of the team int main (int argc, char *argv[]) {    int msgid = msgget (0x1234, 0);    if (MsgId = =-1)        err_exit ("Msgget error");    struct MSGBUF buf;    ssize_t NRCV;    while (NRCV = MSGRCV (MsgId, &buf, sizeof (Buf.mtext), 0, ipc_nowait)) > 0)    {        cout << "recv" << Nrcv << "bytes, type:" << buf.mtype        << ", message:" << buf.mtext << Endl;    }}

[Use of]-getopt function

#include <unistd.h>int getopt (int argc, char * const argv[],                  const char *optstring); extern char *optarg;extern int Optind, Opterr, optopt;
Example: parsing./main-n-T 3 parameter options int main (int argc, char *argv[]) {while    (true)    {        int opt = getopt (argc, argv, "n T: ");        if (opt = = '? ')            Exit (exit_failure);        else if (opt = =-1) break            ;        Switch (opt)        {case        ' n ':            cout << "-n" << Endl;            break;        Case ' t ':            int n = atoi (optarg);            cout << "T" << n << Endl;            Break;}}}    

Linux IPC Practice (5)--system v Message Queuing (2)

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.