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_PRIVATE may 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 key and the IPC_CREAT bit is set in flag, 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 group
  • 0020 β€” write access for group
  • 0004 β€” read access for other
  • 0002 β€” 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? -1 comes back and errno is 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 matching key, and IPC_CREAT was 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_CREAT and IPC_EXCL were specified, and a shared memory segment corresponding to key already 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