sem_overview(7)
๐ sem_overview โ POSIX semaphores, explained by your favorite Unix bestie ๐
okay so, quick intro: this is sem_overview(7), and it's basically the vibe check on POSIX semaphores. No cap. Let's go. ๐ฆ
โจ NAME โจ
sem_overview โ the whole overview of POSIX semaphores, bestie. That's the assignment. ๐
๐ธ DESCRIPTION ๐ธ
So POSIX semaphores let processes AND threads sync up their actions with each other. Like, "you go, then I go" energy. ๐ซถ
A semaphore is an integer whose value is never allowed to go below zero. Periodt. And you can do exactly two operations on a semaphore:
- increment the semaphore value by one โ that's
sem_post(3)๐ - decrement the semaphore value by one โ that's
sem_wait(3)โจ
And here's the iconic part: if the semaphore's value is currently zero, then a sem_wait(3) operation will block until the value becomes greater than zero. It just sits there. Waiting. Main character patience. ๐ญ
POSIX semaphores come in two forms: named semaphores and unnamed semaphores. Two flavors, both slay.
๐ Named semaphores ๐
A named semaphore is identified by a name of the form /somename โ and yes, that means a null-terminated string of up to NAME_MAX-4 (i.e., 251) characters, consisting of an initial slash, followed by one or more characters, and none of which are slashes. That last bit is important, don't come for me.
Two processes can operate on the same named semaphore by passing the same name to sem_open(3). Shared name = shared semaphore. It's giving teamwork. ๐ค
The sem_open(3) function creates a new named semaphore or opens an existing named semaphore. Once it's opened, you can operate on it using sem_post(3) and sem_wait(3). When a process is done using the semaphore, it can use sem_close(3) to close it. And when all processes are finished with it, it can be removed from the system using sem_unlink(3). Cleanup era. ๐งนโจ
๐ฆ Unnamed semaphores (memory-based semaphores) ๐ฆ
An unnamed semaphore does not have a name. Correct, that's literally why. ๐ Instead, the semaphore gets placed in a region of memory that's shared between multiple threads (a thread-shared semaphore) or processes (a process-shared semaphore).
- A thread-shared semaphore lives in an area of memory shared between the threads of a process โ for example, a global variable.
- A process-shared semaphore must be placed in a shared memory region (e.g., a System V shared memory segment created using
shmget(2), or a POSIX shared memory object built created usingshm_open(3)).
Before being used, an unnamed semaphore must be initialized using sem_init(3). After that it can be operated on using sem_post(3) and sem_wait(3). And when the semaphore is no longer required โ and before the memory in which it's located is deallocated โ the semaphore should be destroyed using sem_destroy(3). Order matters, bestie. Don't skip that step. โ ๏ธ
The remainder of this section describes some specific details of the Linux implementation of POSIX semaphores. Linux girlies, this one's for us. ๐ง
โญ Versions โญ
Before Linux 2.6, Linux supported only unnamed, thread-shared semaphores. On a system with Linux 2.6 and a glibc that provides the NPTL threading implementation, a complete implementation of POSIX semaphores is provided. Glow up achieved. ๐
โญ Persistence โญ
POSIX named semaphores have kernel persistence: if not removed by sem_unlink(3), a semaphore will exist until the system is shut down. It does not simply vanish when you log out. It stays. Iconic, honestly. ๐
โญ Linking โญ
Programs using the POSIX semaphores API must be compiled with cc -pthread to link against the real-time library, librt. Write that down. โ๏ธ
โญ Accessing named semaphores via the filesystem โญ
On Linux, named semaphores are created in a virtual filesystem, normally mounted under /dev/shm, with names of the form sem.somename. (And this is the reason that semaphore names are limited to NAME_MAX-4 rather than NAME_MAX characters. Mystery solved, iykyk.) ๐
Since Linux 2.6.19, ACLs can be placed on files under this directory, to control object permissions on a per-user and per-group basis. Fancy permissions, we love to see it. ๐
๐ NOTES ๐
System V semaphores (semget(2), semop(2), etc.) are an older semaphore API. POSIX semaphores provide a simpler, and better designed interface than System V semaphores โ lowkey the upgrade. โจ
On the other hand: POSIX semaphores are less widely available (especially on older systems) than System V semaphores. So like, check the room before you assume, fr fr.
๐ EXAMPLES ๐
An example of the use of various POSIX semaphore functions is shown in sem_wait(3). Go read it, bestie, it eats. ๐
๐ SEE ALSO ๐
sem_close(3)sem_destroy(3)sem_getvalue(3)sem_init(3)sem_open(3)sem_post(3)sem_unlink(3)sem_wait(3)pthreads(7)shm_overview(7)
and that's the whole page!! mwah ๐ This one came from the Linux man-pages, version 6.20, dated 2026-02-08, so if you ever need to cite your sources, that's the receipt. Stay synchronized, bestie ๐ฆโจ๐
๐ฆ where this page comes from
- 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 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms