NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |VERSIONS |STANDARDS |HISTORY |SEE ALSO |COLOPHON | |
mq_receive(3) Library Functions Manualmq_receive(3)mq_receive, mq_timedreceive - receive a message from a message queue
Real-time library (librt,-lrt)
#include <mqueue.h>ssize_t mq_receive(size_t msg_len;mqd_tmqdes, charmsg_ptr[msg_len],size_tmsg_len, unsigned int *msg_prio);#include <time.h>#include <mqueue.h>ssize_t mq_timedreceive(size_t msg_len;mqd_tmqdes, char *restrictmsg_ptr[msg_len],size_tmsg_len, unsigned int *restrictmsg_prio,const struct timespec *restrictabs_timeout); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):mq_timedreceive(): _POSIX_C_SOURCE >= 200112L
mq_receive() removes the oldest message with the highest priority from the message queue referred to by the message queue descriptormqdes, and places it in the buffer pointed to bymsg_ptr. Themsg_len argument specifies the size of the buffer pointed to bymsg_ptr; this must be greater than or equal to themq_msgsize attribute of the queue (seemq_getattr(3)). Ifmsg_prio is not NULL, then the buffer to which it points is used to return the priority associated with the received message. If the queue is empty, then, by default,mq_receive() blocks until a message becomes available, or the call is interrupted by a signal handler. If theO_NONBLOCKflag is enabled for the message queue description, then the call instead fails immediately with the errorEAGAIN.mq_timedreceive() behaves just likemq_receive(), except that if the queue is empty and theO_NONBLOCKflag is not enabled for the message queue description, thenabs_timeout points to a structure which specifies how long the call will block. This value is an absolute timeout in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC), specified in atimespec(3) structure. If no message is available, and the timeout has already expired by the time of the call,mq_timedreceive() returns immediately.
On success,mq_receive() andmq_timedreceive() return the number of bytes in the received message; on error, -1 is returned, witherrno set to indicate the error.
EAGAINThe queue was empty, and theO_NONBLOCKflag was set for the message queue description referred to bymqdes.EBADFThe descriptor specified inmqdes was invalid or not opened for reading.EINTRThe call was interrupted by a signal handler; seesignal(7).EINVALThe call would have blocked, andabs_timeout was invalid, either becausetv_sec was less than zero, or becausetv_nsec was less than zero or greater than 1000 million.EMSGSIZEmsg_len was less than themq_msgsize attribute of the message queue.ETIMEDOUT The call timed out before a message could be transferred.
For an explanation of the terms used in this section, seeattributes(7). ┌──────────────────────────────────────┬───────────────┬─────────┐ │Interface│Attribute│Value│ ├──────────────────────────────────────┼───────────────┼─────────┤ │mq_receive(),mq_timedreceive() │ Thread safety │ MT-Safe │ └──────────────────────────────────────┴───────────────┴─────────┘
On Linux,mq_timedreceive() is a system call, andmq_receive() is a library function layered on top of that system call.
POSIX.1-2008.
POSIX.1-2001.
mq_close(3),mq_getattr(3),mq_notify(3),mq_open(3),mq_send(3),mq_unlink(3),timespec(3),mq_overview(7),time(7)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-06-28mq_receive(3)Pages that refer to this page:syscalls(2), mq_close(3), mq_getattr(3), mq_notify(3), mq_open(3), mq_send(3), mq_unlink(3), mq_overview(7), signal(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |