Sytem V Message Queue (2), sytemv Message Queue

Source: Internet
Author: User

Sytem V Message Queue (2), sytemv Message Queue
1. msgsnd Function
Function: adds a message to the message queue.
Int msgsnd (int msqid, // Message Queue flag returned by msgget?
Const void * msgp, // a pointer, only want to prepare the message to be sent
Size_t msgsz, // The length of the message that msgp points
Int msgflg); // controls what happens when the current message queue is full or when it reaches the system

Return Value:
0 is returned successfully.
-1 returned for failure
Note:
Msgflg = IPC_NOWAIT indicates that the queue is full and does not wait, and an EAGAIN error is returned. 0 indicates blocking wait
The message structure is restricted in two aspects. First, the specific data must be smaller than the upper limit MSGMAX. Second, it must start with a long int, and the receiver function will use this long integer to determine the message type.
Message structure Reference Form:
Struct msgbuf {
Long mtype;/* message type, must be> 0 */
Char mtext [1];/* message data */

};


Ii. msgrcv Functions
Function: receives messages from a message queue.
Ssize_t msgrcv (int msqid, // Message Queue identifier
Void * msgp, // pointer to the message to be received
Size_t msgsz, // message length
Long msgtyp, // simple form of receiving priority
Int msgflg); // controls what will happen when no message of the corresponding type in the queue is available for receiving. msgtype = 0; always ends in order.

Return Value:
Success: returns the number of characters actually placed in the receiving buffer.
Failed:-1 is returned.

Note:
Msgtype = 0 return the first message in the queue, received in order
Msgtype> 0 return the message with the first type equal to msgtype in the queue
Msgtype <0 returns the message whose first type is smaller than or equal to the absolute value of msgtype in the queue, and is the minimum message type that meets the condition.


Msgflg = IPC_NOWAIT, the queue does not have readable messages, do not wait (do not block), return ENOMSG error.

Msgflg = MSG_NOERROR. The message is truncated when the message size exceeds msgsz.

Msgtype> 0 and msgflg = msg_0000t. The receiving type is not equal to the first message of msgtype.


Sending function msg_send.c

# Include <unistd. h> # include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h> # include <stdlib. h> # include <stdio. h> # include <errno. h> # include <string. h ># define ERR_EXIT (m) \ do \ {\ perror (m); \ exit (EXIT_FAILURE) ;\} while (0) struct msgbuf {long mtype; // message type, must be> 0 char mtext [1]; // message data}; int main (int argc, char * argv []) {if (argc! = 3) {fprintf (stderr, "Usage: % s <bytes> <types> \ n", argv [0]); exit (EXIT_FAILURE );} int len = atoi (argv [1]); // message length int type = atoi (argv [2]); // Message type int msgid; msgid = msgget (1234); // open a Message Queue if (msgid =-1) ERR_EXIT ("msgget err"); struct msgbuf * ptr; ptr = (struct msgbuf *) malloc (sizeof (long) + len); // allocate space ptr-> mtype = type; if (msgsnd (msgid, ptr, len, 0) <0) // send the message ERR_EXIT ("msgsnd") to the Message Queue; return 0 ;}


Receiving function msg_rcv.c

# Include <unistd. h> # include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h> # include <stdlib. h> # include <stdio. h> # include <errno. h> # include <string. h ># define ERR_EXIT (m) \ do \ {\ perror (m); \ exit (EXIT_FAILURE) ;\} while (0) # define MSGMAX 8192 struct msgbuf {long mtype; // message type, must be> 0 char mtext [1]; // message data}; int main (int argc, char * argv []) {int flag = 0; // The int type = 0; in is blocked by default. T opt; while (1) {opt = getopt (argc, argv, "nt:"); // The parameter if (opt = '? ') ERR_EXIT ("getopt err"); if (opt =-1) break; switch (opt) {case 'N ': // printf ("AAAA \ n"); flag | = IPC_NOWAIT; // receives the break in a non-blocking manner; case 'T ': // printf ("BBBB \ n"); // int n = atoi (optarg); // printf ("n = % d \ n", n ); type = atoi (optarg); // type save, receive the corresponding type of message break; default: break;} int msgid; msgid = msgget ); if (msgid =-1) ERR_EXIT ("msgget err"); struct msgbuf * ptr; ptr = (struct msgbuf *) malloc (sizeof (long) + MSGMAX ); ptr-> mtype = type; int n = 0; // if (n = msgrcv (msgid, ptr, MSGMAX, type, flag) received bytes <0) ERR_EXIT ("msgsnd"); printf ("read % d bytes type = % ld \ n", n, ptr-> mtype); return 0 ;}

Send:


When sending a 1 100 message with a length of 1 and a 100 message type, it fails because no message queue is created and msg_get is used to create a message queue,

Receive:

You can receive messages in sequence or in a specified message type. The-n option indicates that messages are received in a non-blocking manner, and the-t option specifies that messages are received in a type.

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.