readv2: Note preadv2(..., RWF_NOWAIT) bug in BUGS section
[man-pages.git] / man7 / sem_overview.7
blob97ccf144c9b19794a89fb40c899631493c77bd0d
1 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH SEM_OVERVIEW 7 2020-06-09 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 sem_overview \- overview of POSIX semaphores
28 .SH DESCRIPTION
29 POSIX semaphores allow processes and threads to synchronize their actions.
30 .PP
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
34 .RB ( sem_post (3));
35 and decrement the semaphore value by one
36 .RB ( sem_wait (3)).
37 If the value of a semaphore is currently zero, then a
38 .BR sem_wait (3)
39 operation will block until the value becomes greater than zero.
40 .PP
41 POSIX semaphores come in two forms: named semaphores and
42 unnamed semaphores.
43 .TP
44 .B Named semaphores
45 A named semaphore is identified by a name of the form
46 .IR /somename ;
47 that is, a null-terminated string of up to
48 .BI NAME_MAX \-4
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
61 the same name to
62 .BR sem_open (3).
63 .IP
64 The
65 .BR sem_open (3)
66 function creates a new named semaphore or opens an existing
67 named semaphore.
68 After the semaphore has been opened, it can be operated on using
69 .BR sem_post (3)
70 and
71 .BR sem_wait (3).
72 When a process has finished using the semaphore, it can use
73 .BR sem_close (3)
74 to close the semaphore.
75 When all processes have finished using the semaphore,
76 it can be removed from the system using
77 .BR sem_unlink (3).
78 .TP
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" )
84 or processes (a
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
90 .BR shmget (2),
91 or a POSIX shared memory object built created using
92 .BR shm_open (3)).
93 .IP
94 Before being used, an unnamed semaphore must be initialized using
95 .BR sem_init (3).
96 It can then be operated on using
97 .BR sem_post (3)
98 and
99 .BR sem_wait (3).
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
103 .BR sem_destroy (3).
105 The remainder of this section describes some specific details
106 of the Linux implementation of POSIX semaphores.
107 .SS Versions
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.
113 .SS Persistence
114 POSIX named semaphores have kernel persistence:
115 if not removed by
116 .BR sem_unlink (3),
117 a semaphore will exist until the system is shut down.
118 .SS Linking
119 Programs using the POSIX semaphores API must be compiled with
120 .I cc \-pthread
121 to link against the real-time library,
122 .IR librt .
123 .SS Accessing named semaphores via the filesystem
124 On Linux, named semaphores are created in a virtual filesystem,
125 normally mounted under
126 .IR /dev/shm ,
127 with names of the form
128 .IR \fBsem.\fPsomename .
129 (This is the reason that semaphore names are limited to
130 .BI NAME_MAX \-4
131 rather than
132 .B NAME_MAX
133 characters.)
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.
137 .SH NOTES
138 System V semaphores
139 .RB ( semget (2),
140 .BR semop (2),
141 etc.) are an older semaphore API.
142 POSIX semaphores provide a simpler, and better designed interface than
143 System V semaphores;
144 on the other hand POSIX semaphores are less widely available
145 (especially on older systems) than System V semaphores.
146 .SH EXAMPLES
147 An example of the use of various POSIX semaphore functions is shown in
148 .BR sem_wait (3).
149 .SH SEE ALSO
150 .BR sem_close (3),
151 .BR sem_destroy (3),
152 .BR sem_getvalue (3),
153 .BR sem_init (3),
154 .BR sem_open (3),
155 .BR sem_post (3),
156 .BR sem_unlink (3),
157 .BR sem_wait (3),
158 .BR pthreads (7),
159 .BR shm_overview (7)