[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Base Operating System and Extensions, Volume 1

msgsnd Subroutine


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 routine may coredump instead of returning EFAULT when an invalid pointer is passed in case of 64-bit application calling 32-bit kernel interface.

The following system limits apply to the message queue:

For a 64-bit process, the mtype field is 64 bits long. However, for compatibility with 32-bit processes, the mtype field must be a 32-bit signed value that is sign-extended to 64 bits. The most significant 32 bits are not put on the message queue. For a 64-bit process, the mtype field is again sign-extended to 64 bits.

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:


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.

Return Values

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.

Error Codes

The msgsnd subroutine is unsuccessful and no message is sent if one or more of the following conditions is true:

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.

Related Information

The msgctl (msgctl Subroutine) subroutine, msgget (msgget Subroutine) subroutine, msgrcv (msgrcv Subroutine) subroutine, msgxrcv (msgxrcv Subroutine) subroutine, sigaction subroutine.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]