msgget(2)

โœจ msgget(2) โœจ

okay bestie, welcome to the System Calls Manual ๐Ÿ’– this is msgget(2) and it's literally your ticket into the System V message queue world. no cap.


๐ŸŒธ NAME ๐ŸŒธ

msgget โ€” get a System V message queue identifier ๐Ÿ‘‘


๐ŸŽ€ LIBRARY ๐ŸŽ€

Standard C library (libc, -lc) ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <sys/msg.h>

int msgget(key_t key, int msgflg);

๐Ÿ’… DESCRIPTION ๐Ÿ’…

so the msgget() system call hands you back the System V message queue identifier that's associated with whatever value you put in the key argument. it's giving โœจtwo jobs in oneโœจ, because you use it either to grab the identifier of a queue that already exists (that's when msgflg is zero and key does not have the value IPC_PRIVATE), or to create a whole new set. iconic.

A new message queue gets created if key has the value IPC_PRIVATE, or if key isn't IPC_PRIVATE, no message queue with that given key key exists yet, and IPC_CREAT is specified in msgflg. yeah we said key three times, that's just how it is, she's the main character ๐Ÿ’€.

If msgflg specifies both IPC_CREAT and IPC_EXCL and a message queue already exists for key, then msgget() fails with errno set to EEXIST. (this is analogous to the effect of the combination O_CREAT | O_EXCL for open(2) โ€” same energy, different syscall ๐Ÿ”ฅ)

Upon creation, the least significant bits of the argument msgflg define the permissions of the message queue. these permission bits have the exact same format and semantics as the permissions specified for the mode argument of open(2). (the execute permissions are not used โ€” sorry babe, no x bit for you ๐Ÿ™ˆ)

If a new message queue is created, then its associated data structure msqid_ds (see msgctl(2)) is initialized as follows:

  • msg_perm.cuid and msg_perm.uid are set to the effective user ID of the calling process.
  • msg_perm.cgid and msg_perm.gid are set to the effective group ID of the calling process.
  • the least significant 9 bits of msg_perm.mode are set to the least significant 9 bits of msgflg.
  • msg_qnum, msg_lspid, msg_lrpid, msg_stime, and msg_rtime are set to 0.
  • msg_ctime is set to the current time. โฐ
  • msg_qbytes is set to the system limit MSGMNB.

If the message queue already exists, the permissions get verified, and a check is made to see if it's marked for destruction. vibe check, basically ๐Ÿ•ต๏ธโ™€๏ธ


๐Ÿฆ‹ RETURN VALUE ๐Ÿฆ‹

On success, msgget() returns the message queue identifier โ€” a nonnegative integer ๐ŸŽ‰. On failure, -1 is returned, and errno is set to indicate the error. ๐Ÿ˜ญ


๐Ÿ”ฅ ERRORS ๐Ÿ”ฅ

  • EACCES โ€” a message queue exists for key, but the calling process does not have permission to access the queue, and does not have the CAP_IPC_OWNER capability in the user namespace that governs its IPC namespace. rude, honestly.
  • EEXIST โ€” IPC_CREAT and IPC_EXCL were specified in msgflg, but a message queue already exists for key.
  • ENOENT โ€” no message queue exists for key and msgflg did not specify IPC_CREAT.
  • ENOMEM โ€” a message queue has to be created but the system does not have enough memory for the new data structure. the audacity ๐Ÿ’€
  • ENOSPC โ€” a message queue has to be created but the system limit for the maximum number of message queues (MSGMNI) would be exceeded.

๐ŸŒˆ STANDARDS ๐ŸŒˆ

POSIX.1-2024 XSI. periodt. ๐Ÿ’–


๐ŸŒธ HISTORY ๐ŸŒธ

SVr4, SUSv1, POSIX.1-2001 XSI. she's been around, she's a legend ๐Ÿ‘‘

๐Ÿง Linux ๐Ÿง

Until Linux 2.3.20, Linux would return EIDRM for a msgget() on a message queue scheduled for deletion. old-school behavior, we don't do that anymore bestie.


โญ NOTES โญ

IPC_PRIVATE isn't a flag field but a key_t type. so if this special value is used for key, the system call ignores everything but the least significant 9 bits of msgflg and creates a new message queue (on success). she's selective, and honestly? valid.

The following is a system limit on message queue resources affecting a msgget() call:

  • MSGMNI โ€” system-wide limit on the number of message queues. Before Linux 3.19, the default value for this limit was calculated using a formula based on available system memory. Since Linux 3.19, the default value is 32,000. On Linux, this limit can be read and modified via /proc/sys/kernel/msgmni. ๐Ÿ“–โœจ

๐Ÿ’€ BUGS ๐Ÿ’€

The name choice IPC_PRIVATE was perhaps unfortunate โ€” IPC_NEW would more clearly show its function. like who named this ๐Ÿ˜ญ not me being mad about a constant from 1983.


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

msgctl(2), msgrcv(2), msgsnd(2), ftok(3), capabilities(7), mq_overview(7), sysvipc(7) ๐Ÿซถ go read them, they're all connected, iykyk.


and that's the whole page bestie!! this one's from Linux man-pages 6.20, dated 2026-02-11 โ€” so when you cite it, cite it cute ๐Ÿ’… mwah, go make some message queues ๐ŸŽ€

๐Ÿ“ฆ 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