MSGRCV(3P)
π hey bestie, welcome to msgrcv(3P) π
so this is the msgrcv page from the POSIX Programmer's Manual, and it's all about the XSI message receive operation. yes we're doing IPC today. put your hair up, we're going in. π
β¨ PROLOG β¨
okay so before literally anything else: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so pls consult the corresponding Linux manual page for details of Linux behavior, bestie, don't come for me), or β plot twist β the interface may not be implemented on Linux at all. π
πΈ NAME πΈ
msgrcv β XSI message receive operation π
π¦ SYNOPSIS π¦
#include <sys/msg.h>
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
int msgflg);
that's the whole vibe. five arguments, one function, infinite potential. β¨
π DESCRIPTION π
so msgrcv() operates on XSI message queues (peep the Base Definitions volume of POSIX.1β2017, Section 3.226, Message Queue for the lore). and lowkey important: it is unspecified whether this function interoperates with the realtime interprocess communication facilities defined in Section 2.8, Realtime. so don't assume, okay?
the msgrcv() function shall read a message from the queue associated with the message queue identifier specified by msqid and place it in the user-defined buffer pointed to by msgp. π«Ά
the application shall ensure that the argument msgp points to a user-defined buffer that contains first a field of type long specifying the type of the message, and then a data portion that holds the data bytes of the message. the structure below is an example of what this user-defined buffer might look like:
struct mymsg {
long mtype; /* Message type. */
char mtext[1]; /* Message text. */
}
the structure member mtype is the received message's type as specified by the sending process. π·οΈ
the structure member mtext is the text of the message. π¬
the argument msgsz specifies the size in bytes of mtext. and here's the spicy part: the received message shall be truncated to msgsz bytes if it's larger than msgsz and (msgflg & MSG_NOERROR) is non-zero. the truncated part of the message shall be lost and no indication of the truncation shall be given to the calling process. no cap, it just vanishes. π
if the value of msgsz is greater than {SSIZE_MAX}, the result is implementation-defined. iykyk.
the argument msgtyp specifies the type of message requested, and it's giving β¨choose your own adventureβ¨:
- if
msgtypis0, the first message on the queue shall be received. - if
msgtypis greater than0, the first message of typemsgtypshall be received. - if
msgtypis less than0, the first message of the lowest type that is less than or equal to the absolute value ofmsgtypshall be received.
the argument msgflg specifies the action to be taken if a message of the desired type is <em>not</em> on the queue. these are as follows:
- if
(msgflg & IPC_NOWAIT)is non-zero, the calling thread shall return immediately with a return value of-1anderrnoset to[ENOMSG]. bye, no waiting, we're busy. π - if
(msgflg & IPC_NOWAIT)is0, the calling thread shall suspend execution until one of the following occurs:- a message of the desired type is placed on the queue. π
- the message queue identifier
msqidis removed from the system; when this occurs,errnoshall be set to[EIDRM]and-1shall be returned. π - the calling thread receives a signal that is to be caught; in this case a message is not received and the calling thread resumes execution in the manner prescribed in
sigaction(). π¨
upon successful completion, the following actions are taken with respect to the data structure associated with msqid:
msg_qnumshall be decremented by1.msg_lrpidshall be set to the process ID of the calling process.msg_rtimeshall be set to the current time, as described in Section 2.7.1, IPC General Description.
bookkeeping stays booked. πβ¨
π RETURN VALUE π
upon successful completion, msgrcv() shall return a value equal to the number of bytes actually placed into the buffer mtext. π₯³
otherwise? no message shall be received, msgrcv() shall return -1, and errno shall be set to indicate the error. main character exit, honestly. π
π₯ ERRORS π₯
the msgrcv() function shall fail if:
E2BIGβ the value ofmtextis greater thanmsgszand(msgflg & MSG_NOERROR)is0. too much text, not enough room, we can't.EACCESβ operation permission is denied to the calling process; see Section 2.7, XSI Interprocess Communication. not your queue, not your message, bestie. πEIDRMβ the message queue identifiermsqidis removed from the system. it's gone. it's giving ghosted. π»EINTRβ themsgrcv()function was interrupted by a signal.EINVALβmsqidis not a valid message queue identifier.ENOMSGβ the queue does not contain a message of the desired type and(msgflg & IPC_NOWAIT)is non-zero.
the following sections are informative. so like, bonus content. π
π EXAMPLES π
Receiving a Message
the following example receives the first message on the queue (based on the value of the msgtyp argument, 0). the queue is identified by the msqid argument (assuming that the value has previously been set). this call specifies that an error should be reported if no message is available, but not if the message is too large. the message size is calculated directly using the sizeof operator. iconic. π
#include <sys/msg.h>
...
int result;
int msqid;
struct message {
long type;
char text[20];
} msg;
long msgtyp = 0;
...
result = msgrcv(msqid, (void *) &msg, sizeof(msg.text),
msgtyp, MSG_NOERROR | IPC_NOWAIT);
π§ APPLICATION USAGE π§
the POSIX Realtime Extension defines alternative interfaces for interprocess communication (IPC). application developers who need to use IPC should design their applications so that modules using the IPC routines described in Section 2.7, XSI Interprocess Communication can be easily modified to use the alternative interfaces. future-proofing is self-care, fr fr. π
π RATIONALE π
none.
(truly nothing. the committee said no thoughts, head empty. πβ¨)
β FUTURE DIRECTIONS β
none.
π«Ά SEE ALSO π«Ά
Section 2.7, XSI Interprocess Communication, Section 2.8, Realtime, mq_close(), mq_getattr(), mq_notify(), mq_open(), mq_receive(), mq_send(), mq_setattr(), mq_unlink(), msgctl(), msgget(), msgsnd(), sigaction()
the Base Definitions volume of POSIX.1β2017, Section 3.226, Message Queue, <sys_msg.h>
π COPYRIGHT π
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, bestie!! this one came to you courtesy of IEEE/The Open Group, dated 2017. mwah, take care of your message queues and they'll take care of you. πβ¨π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms