Message Queue Functions

Message queues provide a form of message passing in which any process (given that it has the necessary permissions) can read a message from or write a message to any IPC message queue on the system. There are no requirements that a process be waiting to receive a message from a queue before another process sends one, or that a message exist on the queue before a process requests to receive one.

Every message on a queue has the following attributes:

A thread gets a message queue identifier by calling the msgget() function. Depending on the key and msgflg parameters passed in, either a new message queue is created or an existing message queue is accessed. When a new message queue is created, a data structure is also created to contain information about the message queue. This structure is defined in the <sys/msg.h> header file as follows:

typedef struct msqid_ds {
    struct ipc_perm   msg_perm;    /* Operation permission struct */
    msgqnum_t         msg_qnum;    /* # msgs currently on queue   */
    msglen_t          msg_qbytes;  /* Max # bytes allowed on queue*/
    pid_t             msg_lspid;   /* Process ID of last msgsnd() */
    pid_t             msg_lrpid;   /* Process ID of last msgrcv() */
    time_t            msg_stime;   /* Time of last msgsnd()       */
    time_t            msg_rtime;   /* Time of last msgrcv()       */
    time_t            msg_ctime;   /* Time of last change         */
} msqid_ds_t;

A thread puts a message on a message queue by calling the msgsnd() function. The following parameters are passed in:

A thread gets a message from a message queue by calling the msgrcv() function. The following parameters are passed in:

A thread removes a message queue ID by calling the msgctl() function. The thread also can use the msgctl() function to change the data structure values associated with the message queue ID or to retrieve the data structure values associated with the message queue ID. The following parameters are passed in:


Message Queue Differences and Restrictions

i5/OS message queues differ from the message queue definition in the Single UNIX Specification in the following ways:


The message queue functions are:

See also IPC Key Generation Functions for additional message queue functions.



Top | UNIX-Type APIs | APIs by category