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_PRIVATEmay 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
keyis specified and theIPC_CREATbit is set inflag, a new one will be created. (yes, theIPC_CREATbit 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_CREATandIPC_EXCLwere specified, and a semaphore set corresponding tokeyalready 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 tokeyalready exists and contains fewer semaphores thannsems. (you asked for more than she has. awkward.) π¬[EINVAL]β A semaphore set corresponding tokeydoes not exist andnsemsis0or 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 astruct semid_ds. (kernel said "no thoughts, head empty, can't allocate.") π[ENOENT]β No semaphore set was found corresponding tokey, andIPC_CREATwas 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
- 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