MQ_RECEIVE(3P)

πŸ’– MQ_RECEIVE(3P) πŸ’–

okay bestie, gather round, because today we're talking about mq_receive, mq_timedreceive, and the whole POSIX Programmer's Manual vibe. This one's from the POSIX Programmer's Manual, just so you know where you are 🫢


✨ PROLOG ✨

so like, this page is part of the POSIX Programmer's Manual, right? but heads up β€” the Linux implementation of this interface may be different, so if you're on Linux, go check the corresponding Linux manual page for the deets on how Linux actually does it. or honestly? the interface might not even be implemented on Linux at all. no cap. πŸ™ˆ


🌸 NAME 🌸

mq_receive, mq_timedreceive β€” receive a message from a message queue (REALTIME)

it's giving "get the message, girl" πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <mqueue.h>

ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
    unsigned *msg_prio);

#include <mqueue.h>
#include <time.h>

ssize_t mq_timedreceive(mqd_t mqdes, char *restrict msg_ptr,
    size_t msg_len, unsigned *restrict msg_prio,
    const struct timespec *restrict abstime);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so the way mq_receive() works is: it shall receive the oldest of the highest priority message(s) from the message queue that mqdes points at. like, the queue has opinions and its opinion is priority first, then age. iconic behavior tbh πŸ‘‘

now β€” if the size of the buffer in bytes, which is what msg_len is, is less than the mq_msgsize attribute of the message queue, the function shall fail and return an error. not negotiable. otherwise, the selected message shall be removed from the queue and copied into the buffer that msg_ptr is pointing at. bye bye message, hello buffer πŸ’Œ

if the value of msg_len is greater than {SSIZE_MAX}, the result is implementation-defined. so like... good luck, that's between you and your implementation 😭

if the argument msg_prio is not NULL, then the priority of the selected message shall be stored in the location that msg_prio references. cute little metadata moment.

now, the blocking saga: if the specified message queue is empty and O_NONBLOCK is not set in the message queue description associated with mqdes, then mq_receive() shall block until a message is enqueued on the message queue, or until mq_receive() gets interrupted by a signal. it will literally just sit there. patiently. like you waiting for a text back. 😭

and if more than one thread is waiting to receive a message when a message arrives at an empty queue, and the Priority Scheduling option is supported? then the thread of highest priority that has been waiting the longest shall be selected to receive the message. slay, seniority + clout. otherwise (no priority scheduling support), it is unspecified which waiting thread receives the message. it's giving chaos, fr fr πŸ’€

and if the specified message queue is empty and O_NONBLOCK is set in the message queue description associated with mqdes, then no message shall be removed from the queue, and mq_receive() shall return an error. no waiting, no message, just an error and vibes.


so mq_timedreceive() does the exact same thing β€” it shall receive the oldest of the highest priority messages from the message queue specified by mqdes, as described for mq_receive() above. BUT (and it's a big but) if O_NONBLOCK was not specified when the message queue was opened via the mq_open() function, and no message exists on the queue to satisfy the receive, then the wait for such a message shall be terminated when the specified timeout expires. it's the impatient version, and honestly? valid. ⏰

if O_NONBLOCK is set, this function is equivalent to mq_receive(). same girl, different wig.

the timeout expires when the absolute time specified by abstime passes, as measured by the clock on which timeouts are based β€” that is, when the value of that clock equals or exceeds abstime β€” or if the absolute time specified by abstime has already been passed at the time of the call. so if you're late, you're late, bestie. πŸ”₯

the timeout shall be based on the CLOCK_REALTIME clock. the resolution of the timeout shall be the resolution of the clock on which it's based. the timespec argument is defined in the <time.h> header. periodt.

and here's the iconic caveat: under no circumstance shall the operation fail with a timeout if a message can be removed from the message queue immediately. and the validity of the abstime parameter need not be checked if a message can be removed from the message queue immediately. if there's a message waiting, we're not doing timeout math, we're just grabbing it πŸ’…


🌈 RETURN VALUE 🌈

upon successful completion, the mq_receive() and mq_timedreceive() functions shall return the length of the selected message in bytes, and the message shall be removed from the queue. ate. πŸ“

otherwise? no message shall be removed from the queue, the functions shall return a value of -1, and set errno to indicate the error. sad girl hours. 😭


πŸ’œ ERRORS πŸ’œ

these functions shall fail if:

  • EAGAIN β€” O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty.
  • EBADF β€” the mqdes argument is not a valid message queue descriptor open for reading.
  • EMSGSIZE β€” the specified message buffer size, msg_len, is less than the message size attribute of the message queue.
  • EINTR β€” the mq_receive() or mq_timedreceive() operation was interrupted by a signal.
  • EINVAL β€” the process or thread would have blocked, and the abstime parameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million. (that's 1000 million, yes, that's a billion, yes, the manpage said 1000 million, and we respect the manpage πŸ’€)
  • ETIMEDOUT β€” the O_NONBLOCK flag was not set when the message queue was opened, but no message arrived on the queue before the specified timeout expired.

these functions may fail if:

  • EBADMSG β€” the implementation has detected a data corruption problem with the message. yikes. 🫣

the following sections are informative. (translation: they're the bonus content, not the rules ✨)


πŸ’… EXAMPLES πŸ’…

None.

it's giving "no examples, figure it out, you got this" 🫢


🧚 APPLICATION USAGE 🧚

None.


⭐ RATIONALE ⭐

None.


πŸ”₯ FUTURE DIRECTIONS πŸ”₯

None.


πŸ‘‘ SEE ALSO πŸ‘‘

mq_open(), mq_send(), msgctl(), msgget(), msgrcv(), msgsnd(), time()

and from the Base Definitions volume of POSIX.1‐2017: <mqueue.h>, <time.h>

go read those next, bestie, iykyk πŸ’–


okay this part is the legal bit and we keep it verbatim, no edits, no shortcuts:

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 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 .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, babes 🌸 this little icon came to you courtesy of IEEE/The Open Group, 2017 β€” mwah, thanks for the lore, bestie, now go receive those messages πŸ’Œβœ¨

πŸ“¦ where this page comes from