Linux System v Message Queuing implements the MSGSND client/server and the MSGRCV function

Source: Internet
Author: User
Tags message queue

First, msgsnd and MSGRCV functions

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

Function: Add a message to the message queue

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

Parameters

MSGID: Message Queue identification code returned by the Msgget function

MSGP: is a pointer to the message structure that is ready to be sent

MSGSZ: is the message length that the MSGP points to, this length does not contain the long int that holds the message type

MSGFLG: Controls what happens when the current message queue is full or arrives on the system

Return value: successful return 0; failure return-1

Msgflg=ipc_nowait indicates that the queue is full without waiting and returns a Eagain error. For 0 indicates blocking wait

The message structure is constrained in two ways. First, its specific data must be smaller than the system-specified upper-bound value msgmax; second, it must begin with a long int, which will use this long integer to determine the type of message.

The message structure reference form is as follows:

struct MSGBUF {

Long Mtype;

char mtext [1];

};

The Mtext field is a array (or other structure) whose the size was specified by Msgsz, a nonnegative integer value. Messages of zero length (i.e., no mtext field) are permitted.

That is, the Mtex area can be a number or structure, and the size is indicated by the parameter msgsz.

Function: To receive messages from a message queue

Prototype 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 a message structure that is ready to receive

MSGSZ: is the message length that the MSGP points to, this length does not contain the long int that holds the message type

Msgtype: It can achieve a simple form of receive priority

MSGFLG: Control what happens when there is no corresponding type of message in the queue to receive

Return value: Successfully returns the number of characters actually placed in the receive buffer, failure returns-1

Msgtype=0 returns the first message of the queue

MSGTYPE>0 returns the message that the first type of the queue equals Msgtype

MSGTYPE<0 returns a message that the first type of the queue is less than or equal to the Msgtype absolute value and is the smallest message type that satisfies the condition

Msgflg=ipc_nowait, the queue does not wait for a readable message and returns a enomsg error.

Msgflg=msg_noerror, truncated when message size exceeds MSGSZ

Msgtype>0 and Msgflg=msg_except, the receiving type is not equal to the first message of Msgtype.

Second, the message queue implementation of the ECHO client/server

In the previous series, we used socket sockets to implement a MSGSND,MSGRCV client/server program, and now try to use Message Queuing to implement it, mainly using the two functions described above.

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.