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 the O_NONBLOCK flag was set for the message queue description referred to by mqdes.
  • EBADF β€” the descriptor specified in mqdes was invalid, or not opened for reading.
  • EINTR β€” the call was interrupted by a signal handler; peep signal(7).
  • EINVAL β€” the call would have blocked, and abs_timeout was invalid, either because tv_sec was less than zero, or because tv_nsec was less than zero or greater than 1000 million. Yeah. 1000 million. That's the number. Don't ask πŸ™ˆ
  • EMSGSIZE β€” msg_len was less than the mq_msgsize attribute 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms