shmctl(2)
๐ฆ shmctl(2) โ System V shared memory control ๐ฆ
okay so real talk: you're reading the page for shmctl(2), straight out of the System Calls Manual, and it is a LOT. That's why this comes in two parts bestie, we're pacing ourselves ๐. Part 1 is the whole DESCRIPTION, RETURN VALUE, ERRORS and VERSIONS of the thing โ the actual mechanics, no skips, no "you get the idea". Every flag, every number, every struct, all of it. Let's go โจ
๐ซ NAME ๐ซ
shmctl โ System V shared memory control. that's the name. iconic, short, does exactly what it says ๐
๐ LIBRARY ๐
Standard C library (libc, -lc). yes queen, it's just right there, you don't need to link anything weird.
๐ซถ SYNOPSIS ๐ซถ
#include <sys/shm.h>
int shmctl(int shmid, int op, struct shmid_ds *buf);
three args, one call, endless power. lowkey the whole page is just "what do you put in op" ๐ธ
๐ DESCRIPTION ๐
shmctl() performs the control operation specified by op on the System V shared memory segment whose identifier is given in shmid.
The buf argument is a pointer to a shmid_ds structure, defined in <sys/shm.h> as follows:
struct shmid_ds {
struct ipc_perm shm_perm; /* Ownership and permissions */
size_t shm_segsz; /* Size of segment (bytes) */
time_t shm_atime; /* Last attach time */
time_t shm_dtime; /* Last detach time */
time_t shm_ctime; /* Creation time/time of last
modification via shmctl() */
pid_t shm_cpid; /* PID of creator */
pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */
shmatt_t shm_nattch; /* # of current attaches */
...
};
The fields of the shmid_ds structure are as follows โ tour time, bestie ๐ง:
shm_permโ this is anipc_permstructure (spelled out below) that says who's allowed to do what with the shared memory segment.shm_segszโ size in bytes of the shared memory segment.shm_atimeโ time of the lastshmat(2)system call that attached this segment.shm_dtimeโ time of the lastshmdt(2)system call that detached this segment. (the original page typed it as "tgis" and honestly, same energy, we don't gatekeep typos ๐ )shm_ctimeโ time of creation of the segment, or the time of the lastshmctl()IPC_SEToperation. whichever happened most recently.shm_cpidโ ID of the process that created the shared memory segment. the OG.shm_lpidโ ID of the last process that executed ashmat(2)orshmdt(2)system call on this segment.shm_nattchโ number of processes that have this segment attached right now.
๐ the ipc_perm structure ๐
The ipc_perm structure is defined as follows (the highlighted fields are settable using IPC_SET):
struct ipc_perm {
key_t __key; /* Key supplied to shmget(2) */
uid_t uid; /* Effective UID of owner */
gid_t gid; /* Effective GID of owner */
uid_t cuid; /* Effective UID of creator */
gid_t cgid; /* Effective GID of creator */
unsigned short mode; /* Permissions + SHM_DEST and
SHM_LOCKED flags */
unsigned short __seq; /* Sequence number */
};
The least significant 9 bits of the mode field of the ipc_perm structure define the access permissions for the shared memory segment. The permission bits are as follows:
0400โ Read by user0200โ Write by user0040โ Read by group0020โ Write by group0004โ Read by others0002โ Write by others
Bits 0100, 0010, and 0001 (the execute bits) are unused by the system. (It is not necessary to have execute permission on a segment in order to perform a shmat(2) call with the SHM_EXEC flag.) likeโฆ the kernel just doesn't care about execute here, periodt ๐
๐ฅ valid values for op ๐ฅ
This is the main character list. here we go:
IPC_STATโ copy information from the kernel data structure associated withshmidinto theshmid_dsstructure pointed to bybuf. The caller must have read permission on the shared memory segment. no read perm, no tea โIPC_SETโ write the values of some members of theshmid_dsstructure pointed to bybufto the kernel data structure associated with this shared memory segment, and update itsshm_ctimemember while you're at it.
The following fields get updated: shm_perm.uid, shm_perm.gid, and (the least significant 9 bits of) shm_perm.mode.
The effective UID of the calling process must match the owner (shm_perm.uid) or creator (shm_perm.cuid) of the shared memory segment, or the caller must be privileged. it's giving "you have to be on the list" ๐
IPC_RMIDโ mark the segment to be destroyed. The segment will actually be destroyed only after the last process detaches it (i.e., when theshm_nattchmember of the associated structureshmid_dsis zero). The caller must be the owner or creator of the segment, or be privileged. Thebufargument is ignored.
If a segment has been marked for destruction, then the (nonstandard) SHM_DEST flag of the shm_perm.mode field in the associated data structure retrieved by IPC_STAT will be set.
โ ๏ธ The caller must ensure that a segment is eventually destroyed; otherwise its pages that were faulted in will remain in memory or swap. like, actually please clean up after yourself bestie, your pages are just sitting there ๐ญ
See also the description of /proc/sys/kernel/shm_rmid_forced in proc(5).
IPC_INFO(Linux-specific) โ return information about system-wide shared memory limits and parameters in the structure pointed to bybuf. This structure is of typeshminfo(thus, a cast is required), defined in<sys/shm.h>if the_GNU_SOURCEfeature test macro is defined:
struct shminfo {
unsigned long shmmax; /* Maximum segment size */
unsigned long shmmin; /* Minimum segment size;
always 1 */
unsigned long shmmni; /* Maximum number of segments */
unsigned long shmseg; /* Maximum number of segments
that a process can attach;
unused within kernel */
unsigned long shmall; /* Maximum number of pages of
shared memory, system-wide */
};
The shmmni, shmmax, and shmall settings can be changed via /proc files of the same name; see proc(5) for details. yes, the same name. they're not even being subtle about it โจ
SHM_INFO(Linux-specific) โ return ashm_infostructure whose fields contain information about system resources consumed by shared memory. This structure is defined in<sys/shm.h>if the_GNU_SOURCEfeature test macro is defined:
struct shm_info {
int used_ids; /* # of currently existing
segments */
unsigned long shm_tot; /* Total number of shared
memory pages */
unsigned long shm_rss; /* # of resident shared
memory pages */
unsigned long shm_swp; /* # of swapped shared
memory pages */
unsigned long swap_attempts;
/* Unused since Linux 2.4 */
unsigned long swap_successes;
/* Unused since Linux 2.4 */
};
lowkey obsessed that two whole fields are just sitting there unused since Linux 2.4. relics. we love them ๐
SHM_STAT(Linux-specific) โ return ashmid_dsstructure as forIPC_STAT. However, theshmidargument is not a segment identifier, but instead an index into the kernel's internal array that maintains information about all shared memory segments on the system. plot twist, the argument switched jobs ๐SHM_STAT_ANY(Linux-specific, since Linux 4.17) โ return ashmid_dsstructure as forSHM_STAT. However,shm_perm.modeis not checked for read access forshmid, meaning that any user can employ this operation (just as any user may read/proc/sysvipc/shmto obtain the same information).
๐ธ the swapping ops ๐ธ
The caller can prevent or allow swapping of a shared memory segment with the following op values:
SHM_LOCK(Linux-specific) โ prevent swapping of the shared memory segment. The caller must fault in any pages that are required to be present after locking is enabled. If a segment has been locked, then the (nonstandard)SHM_LOCKEDflag of theshm_perm.modefield in the associated data structure retrieved byIPC_STATwill be set. lock it down, no swap for you ๐SHM_UNLOCK(Linux-specific) โ unlock the segment, allowing it to be swapped out. free it, let it roam ๐ฆ
Before Linux 2.6.10, only a privileged process could employ SHM_LOCK and SHM_UNLOCK. Since Linux 2.6.10, an unprivileged process can employ these operations if its effective UID matches the owner or creator UID of the segment, and (for SHM_LOCK) the amount of memory to be locked falls within the RLIMIT_MEMLOCK resource limit (see setrlimit(2)). glow-up for the unprivileged girlies ๐
โญ RETURN VALUE โญ
A successful IPC_INFO or SHM_INFO operation returns the index of the highest used entry in the kernel's internal array recording information about all shared memory segments. (This information can be used with repeated SHM_STAT or SHM_STAT_ANY operations to obtain information about all shared memory segments on the system.) A successful SHM_STAT operation returns the identifier of the shared memory segment whose index was given in shmid. Other operations return 0 on success.
On error, -1 is returned, and errno is set to indicate the error.
so:-1means something went wrong anderrnowill tell you what. check it. don't just stare at the-1. ๐ญ
๐ ERRORS ๐
EACCESโIPC_STATorSHM_STATis requested andshm_perm.modedoes not allow read access forshmid, and the calling process does not have theCAP_IPC_OWNERcapability in the user namespace that governs its IPC namespace.EFAULTโ the argumentophas valueIPC_SETorIPC_STATbut the address pointed to bybufisn't accessible.EIDRMโshmidpoints to a removed identifier. it's gone bestie, it got deleted ๐ซถEINVALโshmidis not a valid identifier, oropis not a valid operation. Or: for aSHM_STATorSHM_STAT_ANYoperation, the index value specified inshmidreferred to an array slot that is currently unused.ENOMEM(Since Linux 2.6.9) โSHM_LOCKwas specified and the size of the to-be-locked segment would mean that the total bytes in locked shared memory segments would exceed the limit for the real user ID of the calling process. This limit is defined by theRLIMIT_MEMLOCKsoft resource limit (seesetrlimit(2)).EOVERFLOWโIPC_STATis attempted, and the GID or UID value is too large to be stored in the structure pointed to bybuf. big number, small struct, tragic ๐EPERMโIPC_SETorIPC_RMIDis attempted, and the effective user ID of the calling process is not that of the creator (found inshm_perm.cuid), or the owner (found inshm_perm.uid), and the process was not privileged (Linux: did not have theCAP_SYS_ADMINcapability).
Or (before Linux 2.6.9), SHM_LOCK or SHM_UNLOCK was specified, but the process was not privileged (Linux: did not have the CAP_IPC_LOCK capability). (Since Linux 2.6.9, this error can also occur if the RLIMIT_MEMLOCK is 0 and the caller is not privileged.)
๐ VERSIONS ๐
Linux permits a process to attach (shmat(2)) a shared memory segment that has already been marked for deletion using shmctl(IPC_RMID). This feature is not available on other UNIX implementations; portable applications should avoid relying on it. like, don't build your whole personality on a Linux-only trick if you want to travel โ๏ธ
๐ STANDARDS ๐
Posix.1-2024, bestie. That's the standard it's serving. ๐
๐ HISTORY ๐
So like, this goes way back:
POSIX.1-2001, and alsoSVr4. Vintage, iconic, still relevant. ๐
Also, a little glow-up story for you: various fields inside struct shmid_ds were typed as short back under Linux 2.2, and they became long under Linux 2.4. The way the struct literally hit puberty. ๐ญ
To take advantage of that, just recompiling under glibc-2.1.91 or later should be enough โ that's it, that's the whole trick, no cap. (And how does the kernel tell old calls from new ones? By peeping an IPC_64 flag inside op. Sneaky little thing. ๐)
๐ก NOTES ๐ก
Okay so these three operations โ IPC_INFO, SHM_STAT, and SHM_INFO โ are the ones the ipcs(1) program uses to hand you info about allocated resources. That's their whole job, that's their moment. โจ
And heads up: in the future, these may get modified or moved over to a /proc filesystem interface. So likeโฆ don't get too attached to them exactly as they are. Not me warning you in advance. ๐
๐ซถ SEE ALSO ๐ซถ
Go read them, they're all lowkey essential. ๐
And that's the page, bestie โ courtesy of Linux man-pages 6.20, dated 2026-02-08, for none other than shmctl(2) itself. ๐ Slay, you made it to the end. Now go shmctl responsibly. ๐
๐ฆ 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, Luigi P. Bai <lpb@softint.com>
- Copyright 1993, Giorgio Ciucci <giorgio@crcc.it>
- Copyright 2004-2005, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms