SHMGET(2)
π SHMGET(2) π
okay bestie, welcome to shmget β it's a system call from the System Calls Manual and it's literally the bouncer at the door of shared memory. her whole job? obtain a shared memory identifier. that's it. that's the vibe. β¨
she lives in the Standard C Library, aka libc, aka -lc. slay.
β¨ SYNOPSIS β¨
#include <sys/shm.h>
int
shmget(key_t key, size_t size, int flag);
three args, no drama. let's get into it π«Ά
πΈ DESCRIPTION πΈ
so depending on what you pass as key and flag, shmget() hands you back the identifier of a shared memory segment β either one that's brand new or one that already existed. either way you're getting a valid ID back, bestie. no cap.
and think of key like a filename, fr fr β it's the handle that names an IPC object. you need a name to find your thing, same energy. π
there are three ways to specify a key:
IPC_PRIVATEmay be specified β in which case a new IPC object will be created. baby's first segment, iconic.- an integer constant may be specified β and if there's no IPC object corresponding to
keyand theIPC_CREATbit is set inflag, a new one will be created. so basically: "make it if it's not there." king behavior. ftok(3)may be used to generate a key from a pathname. pathname β key pipeline, obsessed. π¦
now, the mode of a newly created IPC object? that's decided by which of these constants you OR into the flag argument. memorize these, they're the whole permission vibe check:
0400β read access for owner π0200β write access for owner π0040β read access for group0020β write access for group0004β read access for other0002β write access for other
and when you're creating a new shared memory segment, size is the desired size of the new segment in bytes. lowkey important caveat tho: the size of the segment may be rounded up to a multiple convenient to the kernel β i.e. the page size. so don't come crying if you get a lil extra. it's giving "close enough." π
π RETURN VALUES π
- success?
shmget()returns the positive integer identifier of a shared memory segment. positive! good! we love her! β¨ - not success?
-1comes back anderrnois set to indicate the error. sobbing. π
π₯ ERRORS π₯
the shmget() system call will fail if:
[EINVAL]β size specified is greater than the size of the previously existing segment. or size specified is less than the system imposed minimum, or greater than the system imposed maximum. so: too big for the old one, too small for the system, or too big for the system. goldilocks but make it memory.[ENOENT]β no shared memory segment was found matchingkey, andIPC_CREATwas not specified. girl is not there and you never asked for her to be created. π[ENOSPC]β the kernel was unable to allocate enough memory to satisfy the request. the kernel tried and the kernel could not. rip.[EEXIST]βIPC_CREATandIPC_EXCLwere specified, and a shared memory segment corresponding tokeyalready exists. you said "create it" AND "but only if it's new" β and it wasn't new. awkward.
heads up bestie: these are the only failure modes listed, so if your call blows up some other way, that's not this page's problem, iykyk.
π SEE ALSO π
go read those next, they're the rest of the shared memory friend group π
and that's the whole page, bestie!! this one came from Debian and it's dated March 4, 2018 β the SHMGET(2) era, periodt. now go get yourself a shared memory identifier, you main character. π β¨π«Ά
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1995 David Hovemeyer <daveho@infocom.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms