SEMOP(2)
π semop(2) β bestie's guide to semaphore ops π
okay so this is the semop page from section 2 of the manual (system calls, duh), and it is literally the most unhinged little API in the whole IPC girlboss universe. atomic array of operations on a semaphore set. say that five times fast. π
β¨ LIBRARY β¨
Standard C Library (libc, -lc). she's a libc girlie, always has been. π
π¦ SYNOPSIS π¦
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int
semop(int semid, struct sembuf *array, size_t nops);
πΈ DESCRIPTION πΈ
okay so here's the tea. semop() is a system call that atomically does the whole array of operations that array points at, on the semaphore set that semid refers to. like, all of them or none of them, no in-between, no half-finished mess. nops is how long that array is.
every single operation is packed into a struct sembuf, which looks like this:
struct sembuf {
u_short sem_num; /* semaphore # */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */
};
so for each element in array, the sem_op and sem_flg decide what happens to semaphore number sem_num in the set. and you can OR the values SEM_UNDO and IPC_NOWAIT into that sem_flg member to change up the vibe of the operation. β¨
what actually goes down depends on sem_op's value, and it goes a lil something like this:
- when
sem_opis positive and the process has alter permission, the semaphore's value gets incremented bysem_op's value. and ifSEM_UNDOwas specified, the semaphore's adjust on exit value gets decremented bysem_op's value. a positivesem_opgenerally means the process is releasing a resource that's associated with the semaphore. bye bestie, u did ur job π - when
sem_opis negative and the process has alter permission, what happens depends on the semaphore's current value, and it's giving choose-your-own-adventure:- if the current value of the semaphore is greater than or equal to the absolute value of
sem_op, then the value gets decremented by the absolute value ofsem_op. and ifSEM_UNDOwas specified, the semaphore's adjust on exit value gets incremented by the absolute value ofsem_op. - if the current value of the semaphore is less than the absolute value of
sem_op, one of these things happens instead:- if
IPC_NOWAITwas specified, thensemop()returns immediately with a return value ofEAGAIN. not waiting, not today. π - otherwise, the calling process gets put to sleep until one of these conditions is satisfied:
- some other process removes the semaphore with the
IPC_RMIDoption ofsemctl(2). in this case,semop()returns immediately with a return value ofEIDRM. - the process receives a signal that is to be caught. in this case, the process will resume execution as defined by
sigaction(2). - the semaphore's value is greater than or equal to the absolute value of
sem_op. when this condition becomes true, the semaphore's value is decremented by the absolute value ofsem_op, and the semaphore's adjust on exit value is incremented by the absolute value ofsem_op.
- some other process removes the semaphore with the
- if
- if the current value of the semaphore is greater than or equal to the absolute value of
a negative value for sem_op generally means a process is waiting for a resource to become available. she's patient. she's waiting. she's in her waiting era. π§
- when
sem_opis zero and the process has read permission, one of these will occur:- if the current value of the semaphore is equal to zero, then
semop()can return immediately. slay. - if
IPC_NOWAITwas specified, thensemop()returns immediately with a return value ofEAGAIN. - otherwise, the calling process gets put to sleep until one of these conditions is satisfied:
- some other process removes the semaphore with the
IPC_RMIDoption ofsemctl(2). in this case,semop()returns immediately with a return value ofEIDRM. - the process receives a signal that is to be caught. in this case, the process will resume execution as defined by
sigaction(2). - the semaphore's value becomes zero.
- some other process removes the semaphore with the
- if the current value of the semaphore is equal to zero, then
for every semaphore a process has in use, the kernel keeps an "adjust on exit" value, which is the thing we kept hinting at earlier like a lil cliffhanger. when a process exits β voluntarily or involuntarily, doesn't matter, no take-backs β the adjust on exit value for each semaphore gets added to that semaphore's value. this is how you make sure a resource gets released even if a process just. dies. mid-thought. no cap, it's the safety net of the whole system. π«Ά
π RETURN VALUES π
semop() returns the value 0 if it succeeded. otherwise, the value -1 is returned, and the global variable errno gets set to tell you which error it was. iconic behaviour honestly.
π₯ ERRORS π₯
the semop() system call will fail if:
[EINVAL]β no semaphore set corresponds tosemid, or the process would exceed the system-defined limit for the number of per-processSEM_UNDOstructures.[EACCES]β permission denied, because the operation and the mode of the semaphore set don't match up. mismatch energy. π[EAGAIN]β the semaphore's value would have resulted in the process being put to sleep, andIPC_NOWAITwas specified.[E2BIG]β too many operations were specified.[SEMOPM][EFBIG]βsem_numwas not in the range of valid semaphores for the set.[EIDRM]β the semaphore set was removed from the system.[EINTR]β thesemop()system call was interrupted by a signal.[ENOSPC]β the systemSEM_UNDOpool[SEMMNU]is full.[ERANGE]β the requested operation would cause either the semaphore's current value[SEMVMX]or its adjust on exit value[SEMAEM]to exceed the system-imposed limits.
π SEE ALSO π
semctl(2), semget(2), sigaction(2)
π BUGS π
thesemop()system call may block waiting for memory even ifIPC_NOWAITwas specified. yes really. i know. she said "no wait" and then she waited. betrayal of the highest order. π
and that's the whole page, bestie. this one came from the Debian crew, dated September 22, 1995 β which is genuinely ancient, like older than most of the internet, and yet here she is, still doing atomic semaphore operations like the queen she is. go forth and synchronize responsibly. πβ¨π
π¦ 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