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.cuidandmsg_perm.uidare set to the effective user ID of the calling process.msg_perm.cgidandmsg_perm.gidare set to the effective group ID of the calling process.- the least significant 9 bits of
msg_perm.modeare set to the least significant 9 bits ofmsgflg. msg_qnum,msg_lspid,msg_lrpid,msg_stime, andmsg_rtimeare set to0.msg_ctimeis set to the current time. โฐmsg_qbytesis set to the system limitMSGMNB.
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 forkey, but the calling process does not have permission to access the queue, and does not have theCAP_IPC_OWNERcapability in the user namespace that governs its IPC namespace. rude, honestly.EEXISTโIPC_CREATandIPC_EXCLwere specified inmsgflg, but a message queue already exists forkey.ENOENTโ no message queue exists forkeyandmsgflgdid not specifyIPC_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 is32,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
- 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