MSGOP(3L)

NAME
msgop - message operations

SYNOPSIS
#include <message.h> int msgsnd (msqid, msgp, msgsz, msgflg)
long msqid;
struct msgbuf *msgp;
int msgsz, msgflg;

int msgrcv (msqid, msgp, msgsz, msgtyp, msgflg)
long msqid;
struct msgbuf [*]*msgp;
int msgsz;
long msgtyp;
int msgflg;

DESCRIPTION
Msgsnd is used to send a message to the queue associated with the message queue identifier specified by msqid. Msgp points to a structure containing the message. This structure is composed of the following members:

    long mtype; /* message type */
    char mtext[]; /* message text */

Mtype is a positive integer that can be used by the receiving process for message selection (see msgrcv below). Mtext is any text of length msgsz bytes. Msgsz can range from 0 to what memory will allow.

Msgflg specifies the action to be taken if one or more of the following are true:

    The number of bytes already on the queue is equal to msg_qbytes.

    There is not enough memory to put the message on the queue.

These actions are as follows:

    If (msgflg & IPC_NOWAIT) is ``true'', the message will not be sent and the calling process will return immediately.

    If (msgflg & IPC_NOWAIT) is ``false'', the calling process will suspend execution until one of the following occurs:

        The condition responsible for the suspension no longer exists, in which case the message is sent.

        Msqid is removed from the system (see msgctl).

Msgsnd will fail and no message will be sent if one or more of the following are true:

    Msqid is not a valid message queue identifier.

    Mtype is less than 1.

    The message cannot be sent for one of the reasons cited above and (msgflg & IPC_NOWAIT) is ``true''.

    Msgsz is less than zero.

Upon successful completion, the following actions are taken with respect to the data structure associated with msqid.

    Msg_qnum is incremented by 1.

    Msg_lspid is set equal to the process id of the cal ling process.

    Msg_stime is set equal to the current time.

An extra feature of msgsnd lets the user send messages without a copy being made. If (msgflg & NO_COPY) is ``true'', the message pointed to by msgp is put directly into the message queue. This can only be done if the msgbuf was allocated (created by a call to alloc). This is because the ownership of that memory must be changed. The sending process then no longer owns that memory and should not try to access it or free it.

Msgrcv reads a message from the queue associated with the message queue identifier specified by msqid and places it in the structure pointed to by msgp. This structure is composed of the following members:

    long mtype; /* message type */
    char mtext[]; /* message text */

Mtype is the received message's type as specified by the sending process. Mtext is the text of the message. Msgsz specifies the size in bytes of mtext. The received message is truncated to msgsz bytes if it is larger than msgsz and (msgflg & MSG_NOERROR) is ``true''. The truncated part of the message is lost and no indication of the truncation is given to the calling process.

Msgtyp specifies the type of message requested as follows:

    If msgtyp is equal to 0, the first message on the queue is received.

    If msgtyp is greater than 0, the first message of type msgtyp is received.

    If msgtyp is less than 0, the first message of the lowest type that is less than or equal to the absolute value of msgtyp is received.

Msgflg specifies the action to be taken if a message of the desired type is not on the queue. These are as follows:

    If (msgflg & IPC_NOWAIT) is ``true'', the calling process will return immediately with a return value of -1.

    If (msgflg & IPC_NOWAIT) is ``false'', the calling process will suspend execution until one of the following occurs:

        A message of the desired type is placed on the queue.

        Msqid is removed from the system. When this occurs, a value of -1 is returned.

Msgrcv will fail and no message will be received if one or more of the following are true:

    Msqid is not a valid message queue identifier.

    Msgsz is less than 0.

    Mtext is greater than msgsz and (msgflg & MSG_NOERROR) is ``false''.

    The queue does not contain a message of the desired type and (msgtyp & IPC_NOWAIT) is ``true''.

Upon successful completion, the following actions are taken with respect to the data structure associated with msqid.

    Msg_qnum is decremented by 1.

    Msg_lrpid is set equal to the process id of the calling process.

    Msg_rtime is set equal to the current time.

Again, the option of not copying the message is available. If (msgflg & NO_COPY) is ``true'', msgp is treated as a pointer to a pointer to a message. In other words, *msgp is set to the address of the received message. The ownership of the message is then set to the receiving process. The receiving process can then read the message and free it.

A side effect of msgrcv is that it clears the MSG resource ready condition used by the wait and own resource functions.

  Return Values
Upon successful completion, the return value is as follows:

    Msgsnd returns a value of 0.

    Msgrcv returns a value equal to the number of bytes actually placed into mtext.

Otherwise, a value of -1 is returned.

SEE ALSO
alloc(3R), msgctl(3L), msgget(3L), realtime(3R), resources(3R), structures(3R).