Name

msgsnd - message operations

Library

libc.lib

Synopsis

  #include <sys/types.h>
  #include <sys/ipc.h>
  #include <sys/msg.h>
  int msgsnd (int msqid, const void *msgp, size_t msgsz, int msgflg);

Return values

The msgsnd function returns the value 0 if successful; otherwise the value -1 is returned and errno is set to indicate the error.

Detailed description

The msgsnd function sends a message to the message queue specified in msqid. The msgp argument points to a structure containing the message. This structure should consist of the following members:
    long mtype;    /* message type */
    char mtext[1]; /* body of message */

         

mtype is an integer greater than 0 that can be used for selecting messages (see msgrcv mtext is an array of bytes, with a size up to that of the system limit (Dv MSGMAX).

If the number of bytes already on the message queue plus msgsz is bigger than the maximum number of bytes on the message queue (Va msg_qbytes, see msgctl or the number of messages on all queues system-wide is already equal to the system limit, msgflg determines the action of msgsnd. If msgflg has IPC_NOWAIT mask set in it, the call will return immediately. If msgflg does not have IPC_NOWAIT set in it, the call will block until:

After a successful call, the data structure associated with the message queue is updated in the following way:


Examples

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

         
#define MESSAGE_Q_KEY 1000

         
int main(void)
{
   int msq_id, len;
   struct {
       long mtype;
       char mtext[128];
   } msg_buf;
   /*
    * Create a message queue with the given key
    */
    if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
       printf("Message Q create failed with errno %d\n", errno);
       return -1;
    }
    msg_buf.mtype = 1; /* message identifier */
    strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
    len = strlen(msg_buf.mtext)+1;
    /*
     * Put the message in the queue
     */
    if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, 0) == -1) {
        printf("Message Q send failed with errno %d\n", errno);
    }
    return 0;
}

         


Errors

The msgsnd function will fail if:
[EINVAL]
  The msqid argument is not a valid message queue identifier

The message queue was removed while msgsnd was waiting for a resource to become available in order to deliver the message.

The msgsz argument is less than 0, or greater than msg_qbytes.

The mtype argument is not greater than 0.

[EACCES]
  The calling process does not have write access to the message queue.
[EAGAIN]
  There was no space for this message either on the queue, or in the whole system, and IPC_NOWAIT was set in msgflg.
[EIDRM]
  The message queue identifier msqid is removed from the system.

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top