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