SEMGET(2)

✨ semget(2) β€” System Calls Manual energy ✨

okay bestie, pull up a chair because we're about to talk about semaphores and honestly? it's giving iconic. this is semget(2), from the System Calls Manual, and she's here to hand you a semaphore id like it's a friendship bracelet. πŸ’–


πŸŽ€ NAME πŸŽ€

semget β€” obtain a semaphore id


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) β€” yeah she lives in libc, no cap.


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <sys/sem.h>

int
semget(key_t key, int nsems, int flag);

πŸ’… DESCRIPTION πŸ’…

so basically, based on the values of key and flag, semget() returns the identifier of a newly created or previously existing set of semaphores. periodt.

think of key like a filename, fr fr β€” it's the handle that names an IPC object. it's literally the name tag at the party so everyone knows which semaphore set they're talking about. πŸͺͺ

there are three ways to specify a key, and yes you have to pick one:

  • IPC_PRIVATE may be specified, in which case a new IPC object will be created. brand new, no notes, main character debut. ✨
  • An integer constant may be specified. If no IPC object corresponding to key is specified and the IPC_CREAT bit is set in flag, a new one will be created. (yes, the IPC_CREAT bit is doing all the heavy lifting here, she's the moment.) πŸ’…
  • The ftok(3) function may be used to generate a key from a pathname. cute lil pathname-to-key pipeline, obsessed. 🧚

the mode of a newly created IPC object is determined by ORing these constants into the flag argument:

  • 0400 β€” Read access for user. πŸ‘‘
  • 0200 β€” Alter access for user. ✏️
  • 0040 β€” Read access for group. 🫢
  • 0020 β€” Alter access for group. πŸ–ŠοΈ
  • 0004 β€” Read access for other. 🌸
  • 0002 β€” Alter access for other. 🌈

and if a new set of semaphores is being created, nsems is used to indicate the number of semaphores the set should contain. otherwise, nsems may be specified as 0. easy peasy, lowkey. πŸ’œ


🌟 RETURN VALUES 🌟

The semget() system call returns the id of a semaphore set if successful; otherwise, -1 is returned and errno is set to indicate the error. so if you see -1, that's not a vibe check passing, bestie. 😭


πŸ’€ ERRORS πŸ’€

The semget() system call will fail if:

  • [EACCES] β€” Access permission failure. (the door said no. πŸ™ˆ)
  • [EEXIST] β€” IPC_CREAT and IPC_EXCL were specified, and a semaphore set corresponding to key already exists. (she's already here, babes, you can't double-book.) πŸ‘€
  • [EINVAL] β€” The number of semaphores requested exceeds the system imposed maximum per set. (too greedy, the system said enough.) πŸ›‘
  • [EINVAL] β€” A semaphore set corresponding to key already exists and contains fewer semaphores than nsems. (you asked for more than she has. awkward.) 😬
  • [EINVAL] β€” A semaphore set corresponding to key does not exist and nsems is 0 or negative. (nothing there, and you brought no semaphores. tragic.) πŸ’€
  • [ENOSPC] β€” Insufficiently many semaphores are available. (the semaphore shelf is empty, we're out of stock.) πŸ“
  • [ENOSPC] β€” The kernel could not allocate a struct semid_ds. (kernel said "no thoughts, head empty, can't allocate.") 😭
  • [ENOENT] β€” No semaphore set was found corresponding to key, and IPC_CREAT was not specified. (you didn't ask her to create one, so she didn't. respect the rules.) πŸ™…

πŸ”₯ SEE ALSO πŸ”₯

semctl(2), semop(2), ftok(3) β€” go read these next, iykyk. the whole semaphore friend group is here and they all ate. πŸ‘‘


fr fr bestie, if you're gonna create IPC objects, be careful with those mode bits. permissions are not a joke, even when they're giving cute. πŸ’–

and that's the page!! shoutout to Debian for shipping this, dated March 4, 2018 β€” a classic, honestly, she ate and left no crumbs. now go forth and semget responsibly, my love. 🫢✨

πŸ“¦ where this page comes from