SEMCTL(2)
π SEMCTL(2) β the manual page, but make it bestie
ok so this is a system call page from the System Calls Manual, and it's about semctl β the girl who runs the whole semaphore set. sit down, hydrate, we're doing IPC today π«Ά
β¨ NAME β¨
semctl β control operations on a semaphore set
π LIBRARY π
Standard C Library (libc, -lc)
π SYNOPSIS π
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int
semctl(int semid, int semnum, int cmd, ...);
πΈ DESCRIPTION πΈ
so basically semctl() does the thing that cmd tells it to do, on the semaphore set that semid points at. easy. a fourth argument, a union semun arg, is required for certain values of cmd β like, you literally cannot skip it for those, bestie, it's not optional-optional.
for the commands that use the arg argument, union semun has to be defined like this:
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
u_short *array; /* array for GETALL & SETALL */
};
non-portable software may define _WANT_SEMUN before including sys/sem.h to use the system definition of union semun. iykyk π
commands are performed as follows:
IPC_STATβ fetch the semaphore set'sstruct semid_dsand store it in the memory thatarg.bufpoints to. it's giving "let me see your receipts" πIPC_SETβ changes thesem_perm.uid,sem_perm.gid, andsem_perm.modemembers of the semaphore set'sstruct semid_dsso they match the ones in thestructthatarg.bufpoints to. the calling process's effective uid has to match eithersem_perm.uidorsem_perm.cuid, or it has to have superuser privileges. no permission, no edit, periodt.IPC_RMIDβ immediately removes the semaphore set from the system. the calling process's effective uid has to equal the semaphore set'ssem_perm.uidorsem_perm.cuid, or the process must have superuser privileges. gone. deleted. she left the chat πGETVALβ return the value of semaphore numbersemnum.SETVALβ set the value of semaphore numbersemnumtoarg.val. outstanding adjust on exit values for this semaphore in any process are cleared.GETPIDβ return the pid of the last process to perform an operation on semaphore numbersemnum.GETNCNTβ return the number of processes waiting for semaphore numbersemnum's value to become greater than its current value.GETZCNTβ return the number of processes waiting for semaphore numbersemnum's value to become0.GETALLβ fetch the value of all of the semaphores in the set into the array thatarg.arraypoints to.SETALLβ set the values of all of the semaphores in the set to the values in the array thatarg.arraypoints to. outstanding adjust on exit values for all semaphores in this set, in any process, are cleared.
and the struct semid_ds is defined as follows:
struct semid_ds {
struct ipc_perm sem_perm; /* operation permission struct */
u_short sem_nsems; /* number of sems in set */
time_t sem_otime; /* last operation time */
time_t sem_ctime; /* last change time */
/* Times measured in secs since */
/* 00:00:00 GMT, Jan. 1, 1970 */
};
π RETURN VALUES π
on success, when cmd is one of GETVAL, GETPID, GETNCNT or GETZCNT, semctl() returns the corresponding value π β otherwise, 0 is returned. on failure, -1 is returned, and errno is set to indicate the error. (rude of it, honestly.)
π ERRORS π
the semctl() system call will fail if:
[EINVAL]β no semaphore set corresponds tosemid. girl does not exist. π«₯[EINVAL]β thesemnumargument is not in the range of valid semaphores for the given semaphore set. wrong number bestie π[EPERM]β the calling process's effective uid does not match the uid of the semaphore set's owner or creator.[EACCES]β permission denied due to a mismatch between the operation and the mode of the semaphore set.[ERANGE]βSETVALorSETALLattempted to set a semaphore outside the allowable range[0 .. SEMVMX]. stay in your lane please π§
π₯ SEE ALSO π₯
π BUGS π
SETALL may update some semaphore elements before returning an error. so likeβ¦ it can half-do the thing and then bail. not iconic of it, but there it is π
and that's the whole page, bestie β brought to you by Debian, dated July 9, 2020. go forth and manage your semaphores responsibly, mwah πβ¨π
π¦ 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