[Previous] [Contents] [Next]

mq_receive()

Receive a message from a queue

Synopsis:

#include <mqueue.h>

ssize_t mq_receive( mqd_t mqdes, 
                    char* msg_ptr, 
                    size_t msg_len,
                    unsigned int* msg_prio );

Library:

libc

Description:

The mq_receive() function is used to receive the oldest of the highest priority messages in the queue specified by mqdes. The priority of the message received is put in the location pointed to by msg_prio, the data itself in the location pointed to by msg_ptr, and the size received is be returned.

If mq_receive() is called with a msg_len of anything other than the mq_msgsize of the specified queue, then mq_receive() returns an error, and errno is set to EINVAL.

If there are no messages on the queue specified, and O_NONBLOCK wasn't set in oflag during mq_open(), and MQ_NONBLOCK isn't present in the queue's mq_flags, then the mq_receive() call blocks. If multiple mq_receive() calls are blocked on a single queue, then they're unblocked in FIFO order as messages arrive.

If msg_prio isn't NULL, then the priority of the message removed from the queue are stored in the location referenced by msg_prio.

Calling read() with mqdes is analogous to calling mq_receive() with a NULL msg_prio.

Returns:

The size of the message removed from the queue. If the call fails, -1 is returned as the size, no message is removed from the queue, and errno is set.

Errors:

EAGAIN
The O_NONBLOCK or MQ_NONBLOCK flags were set and there are no messages currently on the specified queue.
EBADF
The mqdes argument doesn't represent a valid queue open for reading.
EINTR
The operation was interrupted by a signal.
EINVAL
The msg_ptr argument isn't a valid pointer, or msg_len is less than 0, or msg_len is less than the message size specified in mq_open(). The default message size is 4096 bytes.
EMSGSIZE
The given msg_len is shorter than the mq_msgsize for the given queue or the given msg_len is too short for the message that would have been received.

Classification:

POSIX 1003.1 (Realtime Extensions)

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes

See also:

mq_close(), mq_open(), mq_send()


[Previous] [Contents] [Next]