MSGOP(2)
๐ MSGOP(2) ๐ โ like, the manual page for msgrcv and msgsnd, bestie
okay so. this is the manual page msgop from section 2 of the manual (system calls, obvi). it's giving kernel-level group chat energy and honestly? iconic. let's get into it ๐ซถ
โจ NAME โจ
msgrcv, msgsnd โ System V message queue operations. yes, both of them, together, one page, they're a package deal ๐
๐ LIBRARY ๐
Standard C library (libc, -lc). the basics, the loml, no notes ๐ธ
๐ SYNOPSIS ๐
#include <sys/msg.h>
ssize_t msgrcv(size_t msgsz;
int msqid, void msgp[msgsz], size_t msgsz, long msgtyp,
int msgflg);
int msgsnd(size_t msgsz;
int msqid, const void msgp[msgsz], size_t msgsz,
int msgflg);
bestie the indentation on those wrapped lines is exactly like that in the original. respect the drip. ๐ฉท
๐ DESCRIPTION ๐
so msgsnd() and msgrcv() are the two system calls you use to send messages to and receive messages from a System V message queue. that's it, that's the job. โจ
heads up: the process doing the calling has to have write permission on the message queue to send, and read permission to receive. perms check first, vibes second. ๐
The msgp argument is a pointer to a caller-defined structure that looks like this, generally:
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
the mtext field is an array (or honestly any other structure, live ur truth) whose size is given by msgsz, which is a nonnegative integer value. and yes โ messages of zero length (like, literally no mtext field at all) are permitted. empty message, full heart. ๐ณ๏ธ๐
The mtype field has to have a strictly positive integer value. and the receiving process can use that value to pick which messages it wants to read (peep the msgrcv() description down below, iykyk). ๐
๐ฆ msgsnd() ๐ฆ
msgsnd() appends a copy of the message pointed to by msgp onto the message queue whose identifier is msqid. copy-paste but make it IPC. ๐
If there's enough space in the queue, msgsnd() succeeds immediately, no waiting, no drama. the queue capacity is governed by the msg_qbytes field in the associated data structure for the message queue. when the queue is created, that field gets initialized to MSGMNB bytes, but you can modify that limit using msgctl(2). glow-up potential. ๐
A message queue is considered full if either of these is true:
- Adding a new message to the queue would cause the total number of bytes in the queue to exceed the queue's maximum size (the
msg_qbytesfield). - Adding another message to the queue would cause the total number of messages in the queue to exceed the queue's maximum size (the
msg_qbytesfield). this check is necessary to stop an unlimited number of zero-length messages from piling onto the queue. like, those messages contain literally no data, and yet they still consume (locked) kernel memory. freeloaders. ๐ค
If there's not enough space in the queue, then the default behavior of msgsnd() is to block until space opens up. if instead IPC_NOWAIT is specified in msgflg, then the call fails with the error EAGAIN instead. no patience today. ๐ซ
A blocked msgsnd() call may also fail if:
- the queue is removed, in which case the system call fails with
errnoset toEIDRM; or - a signal is caught, in which case the system call fails with
errnoset toEINTR; seesignal(7). (and listen โmsgsnd()is never automatically restarted after being interrupted by a signal handler, regardless of how theSA_RESTARTflag was set when establishing that signal handler. stubborn queen. ๐ )
Upon successful completion the message queue data structure is updated like this:
msg_lspidis set to the process ID of the calling process.msg_qnumis incremented by 1.msg_stimeis set to the current time.
๐ธ msgrcv() ๐ธ
msgrcv() removes a message from the queue specified by msqid and places it into the buffer pointed to by msgp. yes bestie, it's destructive, that message is leaving the queue and going home with you. ๐โ๏ธ
The argument msgsz specifies the maximum size in bytes for the member mtext of the structure pointed to by the msgp argument. if the message text is longer than msgsz, then what happens depends on whether MSG_NOERROR is specified in msgflg:
- if
MSG_NOERRORis specified, then the message text gets truncated (and the truncated part is lost, gone, deceased ๐); - if
MSG_NOERRORis not specified, then the message isn't removed from the queue at all and the system call fails, returning-1witherrnoset toE2BIG.
Unless MSG_COPY is specified in msgflg (more on that in a sec), the msgtyp argument specifies the type of message you're requesting, like this:
- If
msgtypis0, then the first message in the queue is read. first come first served, no picks. - If
msgtypis greater than0, then the first message in the queue of typemsgtypis read โ unlessMSG_EXCEPTwas specified inmsgflg, in which case the first message in the queue of type not equal tomsgtypgets read. petty behavior, honestly valid. - If
msgtypis less than0, then the first message in the queue with the lowest type less than or equal to the absolute value ofmsgtypis read. math girlies rise. ๐
The msgflg argument is a bit mask, constructed by ORing together zero or more of these flags:
IPC_NOWAITโ return immediately if no message of the requested type is in the queue. the system call fails witherrnoset toENOMSG. no waiting, no begging. ๐ โ๏ธMSG_COPY(since Linux 3.8) โ nondestructively fetch a copy of the message at the ordinal position in the queue specified bymsgtyp(messages are considered to be numbered starting at0).- This flag must be specified in conjunction with
IPC_NOWAIT, and the result of that combo is: if there's no message available at the given position, the call fails immediately with the errorENOMSG. - Because
MSG_COPYandMSG_EXCEPTalter the meaning ofmsgtypin orthogonal ways, you may not specify both of them inmsgflg. pick a lane bestie. ๐ฃ๏ธ - The
MSG_COPYflag was added for the implementation of the kernel checkpoint-restore facility, and it's available only if the kernel was built with theCONFIG_CHECKPOINT_RESTOREoption. special build, special privileges. ๐
- This flag must be specified in conjunction with
MSG_EXCEPTโ used withmsgtypgreater than0to read the first message in the queue with a message type that differs frommsgtyp.MSG_NOERRORโ to truncate the message text if it's longer thanmsgszbytes. โ๏ธ
If no message of the requested type is available and IPC_NOWAIT isn't specified in msgflg, the calling process gets blocked until one of these things happens:
- A message of the desired type is placed in the queue. ๐
- The message queue is removed from the system. in this case, the system call fails with
errnoset toEIDRM. - The calling process catches a signal. in this case, the system call fails with
errnoset toEINTR. (and again โmsgrcv()is never automatically restarted after being interrupted by a signal handler, regardless of the setting of theSA_RESTARTflag when establishing a signal handler. she does not do second chances. ๐ )
Upon successful completion the message queue data structure is updated like this:
msg_lrpidis set to the process ID of the calling process.msg_qnumis decremented by 1.msg_rtimeis set to the current time.
๐ซถ RETURN VALUE ๐ซถ
On success, msgsnd() returns 0 and msgrcv() returns the number of bytes actually copied into the mtext array. ๐
On failure, both functions return -1, and set errno to indicate the error. iconic flop behavior, but at least they tell you why. ๐ญ
๐ ERRORS ๐
msgsnd() can fail with the following errors:
EACCESโ The calling process does not have write permission on the message queue, and does not have theCAP_IPC_OWNERcapability in the user namespace that governs its IPC namespace.EAGAINโ The message can't be sent due to themsg_qbyteslimit for the queue andIPC_NOWAITwas specified inmsgflg.EFAULTโ The address pointed to bymsgpisn't accessible.EIDRMโ The message queue was removed.EINTRโ Sleeping on a full message queue condition, the process caught a signal.EINVALโ Invalidmsqidvalue, or nonpositivemtypevalue, or invalidmsgszvalue (less than0or greater than the system valueMSGMAX).ENOMEMโ The system does not have enough memory to make a copy of the message pointed to bymsgp. the system is broke, spiritually and RAM-ly. ๐
msgrcv() can fail with the following errors:
E2BIGโ The message text length is greater thanmsgszandMSG_NOERRORisn't specified inmsgflg.EACCESโ The calling process does not have read permission on the message queue, and does not have theCAP_IPC_OWNERcapability in the user namespace that governs its IPC namespace.EFAULTโ The address pointed to bymsgpisn't accessible.EIDRMโ While the process was sleeping to receive a message, the message queue was removed.EINTRโ While the process was sleeping to receive a message, the process caught a signal; seesignal(7).EINVALโmsqidwas invalid, ormsgszwas less than0.EINVAL(since Linux 3.14) โmsgflgspecifiedMSG_COPY, but notIPC_NOWAIT.EINVAL(since Linux 3.14) โmsgflgspecified bothMSG_COPYandMSG_EXCEPT.ENOMSGโIPC_NOWAITwas specified inmsgflgand no message of the requested type existed on the message queue.ENOMSGโIPC_NOWAITandMSG_COPYwere specified inmsgflgand the queue contains fewer thanmsgtypmessages.ENOSYS(since Linux 3.8) โ BothMSG_COPYandIPC_NOWAITwere specified inmsgflg, and this kernel was configured withoutCONFIG_CHECKPOINT_RESTORE. bestie the kernel said no. ๐ซ
โญ STANDARDS โญ
POSIX.1-2024 XSI.
The MSG_EXCEPT and MSG_COPY flags are Linux-specific; their definitions can be obtained by defining the _GNU_SOURCE feature test macro. (define it, define it, define it. ๐ง)
๐ HISTORY ๐
SVr4, SUSv1, POSIX.1-2001 XSI.
The msgp argument is declared as struct msgbuf * in glibc 2.0 and 2.1. It is declared as void * in glibc 2.2 and later, as required by SUSv2 and SUSv3. glow-up arc, we love to see it. โจ
๐ NOTES ๐
These limits on message queue resources affect the msgsnd() call:
MSGMAXโ Maximum size of a message text, in bytes (default value: 8192 bytes). On Linux, this limit can be read and modified via/proc/sys/kernel/msgmax.MSGMNBโ Maximum number of bytes that can be held in a message queue (default value: 16384 bytes). On Linux, this limit can be read and modified via/proc/sys/kernel/msgmnb. A privileged process (Linux: a process with theCAP_SYS_RESOURCEcapability) can increase the size of a message queue beyondMSGMNBusing themsgctl(2)IPC_SEToperation. rich get richer, but make it kernel memory. ๐ธ
The implementation has no intrinsic system-wide limits on the number of message headers (MSGTQL) and the number of bytes in the message pool (MSGPOOL). freedom, no cap (literally). ๐งข
๐ BUGS ๐
In Linux 3.13 and earlier, if msgrcv() was called with the MSG_COPY flag, but without IPC_NOWAIT, and the message queue contained fewer than msgtyp messages, then the call would block until the next message is written to the queue. at that point, the call would return a copy of the message, regardless of whether that message was at the ordinal position msgtyp. yeah. wrong message, same energy. This bug is fixed in Linux 3.14. ๐ ๏ธ
Specifying both MSG_COPY and MSC_EXCEPT in msgflg is a logical error (since these flags impose different interpretations on msgtyp). in Linux 3.13 and earlier, this error was not diagnosed by msgrcv(). This bug is fixed in Linux 3.14. ๐ ๏ธ
and that's part 1 bestie ๐ the lore continues in part 2, don't go anywhere โจ
โจ EXAMPLES โจ
ok so bestie, the little program down below is a whole demo of how msgsnd() and msgrcv() work together ๐
you run it once with the -s option to send a message, and then you run it again with the -r option to receive a message. iconic two-step.
and here's what a sample run of the program actually looks like in the shell, no cap:
$ ./a.out -s
sent: a message at Wed Mar 4 16:25:45 2015
$ ./a.out -r
message received: a message at Wed Mar 4 16:25:45 2015
๐ฆ Program source
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>
#include <unistd.h>
struct msgbuf {
long mtype;
char mtext[80];
};
static void
usage(char *prog_name, char *msg)
{
if (msg != NULL)
fputs(msg, stderr);
fprintf(stderr, "Usage: %s [options]\n", prog_name);
fprintf(stderr, "Options are:\n");
fprintf(stderr, "-s send message using msgsnd()\n");
fprintf(stderr, "-r read message using msgrcv()\n");
fprintf(stderr, "-t message type (default is 1)\n");
fprintf(stderr, "-k message queue key (default is 1234)\n");
exit(EXIT_FAILURE);
}
static void
send_msg(int qid, int msgtype)
{
time_t t;
struct msgbuf msg;
msg.mtype = msgtype;
time(&t);
snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
ctime(&t));
if (msgsnd(qid, &msg, sizeof(msg.mtext),
IPC_NOWAIT) == -1)
{
perror("msgsnd error");
exit(EXIT_FAILURE);
}
printf("sent: %s\n", msg.mtext);
}
static void
get_msg(int qid, int msgtype)
{
struct msgbuf msg;
if (msgrcv(qid, &msg, sizeof(msg.mtext), msgtype,
MSG_NOERROR | IPC_NOWAIT) == -1) {
if (errno != ENOMSG) {
perror("msgrcv");
exit(EXIT_FAILURE);
}
printf("No message available for msgrcv()\n");
} else {
printf("message received: %s\n", msg.mtext);
}
}
int
main(int argc, char *argv[])
{
int qid, opt;
int mode = 0; /* 1 = send, 2 = receive */
int msgtype = 1;
int msgkey = 1234;
while ((opt = getopt(argc, argv, "srt:k:")) != -1) {
switch (opt) {
case 's':
mode = 1;
break;
case 'r':
mode = 2;
break;
case 't':
msgtype = atoi(optarg);
if (msgtype <= 0)
usage(argv[0], "-t option must be greater than 0\n");
break;
case 'k':
msgkey = atoi(optarg);
break;
default:
usage(argv[0], "Unrecognized option\n");
}
}
if (mode == 0)
usage(argv[0], "must use either -s or -r option\n");
qid = msgget(msgkey, IPC_CREAT | 0666);
if (qid == -1) {
perror("msgget");
exit(EXIT_FAILURE);
}
if (mode == 2)
get_msg(qid, msgtype);
else
send_msg(qid, msgtype);
exit(EXIT_SUCCESS);
}
๐ SEE ALSO ๐
the other pages you're gonna want in your tabs, fr fr:
and that's the whole page bestie ๐ซถ this one's from Linux man-pages 6.20, dated 2026-02-11, and yes we're calling it MSGOP(2) โ go send some messages, you're doing amazing ๐
โจ
๐ฆ 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 1993, Giorgio Ciucci <giorgio@crcc.it>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms