MSGRCV(2)
π msgrcv β bestie, come get your message π
okay so first things first: this page is MSGRCV(2), and it lives in the System Calls Manual. Yes, that one. The one with all the SysV IPC lore. Sit down, get comfy, we're learning how to pull a message out of a message queue. π«Ά
β¨ NAME β¨
msgrcv β receive a message from a message queue
π LIBRARY π
Standard C Library (libc, -lc) β she's standard, she's in libc, she's right there. π
π§Ύ SYNOPSIS π§Ύ
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
ssize_t
msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
πΈ DESCRIPTION πΈ
So msgrcv() does exactly what it says on the tin: it receives a message from the message queue you specify in msqid, and it drops that message into the structure that msgp points to. π₯
And that structure? It's giving very much these two members, periodt:
long mtype; /* message type */
char mtext[1]; /* body of message */
mtype is an integer greater than 0, and it's the little tag you use to pick which messages you want. mtext is an array of bytes, and it can be up to as big as the system limit allows (MSGMAX). π
Now, msgtyp β this one's the main character, because its value completely changes the vibe of the call. Here's the tea:
- If
msgtypis greater than 0, you get the first message of typemsgtyp. π― - If
msgtypis equal to 0, you get the first message on the queue, no type discrimination, we're not picky. π« - If
msgtypis less than 0, you get the first message of the lowest message type that is less than or equal to the absolute value ofmsgtyp. Yes bestie, the absolute value. Math class said it would come up again and it did. π
Next up: msgsz specifies the maximum length of the requested message. And here's the part you need to pay attention to, because it's lowkey sneaky:
If the received message is longer thanmsgsz, it gets silently truncated when theMSG_NOERRORflag is set inmsgflg. If that flag is not set, you get an error instead. Silent truncation. Not a word. Nothing. π
Now, what if there's no matching message on the queue that msqid points at? That depends entirely on whether IPC_NOWAIT is set in msgflg:
- If
IPC_NOWAITis set,msgrcv()immediately returns a value of-1and setserrnotoENOMSG. No waiting, no patience, we move. πββοΈ - If
IPC_NOWAITis not set, your calling process gets blocked until one of these three things happens:- A message of the requested type becomes available on the message queue. π
- The message queue is removed β in which case
-1is returned anderrnois set toEINVAL. π - A signal is received and caught β
-1is returned anderrnois set toEINTR. π΅
And when a message is successfully received, the data structure associated with msqid gets updated like this:
msg_cbytesis decremented by the size of the message. πmsg_lrpidis set to the pid of the caller. π«΅msg_lrtimeis set to the current time. β°msg_qnumis decremented by 1. 1οΈβ£
π RETURN VALUES π
When it works out? msgrcv() returns the number of bytes received into the mtext field of the structure that msgp points to. Beautiful. Iconic. β¨
Otherwise you get -1, and errno is set to tell you what went wrong. π
π¨ ERRORS π¨
The msgrcv() function will fail if:
[EINVAL]β Themsqidargument is not a valid message queue identifier. Also: the message queue was removed whilemsgrcv()was waiting for a message of the requested type to become available on it. Also also: themsgszargument is less than 0. π[E2BIG]β A matching message was received, but its size was greater thanmsgszand theMSG_NOERRORflag was not set inmsgflg. She was too big and you didn't give permission. π€[EACCES]β The calling process does not have read access to the message queue. No read permission, no message, sorry bestie. π«[EFAULT]β Themsgpargument points to an invalid address. π³οΈ[EINTR]β The system call was interrupted by the delivery of a signal. π΅βπ«[ENOMSG]β There is no message of the requested type available on the message queue, andIPC_NOWAITis set inmsgflg. Empty queue behavior, immediately returned. π«
π SEE ALSO π
msgctl(2), msgget(2), msgsnd(2) β the whole friend group, go say hi. π«Ά
π HISTORY π
Message queues appeared in the first release of AT&T System V UNIX. She's old money, she's been here since the beginning, and she's still running. π
okay that's the whole page bestie, you're officially a message queue girlie now π β¨
this one's from Debian, dated July 28, 2016 β so shoutout to the Debian crew for keeping the lore alive. mwah. ππ¦
π¦ 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) 1995 Frank van der Linden
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms