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_qbytes field).
  • 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_qbytes field). 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 errno set to EIDRM; or
  • a signal is caught, in which case the system call fails with errno set to EINTR; see signal(7). (and listen โ€” msgsnd() is never automatically restarted after being interrupted by a signal handler, regardless of how the SA_RESTART flag was set when establishing that signal handler. stubborn queen. ๐Ÿ’…)

Upon successful completion the message queue data structure is updated like this:

  • msg_lspid is set to the process ID of the calling process.
  • msg_qnum is incremented by 1.
  • msg_stime is 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_NOERROR is specified, then the message text gets truncated (and the truncated part is lost, gone, deceased ๐Ÿ’€);
  • if MSG_NOERROR is not specified, then the message isn't removed from the queue at all and the system call fails, returning -1 with errno set to E2BIG.

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 msgtyp is 0, then the first message in the queue is read. first come first served, no picks.
  • If msgtyp is greater than 0, then the first message in the queue of type msgtyp is read โ€” unless MSG_EXCEPT was specified in msgflg, in which case the first message in the queue of type not equal to msgtyp gets read. petty behavior, honestly valid.
  • If msgtyp is less than 0, then the first message in the queue with the lowest type less than or equal to the absolute value of msgtyp is 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 with errno set to ENOMSG. 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 by msgtyp (messages are considered to be numbered starting at 0).
    • 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 error ENOMSG.
    • Because MSG_COPY and MSG_EXCEPT alter the meaning of msgtyp in orthogonal ways, you may not specify both of them in msgflg. pick a lane bestie. ๐Ÿ›ฃ๏ธ
    • The MSG_COPY flag was added for the implementation of the kernel checkpoint-restore facility, and it's available only if the kernel was built with the CONFIG_CHECKPOINT_RESTORE option. special build, special privileges. ๐Ÿ‘‘
  • MSG_EXCEPT โ€” used with msgtyp greater than 0 to read the first message in the queue with a message type that differs from msgtyp.
  • MSG_NOERROR โ€” to truncate the message text if it's longer than msgsz bytes. โœ‚๏ธ

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 errno set to EIDRM.
  • The calling process catches a signal. in this case, the system call fails with errno set to EINTR. (and again โ€” msgrcv() is never automatically restarted after being interrupted by a signal handler, regardless of the setting of the SA_RESTART flag when establishing a signal handler. she does not do second chances. ๐Ÿ’…)

Upon successful completion the message queue data structure is updated like this:

  • msg_lrpid is set to the process ID of the calling process.
  • msg_qnum is decremented by 1.
  • msg_rtime is 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 the CAP_IPC_OWNER capability in the user namespace that governs its IPC namespace.
  • EAGAIN โ€” The message can't be sent due to the msg_qbytes limit for the queue and IPC_NOWAIT was specified in msgflg.
  • EFAULT โ€” The address pointed to by msgp isn't accessible.
  • EIDRM โ€” The message queue was removed.
  • EINTR โ€” Sleeping on a full message queue condition, the process caught a signal.
  • EINVAL โ€” Invalid msqid value, or nonpositive mtype value, or invalid msgsz value (less than 0 or greater than the system value MSGMAX).
  • ENOMEM โ€” The system does not have enough memory to make a copy of the message pointed to by msgp. the system is broke, spiritually and RAM-ly. ๐Ÿ˜”

msgrcv() can fail with the following errors:

  • E2BIG โ€” The message text length is greater than msgsz and MSG_NOERROR isn't specified in msgflg.
  • EACCES โ€” The calling process does not have read permission on the message queue, and does not have the CAP_IPC_OWNER capability in the user namespace that governs its IPC namespace.
  • EFAULT โ€” The address pointed to by msgp isn'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; see signal(7).
  • EINVAL โ€” msqid was invalid, or msgsz was less than 0.
  • EINVAL (since Linux 3.14) โ€” msgflg specified MSG_COPY, but not IPC_NOWAIT.
  • EINVAL (since Linux 3.14) โ€” msgflg specified both MSG_COPY and MSG_EXCEPT.
  • ENOMSG โ€” IPC_NOWAIT was specified in msgflg and no message of the requested type existed on the message queue.
  • ENOMSG โ€” IPC_NOWAIT and MSG_COPY were specified in msgflg and the queue contains fewer than msgtyp messages.
  • ENOSYS (since Linux 3.8) โ€” Both MSG_COPY and IPC_NOWAIT were specified in msgflg, and this kernel was configured without CONFIG_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 the CAP_SYS_RESOURCE capability) can increase the size of a message queue beyond MSGMNB using the msgctl(2) IPC_SET operation. 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms