SEMCTL(3POSIX)
π semctl β XSI semaphore control operations π
okay bestie, so this page lives in the POSIX Programmer's Manual (yes, that's the 3POSIX manual, mwah). Heads up tho: the Linux implementation of this interface may differ, so if you're on Linux you gotta consult the corresponding Linux manual page for the deets on Linux behavior β or honestly the interface might not even be implemented on Linux at all. Not me finding that out the hard way π
β¨ NAME β¨
semctl β XSI semaphore control operations π
π¦ SYNOPSIS π¦
#include <sys/sem.h>
int semctl(int semid, int semnum, int cmd, ...);
πΈ DESCRIPTION πΈ
so semctl() operates on XSI semaphores (peep the Base Definitions volume of POSIX.1β2017, Section 4.17, Semaphore). and lowkey it's unspecified whether this function interoperates with the realtime interprocess communication facilities defined in Section 2.8, Realtime. like, nobody's promising that combo, fr fr.
semctl() gives you a whole menu of semaphore control operations, picked by cmd. the fourth argument is optional and depends on which operation you asked for. if it's required, it's of type union semun, which the application shall explicitly declare β you gotta do it yourself, bestie:
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
each operation shall be performed atomically β¨ periodt.
the following semaphore control operations (as specified by cmd) are executed with respect to the semaphore specified by semid and semnum. the level of permission required for each operation is shown with each command β see Section 2.7, XSI Interprocess Communication. the symbolic names for the values of cmd are defined in the <sys/sem.h> header:
GETVALβ returns the value ofsemval; see<sys/sem.h>. Requires read permission.SETVALβ sets the value ofsemvaltoarg.val, whereargis the value of the fourth argument tosemctl(). when this command is successfully executed, thesemadjvalue corresponding to the specified semaphore in all processes is cleared. also, thesem_ctimetimestamp shall be set to the current time, as described in Section 2.7.1, IPC General Description. Requires alter permission; see Section 2.7, XSI Interprocess Communication.GETPIDβ returns the value ofsempid. Requires read permission.GETNCNTβ returns the value ofsemncnt. Requires read permission.GETZCNTβ returns the value ofsemzcnt. Requires read permission.
the ones that operate on EVERY semval in the set π
the following values of cmd operate on each semval in the set of semaphores:
GETALLβ returns the value ofsemvalfor each semaphore in the semaphore set and places it into the array pointed to byarg.array, whereargis the fourth argument tosemctl(). Requires read permission.SETALLβ sets the value ofsemvalfor each semaphore in the semaphore set according to the array pointed to byarg.array, whereargis the fourth argument tosemctl(). when this command is successfully executed, thesemadjvalues corresponding to each specified semaphore in all processes are cleared. also, thesem_ctimetimestamp shall be set to the current time, as described in Section 2.7.1, IPC General Description. Requires alter permission.
and these ones too π
the following values of cmd are also available:
IPC_STATβ places the current value of each member of thesemid_dsdata structure associated withsemidinto the structure pointed to byarg.buf, whereargis the fourth argument tosemctl(). the contents of this structure are defined in<sys/sem.h>. Requires read permission.IPC_SETβ sets the value of the following members of thesemid_dsdata structure associated withsemidto the corresponding value found in the structure pointed to byarg.buf, whereargis the fourth argument tosemctl():
sem_perm.uid
sem_perm.gid
sem_perm.mode
the mode bits specified in Section 2.7.1, IPC General Description are copied into the corresponding bits of the sem_perm.mode associated with semid. the stored values of any other bits are unspecified. the sem_ctime timestamp shall be set to the current time, as described in Section 2.7.1, IPC General Description.
this command can only be executed by a process that has an effective user ID equal to either that of a process with appropriate privileges or to the value ofsem_perm.cuidorsem_perm.uidin thesemid_dsdata structure associated withsemid. so like... know your permissions, bestie π
IPC_RMIDβ removes the semaphore identifier specified bysemidfrom the system and destroys the set of semaphores andsemid_dsdata structure associated with it. this command can only be executed by a process that has an effective user ID equal to either that of a process with appropriate privileges or to the value ofsem_perm.cuidorsem_perm.uidin thesemid_dsdata structure associated withsemid.
π RETURN VALUE π
if it's successful, the value returned by semctl() depends on cmd like this:
GETVALβ the value ofsemval.GETPIDβ the value ofsempid.GETNCNTβ the value ofsemncnt.GETZCNTβ the value ofsemzcnt.- All others β
0.
otherwise, semctl() shall return -1 and set errno to indicate the error. sad girl hours π
π ERRORS π
the semctl() function shall fail if:
EACCESβ operation permission is denied to the calling process; see Section 2.7, XSI Interprocess Communication.EINVALβ the value ofsemidis not a valid semaphore identifier, or the value ofsemnumis less than0or greater than or equal tosem_nsems, or the value ofcmdis not a valid command.EPERMβ the argumentcmdis equal toIPC_RMIDorIPC_SETand the effective user ID of the calling process is not equal to that of a process with appropriate privileges and it is not equal to the value ofsem_perm.cuidorsem_perm.uidin the data structure associated withsemid.ERANGEβ the argumentcmdis equal toSETVALorSETALLand the value to whichsemvalis to be set is greater than the system-imposed maximum.
the following sections are informative.
π EXAMPLES π
refer to semop(). that's it. that's the section. go read that one bestie π«Ά
π§ APPLICATION USAGE π§
the fourth parameter in the SYNOPSIS section is now specified as "..." in order to avoid a clash with the ISO C standard when referring to the union semun (as defined in Issue 3) and for backwards-compatibility.
the POSIX Realtime Extension defines alternative interfaces for interprocess communication. application developers who need to use IPC should design their applications so that modules using the IPC routines described in Section 2.7, XSI Interprocess Communication can be easily modified to use the alternative interfaces. future-proofing, iconic behavior β¨
π RATIONALE π
none. not a single one. we simply vibe.
β FUTURE DIRECTIONS β
none. also nothing. the future is bright and empty here.
π SEE ALSO π
- Section 2.7, XSI Interprocess Communication
- Section 2.8, Realtime
semget()semop()sem_close()sem_destroy()sem_getvalue()sem_init()sem_open()sem_post()sem_trywait()sem_unlink()- The Base Definitions volume of POSIX.1β2017, Section 4.17, Semaphore,
<sys_sem.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! πβ¨ written under IEEE/The Open Group, 2017, page SEMCTL(3POSIX) β slay, go set some semaphores responsibly, ily π«Άπ
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms