msgrcv()-Receive Message Operation



Syntax

#include <sys/msg.h>

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


Threadsafe: Yes

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

Parameters

msqid
(Input) Message queue identifier from which the message will be received.

msgp
(Output) Pointer to a buffer in which the received message will be stored. See the details below on the structure of the user-defined buffer.

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

msgtyp
(Input) Type of message to be received.

msgflg
(Input) Action to be taken if a message of the desired type is not on the queue, or if the data portion of the message to be received is larger than msgsz.

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

  1. A field of type long int that will specify the type of the message.

  2. A data part that will hold the data bytes of the message.

The following structure is an example of what this user-defined buffer might look like:

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

The structure member mtype is the type of the received message, as specified by the sending thread. The structure member mtext is the text of the message.

The parameter msgtyp specifies the type of message requested as follows:

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.

The following example shows pad data and how it affects the size of a message:


  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.   */

Authorities

Figure 1-6. 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 buffer mtext.
-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 mtext is greater than msgsz and (msgflg & MSG_NOERROR) is equal to zero. (& is a bitwise AND.)

[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.

If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.

The calling thread does not have read permission.

[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.

[ERROR_INVALID_PARAMETER]
An invalid parameter was found.

A parameter passed to this function is not valid.

One of the following has occurred:

[ENOMSG]
Message does not exist.

The queue does not contain a message of the desired type and (msgflg & IPC_NOWAIT) is not zero.

[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 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 not zero. The truncated part of the message is lost and no indication of the truncation is given to the calling thread.

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

  3. 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.

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

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

Related Information

Example

The following example receives a message from a message queue:


#include <sys/msg.h>

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

  msgsz = 256;
  msgtyp = 1;
  rc = msgrcv(msqid, &mymsg, msgsz, msgtyp, msgflg | IPC_NOWAIT);
}


Top | Interprocess Communication APIs | APIs by category


[Information Center Home Page | Feedback ] [Legal | AS/400 Glossary]