Msgsnd () function

Source: Internet
Author: User

From the function name, we can see that the msgsnd () function is used to send messages to the message queue. In Linux/msg. h it
The function is defined as follows:
System Call: msgsnd ()
Function declaration: int msgsnd (INT msqid, struct msgbuf * msgp, int msgsz, int msgflg)
Return Value: 0 on success
-1 on error: errno = eagain (queue is full, and ipc_nowait was asserted)
Eacces (permission denied, no write permission)
Efault (msgp address isn't accessable-invalid)
Eidrm (the message queue has been removed)
Eintr (received a signal while waiting to write)
Einval (Invalid Message Queue identifier, nonpositive
Message type, or Invalid Message Size)
Enomem (not enough memory to copy message buffer)
The first parameter msqid passed to the msgsnd () function is the identifier of the Message Queue object (obtained by the msgget () function ).
To), the second parameter msgp points to the memory of the message to be sent, and the third parameter msgsz is
Length (number of bytes), which can be calculated using the following formula:
Msgsz = sizeof (struct mymsgbuf)-sizeof (long );
The fourth parameter is a sign that controls the behavior of a function. You can take the following values:
0. Ignore flag spaces;
Ipc_nowait: if the message queue is full, the message will not be written to the queue, and the control will return the line that calls the function.
. If this parameter is not specified, the thread will be blocked until the message can be written.
Here we will create an encapsulation function to demonstrate the use of the msgsnd () function:
Int send_message (INT qid, struct mymsgbuf * qbuf)
{
Int result, length;
/* The length is essential the size of the structure minus sizeof (mtype )*/
Length = sizeof (struct mymsgbuf)-sizeof (long );
If (result = msgsnd (qid, qbuf, length, 0) =-1)
{
Return (-1 );
}
Return (result );
}

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.