MSGOPSection: Linux Programmer's Manual (2)Updated: 2002-06-01 |
MSGOPSection: Linux Programmer's Manual (2)Updated: 2002-06-01 |
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgsnd(int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp, int msgflg);
struct msgbuf {
long mtype;
/* message type, must be > 0 */
char mtext[1];
/* message data */
};
The
mtext
field is an array (or other structure) whose size is specified by
msgsz,
a non-negative integer value.
Messages of zero length (i.e., no
mtext
field) are permitted.
The
mtype
field must have a strictly positive integer value that can be
used by the receiving process for message selection
(see the section about
msgrcv).
The calling process must have write permission to send and read permission to receive a message on the queue.
The msgsnd system call appends a copy of the message pointed to by msgp to the message queue whose identifier is specified by msqid.
If sufficient space is available on the queue, msgsnd succeeds immediately. (The queue capacity is defined by the msg_bytes field in the associated data structure for the message queue. During queue creation this field is initialised to MSGMNB bytes, but this limit can be modified using msgctl.) If insufficient space is available on the queue, then the default behaviour of msgsnd is to block until space becomes available. If IPC_NOWAIT is asserted in msgflg then the call instead fails with the error EAGAIN.
A blocked msgsnd call may also fail if the queue is removed (in which case the system call fails with errno set to EIDRM), or a signal is caught (in which case the system call fails with errno set to EINTR). (msgsnd and msgrcv are never automatically restarted after being interrupted by a signal handler, regardless of the setting of the SA_RESTART flag when establishing a signal handler.)
Upon successful completion the message queue data structure is updated as follows:
The system call msgrcv reads a message from the message queue specified by msqid into the msgbuf pointed to by the msgp argument, removing the read message from the queue.
The argument msgsz specifies the maximum size in bytes for the member mtext of the structure pointed to by the msgp argument. If the message text has length greater than msgsz, then if the msgflg argument asserts MSG_NOERROR, the message text will be truncated (and the truncated part will be lost), otherwise the message isn't removed from the queue and the system call fails returning with errno set to E2BIG.
The argument msgtyp specifies the type of message requested as follows:
The msgflg argument asserts none, one or more (or-ing them) of the following flags:
If no message of the requested type is available and IPC_NOWAIT isn't asserted in msgflg, the calling process is blocked until one of the following conditions occurs:
Upon successful completion the message queue data structure is updated as follows:
When msgrcv fails, at return errno will be set to one among the following values:
The implementation has no intrinsic limits for the system wide maximum number of message headers (MSGTQL) and for the system wide maximum size in bytes of the message pool (MSGPOOL).