SEM_POST(3P)
π SEM_POST(3P) β the POSIX girlies' semaphore unlock π
okay bestie, so this one's from the POSIX Programmer's Manual β that's the manual name, we're in SEM_POST(3P) territory, and it's genuinely one of the most iconic little functions in the whole concurrency saga. β¨
β οΈ real talk first: this page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the actual Linux behavior, no cap), and honestly the interface may not be implemented on Linux at all. vibe check accordingly. π«Ά
πΈ NAME πΈ
sem_post β unlock a semaphore. that's it. that's the whole job. unlock a semaphore. π
β¨ SYNOPSIS β¨
#include <semaphore.h>
int sem_post(sem_t *sem);
π¦ DESCRIPTION π¦
so the sem_post() function shall unlock the semaphore referenced by sem by performing a semaphore unlock operation on that semaphore. slay.
now here's the tea, and it depends entirely on what the semaphore value looks like after the op:
- If the semaphore value resulting from this operation is positive, then literally no threads were blocked waiting for the semaphore to become unlocked β so the semaphore value is simply incremented. easy. no drama. π
- If the value of the semaphore resulting from this operation is zero, then one of the threads blocked waiting for the semaphore shall be allowed to return successfully from its call to
sem_wait(). β¨
and okay, which thread gets unblocked? that's where it gets spicy:
- If the Process Scheduling option is supported, the thread to be unblocked shall be chosen in a manner appropriate to the scheduling policies and parameters in effect for the blocked threads.
- In the case of the schedulers
SCHED_FIFOandSCHED_RR, the highest priority waiting thread shall be unblocked, and if there is more than one highest priority thread blocked waiting for the semaphore, then the highest priority thread that has been waiting the longest shall be unblocked. periodt, first come first served among the VIPs. π - If the Process Scheduling option is not defined, the choice of a thread to unblock is unspecified. π (i.e. don't @ me about which one comes back.)
and if the Process Sporadic Server option is supported, and the scheduling policy is SCHED_SPORADIC, the semantics are as per SCHED_FIFO above. π
the glow-up detail: sem_post() shall be async-signal-safe and may be invoked from a signal-catching function. yes bestie, you can call it from a signal handler. iconic behavior fr fr. π₯
π RETURN VALUE π
- If successful,
sem_post()shall return zero π - Otherwise, the function shall return
-1and seterrnoto indicate the error π
π ERRORS π
The sem_post() function may fail if:
EINVALβ thesemargument does not refer to a valid semaphore. not a valid semaphore bestie, we can't work with that. π
the following sections are informative, so like, they're bonus content. π
π EXAMPLES π
See sem_timedwait(). that's the whole section. go look, she's over there. π«
π APPLICATION USAGE π
None.
π§ RATIONALE π§
None.
β FUTURE DIRECTIONS β
None.
π SEE ALSO π
semctl()semget()semop()sem_timedwait()sem_trywait()
also, from the Base Definitions volume of POSIX.1β2017: Section 4.12, Memory Synchronization, and <semaphore.h>. go read those, they're giving essential reading. π
π 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 π«Ά this came to you courtesy of IEEE/The Open Group, 2017 β that's the source, that's the date, that's the footer, we are not copying it verbatim because we said what we said. π β¨ go unlock your semaphores responsibly. mwah. π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms