msgrcv()--Receive Message Operation


  Syntax
 #include <sys/msg.h>

 int msgrcv(int msqid, void *msgp, size_t msgsz, 
           long int msgtyp, int msgflg); 

  Service Program Name: QP0ZUSHR

  Default Public Authority: *USE

  Threadsafe: Yes

The msgrcv() function reads a message from the message queue specified by the msqid parameter and places it in the user-defined buffer pointed to by the *msgp parameter.

The *msgp parameter points to a user-defined buffer that must contain the following:

  1. A field of type long int that specifies the type of the message.
  2. A data part that contains the data bytes of the message.

The following structure is an example of what this user-defined buffer might look like for a message that has 5 bytes of data:

  struct mymsg {
         long int    mtype;      /* message type */
         char        mtext[5];   /* message text */
  }

The value of mtype is the type of the received message, as specified by the sender of the message.

The msgtyp parameter specifies the type of message to receive from the message queue as follows:

The msgsz parameter specifies the size in bytes of the data part of the message. The received message is truncated to msgsz bytes if it is larger than msgsz and the MSG_NOERROR flag is set in the msgflg parameter. The truncated part of the message is lost and no indication of the truncation is given to the calling thread. Otherwise, msgrcv() returns with return value -1 and errno set to E2BIG.

If a message of the desired type is not available on the message queue, the msgflg parameter specifies the action to be taken. The actions are as follows:


The system maintains status information about a message queue which can be retrieved with the msgctl() function. When a message is successfully received from the message queue, the system sets the members of the msqid_ds structure as follows:


Parameters

msqid
(Input) Message queue identifier, a positive integer. It is returned by the msgget() function and used to identify the message queue to receive the message from.

msgp
(Output) Pointer to a buffer in which the received message will be stored. See above for the details on the format of the buffer.

msgsz
(Input) Length of the data part of the buffer.

msgtyp
(Input) Type of message to be received.

msgflg
(Input) Operations flags. The value of msgflg is either zero or is obtained by performing an OR operation on one or more of the following constants:
IPC_NOWAIT (0x00000800)
If a message is not available, do not wait for the message and return immediately.
MSG_NOERROR (0x00001000)
If the data part of the message is larger than msgsz, do not return an error.


Authorities

Authorization Required for msgrcv()

Object Referred to Authority Required errno
Message queue from which message is received Read EACCES


Return Value

value msgrcv() was successful. The value returned is the number of bytes of data placed in the data part of the buffer pointed to by the msgp parameter.
-1 msgrcv() was not successful. The errno variable is set to indicate the error.


Error Conditions

If msgrcv() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

[E2BIG]

Argument list too long.

The size in bytes of the message is greater than msgsz and the MSG_NOERROR flag is not set in the msgflg parameter.

[EACCES]

Permission denied.

An attempt was made to access an object in a way forbidden by its object access permissions.

The thread does not have access to the specified file, directory, component, or path.

The calling thread does not have read permission to the message queue.

[EDAMAGE]

A damaged object was encountered.

A referenced object is damaged. The object cannot be used.

The message queue has been damaged by a previous message queue operation.

[EFAULT]

The address used for an argument is not correct.

In attempting to use an argument in a call, the system detected an address that is not valid.

While attempting to access a parameter passed to this function, the system detected an address that is not valid.

[EIDRM]

ID has been removed.

The message queue identifier msqid was removed from the system.

[EINTR]

Interrupted function call.

The function msgrcv() was interrupted by a signal.

[EINVAL]

The value specified for the argument is not correct.

A function was passed incorrect argument values, or an operation was attempted on an object and the operation specified is not supported for that type of object.

An argument value is not valid, out of range, or NULL.

One of the following has occurred:

[ENOMSG]

Message does not exist.

The message queue does not contain a message of the desired type and the IPC_NOWAIT flag is set in the msgflg parameter.

[EUNKNOWN]

Unknown system state.

The operation failed because of an unknown system state. See any messages in the job log and correct any errors that are indicated, then retry the operation.


Error Messages

None.


Usage Notes

  1. The parameter msgsz should include any bytes inserted by the compiler for padding or alignment purposes. These bytes are part of the message data and affect the total number of bytes in the message queue.
  2. The following example shows pad data and how it affects the size of a message using datamodel *P128:
  3.  struct mymsg {
         long int    mtype;      /* 12 bytes padding inserted after */
         char       *pointer;    /* the mtype field by the compiler.*/
         char        c;          /* 15 bytes padding inserted after */
         char       *pointer2;   /* the c field by the compiler.    */
      } msg;                     /* After the mtype field, there are*/
                                 /* 33 bytes of user data, but 60   */
                                 /* bytes of data including padding.*/
      msgsz = sizeof(msg) - sizeof(long int);        /* 60 bytes.   */
    
    
  4. The msgrcv() function does not tag message data with a CCSID (coded character set identifier) value. If a CCSID value is required to correctly interpret the message data, it is the responsibility of the caller to include the CCSID value as part of the data.

  5. If the msgrcv() function does not complete successfully, the requested message is not removed from the message queue.

Related Information


Example

See Code disclaimer information for information pertaining to code examples.

The following example receives a message from a message queue:

#include <sys/msg.h>

main() {
  int msqid = 2;
  int rc;
  size_t msgsz;
  long int msgtyp;
  struct mymsg {
      long int mtype;
      char     mtext[256];
  };

  msgsz = sizeof(struct mymsg) - sizeof(long int);
  msgtyp = 1;
  rc = msgrcv(msqid, &mymsg, msgsz, msgtyp, IPC_NOWAIT);
}


API introduced: V3R6
Top | UNIX-Type APIs | APIs by category