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 );
}