mq_receive(3)
π mq_receive β receive a message from a message queue π
okay bestie, sit down, hydrate, because today we're learning about mq_receive and mq_timedreceive from the Linux man pages, section 3 (that's "Library Functions Manual" energy, iykyk πβ¨). This is the glow-up of getting a message OUT of a message queue. Let's go π«Ά
π NAME
mq_receive, mq_timedreceive β receive a message from a message queue π
π LIBRARY
Real-time library (librt, -lrt) β yes you have to link it, no it's not optional, don't come for me πΈ
β¨ SYNOPSIS
#include <mqueue.h>
ssize_t mq_receive(size_t msg_len;
mqd_t mqdes, char msg_ptr[msg_len],
size_t msg_len, unsigned int *msg_prio);
#include <time.h>
#include <mqueue.h>
ssize_t mq_timedreceive(size_t msg_len;
mqd_t mqdes, char *restrict msg_ptr[msg_len],
size_t msg_len, unsigned int *restrict msg_prio,
const struct timespec *restrict abs_timeout);
Feature Test Macro Requirements for glibc (peep feature_test_macros(7) for the lore π):
mq_timedreceive():_POSIX_C_SOURCE >= 200112L
π DESCRIPTION
So mq_receive() does the main character thing: it removes the oldest message with the highest priority from the message queue that the descriptor mqdes is pointing at, and drops it into the buffer that msg_ptr points to. Iconic behavior honestly π
The msg_len argument is literally just the size of the buffer at msg_ptr. And bestie, it must be greater than or equal to the mq_msgsize attribute of the queue (go check mq_getattr(3) for that one, she'll tell you) π§
If msg_prio is not NULL, then the buffer it points to gets used to return the priority that came with the received message. Cute little bonus value π
Now β if the queue is empty? By default mq_receive() just blocks until a message shows up, or until the call gets interrupted by a signal handler. It's giving waiting-by-the-phone energy π But if the O_NONBLOCK flag is enabled for the message queue description, then instead the call fails immediately with the error EAGAIN. No waiting, no patience, periodt π
mq_timedreceive() behaves exactly like mq_receive(), except that if the queue is empty AND the O_NONBLOCK flag is not enabled for the message queue description, then abs_timeout points to a struct that says how long the call will block. And the value is an absolute timeout β seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) β specified in a timespec(3) structure. Absolute, not relative, don't get it twisted β°β¨
And if no message is available, and the timeout has already expired by the time of the call? mq_timedreceive() returns immediately. It's not gonna sit around for a deadline that already passed, she has places to be π
π₯ RETURN VALUE
On success, mq_receive() and mq_timedreceive() return the number of bytes in the received message π On error, -1 is returned, with errno set to indicate the error. Classic.
πΈ ERRORS
EAGAINβ the queue was empty, and theO_NONBLOCKflag was set for the message queue description referred to bymqdes.EBADFβ the descriptor specified inmqdeswas invalid, or not opened for reading.EINTRβ the call was interrupted by a signal handler; peepsignal(7).EINVALβ the call would have blocked, andabs_timeoutwas invalid, either becausetv_secwas less than zero, or becausetv_nsecwas less than zero or greater than 1000 million. Yeah. 1000 million. That's the number. Don't ask πEMSGSIZEβmsg_lenwas less than themq_msgsizeattribute of the message queue.ETIMEDOUTβ the call timed out before a message could be transferred. She waited, the vibe expired, it's over π
π ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
- Interface:
mq_receive(),mq_timedreceive() - Attribute: Thread safety
- Value:
MT-Safe
(Yes they're thread safe. Slay. Multithreaded besties stay winning π)
π¦ VERSIONS
On Linux, mq_timedreceive() is a system call, and mq_receive() is a library function layered on top of that system call. So one's the real one and one's wearing a cute little coat. Both valid π
β STANDARDS
POSIX.1-2008.
π HISTORY
POSIX.1-2001.
π«Ά SEE ALSO
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)
and that's the whole thing bestie, you ate that π this was brought to you by Linux man-pages 6.20, dated 2026-02-08 β page mq_receive(3), out πβ¨π¦ no cap, go read the whole queue now, ily π«Ά
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms