|
|
This section describes how to use the msgsnd and msgrcv system calls. The accompanying program illustrates their use.
The synopsis found on the msgop(2) manual page is as follows:
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>int msgsnd (int msqid, const void *msgp, size_t msgsz, int msgflg)
int msgrcv (int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg)
The msgsnd system call requires four arguments to be passed to it. It returns an integer value.
When successful, it returns a zero value; when unsuccessful, msgsnd returns a -1.
The msqid argument must be a valid, non-negative, integer value. In other words, it must have already been created by using the msgget system call.
The msgp argument is a pointer to a structure in the user memory area that contains the type of the message and the message to be sent.
The msgsz argument specifies the length of the character array in the data structure pointed to by the msgp argument. This is the length of the message. The maximum size of this array is determined by the MSGMAX system-tunable parameter.
The msgflg argument allows the ``blocking message operation'' to be performed if the IPC_NOWAIT flag is not set ((msgflg and IPC_NOWAIT)= = 0); the operation would block if the total number of bytes allowed on the specified message queue are in use (msg_qbytes or MSGMNB), or the total system-wide number of messages on all queues is equal to the system- imposed limit (MSGTQL). If the IPC_NOWAIT flag is set, the system call will fail and return a -1.
The value of the
msg_qbytes
data structure member can be lowered
from
MSGMNB
by using the
msgctl
IPC_SET
control command,
but only
the root (if the SUM privilege module is installed)
can raise it afterwards.
Further details of this system call are discussed in the next example. If you need more information on the logic manipulations in this program, read ``Using msgget''. It goes into more detail than would be practical for every system call.
The msgrcv system call requires five arguments to be passed to it; it returns an integer value.
When successful, it returns a value equal to the number of bytes received; when unsuccessful it returns a -1.
The msqid argument must be a valid, non-negative, integer value. In other words, it must have already been created by using the msgget system call.
The msgp argument is a pointer to a structure in the user memory area that will receive the message type and the message text.
The msgsz argument specifies the length of the message to be received. If its value is less than the message in the array, an error can be returned if desired (see the msgflg argument below).
The msgtyp argument is used to pick the first message on the message queue of the particular type specified. If it is equal to zero, the first message on the queue is received; if it is greater than zero, the first message of the same type is received; if it is less than zero, the lowest type that is less than or equal to its absolute value is received.
The msgflg argument allows the ``blocking message operation'' to be performed if the IPC_NOWAIT flag is not set ((msgflg and IPC_NOWAIT) == 0); the operation would block if there is not a message on the message queue of the desired type (msgtyp) to be received. If the IPC_NOWAIT flag is set, the system call will fail immediately when there is not a message of the desired type on the queue. msgflg can also specify that the system call fail if the message is longer than the size to be received; this is done by not setting the MSG_NOERROR flag in the msgflg argument ((msgflg and MSG_NOERROR)) == 0). If the MSG_NOERROR flag is set, the message is truncated to the length specified by the msgsz argument of msgrcv.
Further details of this system call are discussed in the following program. If you need more information on the logic manipulations in this program, read ``Using msgget''. It goes into more detail than would be practical for every system call.