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 using shm_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 ๐Ÿ‘‘


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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms