Sends a message.
Standard C Library (libc.a)
#include <sys/msg.h>
int msgsnd (MessageQueueID, MessagePointer,MessageSize, MessageFlag)
int MessageQueueID, MessageFlag;
const void * MessagePointer;
size_t MessageSize;
The msgsnd subroutine sends a message to the queue specified by the MessageQueueID parameter. The current process must have write permission to perform this operation. The MessagePointer parameter points to an msgbuf structure containing the message. The sys/msg.h file defines the msgbuf structure. The structure contains the following fields:
mtyp_t mtype; /* Message type */
char mtext[1]; /* Beginning of message text */
The mtype field specifies a positive integer used by the receiving process for message selection. The mtext field can be any text of the length in bytes specified by the MessageSize parameter. The MessageSize parameter can range from 0 to the maximum limit imposed by the system.
The following example shows a typical user-defined msgbuf structure that includes sufficient space for the largest message:
struct my_msgbuf
mtyp_t mtype;
char mtext[MSGSIZ]; /* MSGSIZ is the size of the largest message */
The following system limits apply to the message queue:
The MessageFlag parameter specifies the action to be taken if the message cannot be sent for one of the following reasons:
These actions are as follows:
Item | Description |
---|---|
MessageQueueID | Specifies the queue to which the message is sent. |
MessagePointer | Points to a msgbuf structure containing the message. |
MessageSize | Specifies the length, in bytes, of the message text. |
MessageFlag | Specifies the action to be taken if the message cannot be sent. |
Upon successful completion, a value of 0 is returned and the following actions are taken with respect to the data structure associated with the MessageQueueID parameter:
If the msgsnd subroutine is unsuccessful, a value of -1 is returned and the errno global variable is set to indicate the error.
The msgsnd subroutine is unsuccessful and no message is sent if one or more of the following conditions is true:
Item | Description |
---|---|
EACCES | The calling process is denied permission for the specified operation. |
EAGAIN | The message cannot be sent for one of the reasons stated previously, and the MessageFlag parameter is set to the IPC_NOWAIT value or the system has temporarily ran out of memory resource. |
EFAULT | The MessagePointer parameter points outside of the address space of the process. |
EIDRM | The message queue identifier specified by the MessageQueueID parameter has been removed from the system. |
EINTR | The msgsnd subroutine received a signal. |
EINVAL | The MessageQueueID parameter is not a valid message queue identifier. |
EINVAL | The mtype field is less than 1. |
EINVAL | The MessageSize parameter is less than 0 or greater than the system-imposed limit. |
EINVAL | The upper 32-bits of the 64-bit mtype field for a 64-bit process is not 0. |
ENOMEM | The message could not be sent because not enough storage space was available. |