mq_send(3)
π mq_send β aka mq_send, mq_timedsend
okay bestie, gather round, because today we're learning how to slide a message into a message queue. yes, that's the whole job. it's giving β¨message deliveryβ¨ and honestly? iconic.
btw this page lives in the Library Functions Manual, section 3, and the vibe is Real-time library (librt, -lrt) β so if the linker starts acting up, that's the -lrt you're missing, no cap.
π«Ά LIBRARY
Real-time library (librt, -lrt)
β¨ SYNOPSIS β¨
#include <mqueue.h>
int mq_send(size_t msg_len;
mqd_t mqdes, const char msg_ptr[msg_len],
size_t msg_len, unsigned int msg_prio);
#include <time.h>
#include <mqueue.h>
int mq_timedsend(size_t msg_len;
mqd_t mqdes, const char msg_ptr[msg_len],
size_t msg_len, unsigned int msg_prio,
const struct timespec *abs_timeout);
πΈ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
mq_timedsend():_POSIX_C_SOURCE >= 200112L
like, yes, you literally have to define that macro or glibc will pretend it doesn't know her π
π DESCRIPTION π
So mq_send() takes the message that msg_ptr is pointing at and adds it to the message queue that the message queue descriptor mqdes refers to. That's it. That's the function. She's a deliverer. π
The msg_len argument says how long that message is β the one msg_ptr is pointing to. And listen: this length must be less than or equal to the queue's mq_msgsize attribute. Rule is the rule. Oh, and zero-length messages? allowed. send an empty little nothing, she's valid. π¦
msg_prio is a nonnegative integer and it's the priority of this message. Messages go on the queue in decreasing order of priority β so the high-priority ones skip the line like they own the place β and newer messages with the same priority get placed after the older messages with that same priority. First come, first served between equals, basically. If you wanna know the actual range for message priority, go check mq_overview(7), she has the receipts.
Now. If the message queue is already full β meaning the number of messages on the queue equals the queue's mq_maxmsg attribute β then by default mq_send() blocks. It just stands there, waiting, until enough space frees up so the message can be queued, or until a signal handler interrupts the call. π But if the O_NONBLOCK flag is enabled for that message queue description, the call doesn't wait at all β it fails immediately with the error EAGAIN. Boom. Not sorry. π
mq_timedsend() behaves just like mq_send(), except for one lil upgrade: if the queue is full and the O_NONBLOCK flag is not enabled for the message queue description, then abs_timeout points to a structure that says how long the call will block. That value is an absolute timeout in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC), specified in a timespec(3) structure. Absolute, bestie, not "from now" β absolute. don't get caught slipping. π
And if the message queue is full and the timeout has already expired by the time you make the call, mq_timedsend() returns immediately. No waiting, no drama, just instant feedback. π
π«§ RETURN VALUE π«§
On success, mq_send() and mq_timedsend() return zero. π On error, -1 comes back, with errno set to tell you what went wrong. The errno is basically the "and here's why" text message. π
π ERRORS π
EAGAINβ the queue was full, and theO_NONBLOCKflag was set for the message queue description referred to by mqdes.EBADFβ the descriptor specified in mqdes was invalid, or it wasn't opened for writing. (write permissions, bestie, they matter π)EINTRβ the call got interrupted by a signal handler; seesignal(7).EINVALβ the call would have blocked, and abs_timeout was invalid β either becausetv_secwas less than zero, or becausetv_nsecwas less than zero or greater than 1000 million.EMSGSIZEβ msg_len was greater than themq_msgsizeattribute of the message queue. Too much. Overflowing. Not the vibe. πETIMEDOUTβ the call timed out before a message could be transferred.
π ATTRIBUTES π
For an explanation of the terms used in this section, see attributes(7).
mq_send(),mq_timedsend()β Thread safety:MT-Safeβ
MT-Safe means she's thread-safe and she knows it. main character energy, fr fr.
β VERSIONS β
On Linux, mq_timedsend() is a system call, and mq_send() is a library function layered on top of that system call. So like, mq_send() is wearing mq_timedsend()'s outfit. slay. π
π STANDARDS π
POSIX.1-2008.
π HISTORY π
POSIX.1-2001.
πΈ SEE ALSO πΈ
mq_close(3), mq_getattr(3), mq_notify(3), mq_open(3), mq_receive(3), mq_unlink(3), timespec(3), mq_overview(7), time(7)
and that's the whole page, bestie π«Ά go send some messages, and remember: absolute timeout, not relative, iykyk.
this was brought to you by Linux man-pages 6.20, dated 2026-02-08 β the mq_send(3) era, and we're obsessed with her. bye!! πβ¨ππ¦
π¦ 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