MQ_SEND(2)
π MQ_SEND(2) β bestie, it's the System Calls Manual π
okay so. we're in the System Calls Manual (section 2, the syscall girlies) and today's episode is all about yeeting a message into a queue. real-time flavored. let's GO π
β¨ NAME β¨
mq_send, mq_timedsend β send a message to message queue (REALTIME) π¦
π LIBRARY π
POSIX Real-time Library (librt, -lrt) π
π SYNOPSIS π
#include <mqueue.h>
int
mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned msg_prio);
int
mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned msg_prio, const struct timespec *abs_timeout);
πΈ DESCRIPTION πΈ
so mq_send() is a system call that takes the message msg_ptr is pointing at and drops it into the message queue that mqdes is referring to. iconic behavior. β¨
the msg_len argument says how long that message is β in bytes β the one msg_ptr points at. and bestie, listen: the value of msg_len should be less than or equal to the mq_msgsize attribute of the message queue, or mq_send() will fail. no cap. π
if the message queue you named is not full, mq_send() behaves as if the message gets inserted into the queue at the position indicated by the msg_prio argument. a message with a larger numeric value of msg_prio gets inserted before messages with lower values of msg_prio. and a message gets inserted after other messages in the queue (if there are any) that have an equal msg_prio. the value of msg_prio should be less than {MQ_PRIO_MAX}. priorities are giving main character energy, honestly π
now the spicy part. if the message queue is full and O_NONBLOCK is not set in the message queue description associated with mqdes, then mq_send() will block until space becomes available to enqueue the message, or until mq_send() gets interrupted by a signal. π
and if more than one thread is waiting to send when space opens up in the message queue, and the Priority Scheduling option is supported, then the thread of the highest priority that has been waiting the longest gets unblocked to send its message. otherwise? it is unspecified which waiting thread gets unblocked. π€·βοΈ (unspecified = we literally cannot promise you anything, bestie, don't @ me)
if the message queue is full and O_NONBLOCK is set in the message queue description associated with mqdes, then the message will not be queued and mq_send() will return an error. periodt. π«
π and nowβ¦ mq_timedsend() π
mq_timedsend() adds a message to the message queue specified by mqdes in exactly the manner defined for the mq_send() system call. same vibe, different clock. β¨
however β and this is the whole point β if the specified message queue is full and O_NONBLOCK is not set in the message queue description associated with mqdes, the wait for sufficient room in the queue gets terminated when the specified timeout expires. β°
and if O_NONBLOCK is set in the message queue description, this system call is equivalent to mq_send(). so basically the timeout doesn't even matter there. π«Ά
the timeout will expire when the absolute time specified by abs_timeout passes β as measured by the clock on which timeouts are based (that is, when the value of that clock equals or exceeds abs_timeout) β or if the absolute time specified by abs_timeout has already been passed at the time of the call. so if you're late, you're late. π
π heads up: the timeout is based on theCLOCK_REALTIMEclock. not monotonic, not whatever else.CLOCK_REALTIME. π
π«Ά RETURN VALUES π«Ά
upon successful completion, mq_send() and mq_timedsend() return a value of zero. slay. β¨
otherwise β and this is the sad part β no message will be enqueued, the system calls return -1, and the global variable errno is set to indicate the error. π
π ERRORS π
the mq_send() and mq_timedsend() system calls will fail if:
[EAGAIN]β theO_NONBLOCKflag is set in the message queue description associated withmqdes, and the specified message queue is full. π€[EBADF]β themqdesargument is not a valid message queue descriptor open for writing. bestie that's not even a real queue π[EINTR]β a signal interrupted the call tomq_send()ormq_timedsend(). rude of it honestly π[EINVAL]β the value ofmsg_priowas outside the valid range.[EINVAL]β the process or thread would have blocked, and theabs_timeoutparameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million. that's not a nanosecond field, that's a cry for help π«[EMSGSIZE]β the specified message length,msg_len, exceeds the message size attribute of the message queue. too much. TOO MUCH. π[ETIMEDOUT]β theO_NONBLOCKflag was not set when the message queue was opened, but the timeout expired before the message could be added to the queue. clock said no β°π«
π SEE ALSO π
mq_open(2), mq_receive(2), mq_setattr(2), mq_timedreceive(2)
go read those too, they're the rest of the friend group π«Ά
π STANDARDS π
the mq_send() and mq_timedsend() system calls conform to IEEE Std 1003.1-2004 (βPOSIX.1β). certified. π
π¦ HISTORY π¦
support for POSIX message queues first appeared in FreeBSD 7.0. vintage!! β¨
βοΈ COPYRIGHT βοΈ
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
okay that's the whole page bestie, you're officially a message-queue girlie now π sending u good vibes and zero EAGAINs π
this cutie was brought to you by Debian, dated November 29, 2005 β yeah she's an older icon but she ATE and she's still serving π π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2005 David Xu <davidxu@FreeBSD.org>
- Copyright (C) 2001-2004 by the Institute of
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms