1 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH SEM_OVERVIEW 7 2020-06-09 "Linux" "Linux Programmer's Manual"
27 sem_overview \- overview of POSIX semaphores
29 POSIX semaphores allow processes and threads to synchronize their actions.
31 A semaphore is an integer whose value is never allowed to fall below zero.
32 Two operations can be performed on semaphores:
33 increment the semaphore value by one
35 and decrement the semaphore value by one
37 If the value of a semaphore is currently zero, then a
39 operation will block until the value becomes greater than zero.
41 POSIX semaphores come in two forms: named semaphores and
45 A named semaphore is identified by a name of the form
47 that is, a null-terminated string of up to
49 (i.e., 251) characters consisting of an initial slash,
50 .\" glibc allows the initial slash to be omitted, and makes
51 .\" multiple initial slashes equivalent to a single slash.
52 .\" This differs from the implementation of POSIX message queues.
53 followed by one or more characters, none of which are slashes.
54 .\" glibc allows subdirectory components in the name, in which
55 .\" case the subdirectory tree must exist under /dev/shm, and
56 .\" the fist subdirectory component must exist as the name
57 .\" sem.name, and all of the subdirectory components must allow the
58 .\" required permissions if a user wants to create a semaphore
59 .\" object in a subdirectory.
60 Two processes can operate on the same named semaphore by passing
66 function creates a new named semaphore or opens an existing
68 After the semaphore has been opened, it can be operated on using
72 When a process has finished using the semaphore, it can use
74 to close the semaphore.
75 When all processes have finished using the semaphore,
76 it can be removed from the system using
79 .B Unnamed semaphores (memory-based semaphores)
80 An unnamed semaphore does not have a name.
81 Instead the semaphore is placed in a region of memory that
82 is shared between multiple threads (a
83 .IR "thread-shared semaphore" )
85 .IR "process-shared semaphore" ).
86 A thread-shared semaphore is placed in an area of memory shared
87 between the threads of a process, for example, a global variable.
88 A process-shared semaphore must be placed in a shared memory region
89 (e.g., a System V shared memory segment created using
91 or a POSIX shared memory object built created using
94 Before being used, an unnamed semaphore must be initialized using
96 It can then be operated on using
100 When the semaphore is no longer required,
101 and before the memory in which it is located is deallocated,
102 the semaphore should be destroyed using
105 The remainder of this section describes some specific details
106 of the Linux implementation of POSIX semaphores.
108 Prior to kernel 2.6, Linux supported only unnamed,
109 thread-shared semaphores.
110 On a system with Linux 2.6 and a glibc that provides the NPTL
111 threading implementation,
112 a complete implementation of POSIX semaphores is provided.
114 POSIX named semaphores have kernel persistence:
117 a semaphore will exist until the system is shut down.
119 Programs using the POSIX semaphores API must be compiled with
121 to link against the real-time library,
123 .SS Accessing named semaphores via the filesystem
124 On Linux, named semaphores are created in a virtual filesystem,
125 normally mounted under
127 with names of the form
128 .IR \fBsem.\fPsomename .
129 (This is the reason that semaphore names are limited to
135 Since Linux 2.6.19, ACLs can be placed on files under this directory,
136 to control object permissions on a per-user and per-group basis.
141 etc.) are an older semaphore API.
142 POSIX semaphores provide a simpler, and better designed interface than
144 on the other hand POSIX semaphores are less widely available
145 (especially on older systems) than System V semaphores.
147 An example of the use of various POSIX semaphore functions is shown in
152 .BR sem_getvalue (3),