内容简介
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> |
int msgsnd(int%uA0msqid,%uA0const void *msgp,%uA0size_t%uA0msgsz,%uA0int%uA0msgflg)
ssize_t msgrcv(int%uA0msqid,%uA0void *msgp,%uA0size_t%uA0msgsz,%uA0long%uA0msgtyp,%uA0int%uA0msgflg)
描述
The%uA0msgsnd() and%uA0msgrcv() system calls are used, respectively, to send messages to, and receive messages from, a message queue. The calling process must have write permission on the message queue in order to send a message, and read permission to receive a message.The%uA0msgp%uA0argument is a pointer to caller-defined structure of the following general form:
%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0struct msgbuf {%uA0
%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0long%uA0%uA0%uA0%uA0mtype%uA0%uA0 /* message type, must be > 0 */%uA0
%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0char%uA0%uA0%uA0%uA0mtext[1]%uA0%uA0%uA0%uA0%uA0%uA0%uA0 /* message data */%uA0
%uA0%uA0%uA0%uA0%uA0%uA0%uA0%uA0}
The%uA0mtext%uA0field is an array (or other structure) whose size is specified by%uA0msgsz, a non-negative integer value. Messages of zero length (i.e., no%uA0mtext%uA0field) are permitted. Themtype%uA0field must have a strictly positive integer value. This value can be used by the receiving process for message selection (see the description of%uA0msgrcv() below).
The%uA0msgsnd() system call appends a copy of the message pointed to by%uA0msgp%uA0to the message queue whose identifier is specified by%uA0msqid.
If sufficient space is available in the queue,%uA0msgsnd() succeeds immediately. (The queue capacity is defined by the%uA0msg_bytes%uA0field in the associated data structure for the message queue. During queue creation this field is initialised to%uA0MSGMNB%uA0bytes, but this limit can be modified using%uA0msgctl().) If insufficient space is available in the queue, then the default behaviour of%uA0msgsnd() is to block until space becomes available. IfIPC_NOWAIT%uA0is specified in%uA0msgflg, then the call instead fails with the error%uA0EAGAIN.
A blocked%uA0msgsnd() call may also fail if the queue is removed (in which case the system call fails with%uA0errno%uA0set to%uA0EIDRM), or a signal is caught (in which case the system call fails with%uA0errno%uA0set to%uA0EINTR). (msgsnd%uA0and%uA0msgrcv%uA0are never automatically restarted after being interrupted by a signal handler, regardless of the setting of theSA_RESTART%uA0flag when establishing a signal handler.)
Upon successful completion the message queue data structure is updated as follows:
标签 | 描述 |
---|---|
%uA0 | msg_lspid%uA0is set to the process ID of the calling process. |
%uA0 | msg_qnum%uA0is incremented by 1. |
%uA0 | msg_stime%uA0is set to the current time. |
The system call%uA0msgrcv() removes a message from the queue specified by%uA0msqid%uA0and places it in the buffer pointed to%uA0msgp. | |
The argument%uA0msgsz%uA0specifies the maximum size in bytes for the member%uA0mtext%uA0of the structure pointed to by the%uA0msgp%uA0argument. If the message text has length greater than%uA0msgsz, then the behaviour depends on whether%uA0MSG_NOERROR%uA0is specified inmsgflg. If%uA0MSG_NOERROR%uA0is specified, then the message text will be truncated (and the truncated part will be lost) if%uA0MSG_NOERROR%uA0is not specified, then the message isn’t removed from the queue and the system call fails returning -1 with%uA0errno%uA0set toE2BIG. | |
The argument%uA0msgtyp%uA0specifies the type of message requested as follows: | |
%uA0 | If%uA0msgtyp%uA0is 0, then the first message in the queue is read. |
%uA0 | If%uA0msgtyp%uA0is greater than 0, then the first message in the queue of type%uA0msgtyp%uA0is read, unless%uA0MSG_EXCEPT%uA0was specified inmsgflg, in which case the first message in the queue of type not equal to%uA0msgtyp%uA0will be read. |
%uA0 | If%uA0msgtyp%uA0is less than 0, then the first message in the queue with the lowest type less than or equal to the absolute value ofmsgtyp%uA0will be read. |
The%uA0msgflg%uA0argument is a bit mask constructed by ORing together zero or more of the following flags: | |
IPC_NOWAIT | |
%uA0 | Return immediately if no message of the requested type is in the queue. The system call fails with%uA0errno%uA0set to%uA0ENOMSG. |
MSG_EXCEPT | |
%uA0 | Used with%uA0msgtyp%uA0greater than 0 to read the first message in the queue with message type that differs from%uA0msgtyp. |
MSG_NOERROR | |
%uA0 | To truncate the message text if longer than%uA0msgsz%uA0bytes. |
If no message of the requested type is available and%uA0IPC_NOWAIT%uA0isn’t specified inmsgflg, the calling process is blocked until one of the following conditions occurs: | |
%uA0 | A message of the desired type is placed in the queue. |
%uA0 | The message queue is removed from the system. In this case the system call fails with%uA0errno%uA0set to%uA0EIDRM. |
%uA0 | The calling process catches a signal. In this case the system call fails with%uA0errno%uA0set to%uA0EINTR. |
Upon successful completion the message queue data structure is updated as follows: | |
%uA0 | msg_lrpid%uA0is set to the process ID of the calling process. |
%uA0 | msg_qnum%uA0is decremented by 1. |
%uA0 | msg_rtime%uA0is set to the current time. |
返回值
On failure both functions return -1 with%uA0errno%uA0indicating the error, otherwise%uA0msgsnd() returns 0 and%uA0msgrcv() returns the number of bytes actually copied into the%uA0mtextarray.错误
When%uA0msgsnd() fails,%uA0errno%uA0will be set to one among the following values:标签 | 描述 |
---|---|
EACCES | The calling process does not have write permission on the message queue, and does not have the%uA0CAP_IPC_OWNERcapability. |
EAGAIN | The message can’t be sent due to the%uA0msg_qbytes%uA0limit for the queue and%uA0IPC_NOWAIT%uA0was specified in%uA0msgflg. |
EFAULT | The address pointed to by%uA0msgp%uA0isn’t accessible. |
EIDRM | The message queue was removed. |
EINTR | Sleeping on a full message queue condition, the process caught a signal. |
EINVAL | Invalid%uA0msqid%uA0value, or non-positive%uA0mtype%uA0value, or invalidmsgsz%uA0value (less than 0 or greater than the system valueMSGMAX). |
ENOMEM | The system does not have enough memory to make a copy of the message pointed to by%uA0msgp. |
When%uA0msgrcv() fails,%uA0errno%uA0will be set to one among the following values: | |
E2BIG | The message text length is greater than%uA0msgsz%uA0andMSG_NOERROR%uA0isn’t specified in%uA0msgflg. |
EACCES | The calling process does not have read permission on the message queue, and does not have the%uA0CAP_IPC_OWNERcapability. |
EAGAIN | No message was available in the queue and%uA0IPC_NOWAIT%uA0was specified in%uA0msgflg. |
EFAULT | The address pointed to by%uA0msgp%uA0isn’t accessible. |
EIDRM | While the process was sleeping to receive a message, the message queue was removed. |
EINTR | While the process was sleeping to receive a message, the process caught a signal. |
EINVAL | msgqid%uA0was invalid, or%uA0msgsz%uA0was less than 0. |
ENOMSG | IPC_NOWAIT%uA0was specified in%uA0msgflg%uA0and no message of the requested type existed on the message queue. |
遵循于
SVr4, POSIX.1-2001.注意
The%uA0msgp%uA0argument is declared as%uA0struct msgbuf *%uA0with libc4, libc5, glibc 2.0, glibc 2.1. It is declared as%uA0void *%uA0with glibc 2.2 and later, as required by SUSv2 and SUSv3.The following limits on message queue resources affect the%uA0msgsnd() call:
标签 | 描述 |
---|---|
MSGMAX | Maximum size for a message text: 8192 bytes (on Linux, this limit can be read and modified via%uA0/proc/sys/kernel/msgmax). |
MSGMNB | Default maximum size in bytes of a message queue: 16384 bytes (on Linux, this limit can be read and modified via/proc/sys/kernel/msgmnb). The superuser can increase the size of a message queue beyond%uA0MSGMNB%uA0by a%uA0msgctl() system call. |