2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH SEM_OVERVIEW 7 2006-06-15 "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 Two processes can operate on the same named semaphore by passing
54 function creates a new named semaphore or opens an existing
56 After the semaphore has been opened, it can be operated on using
60 When a process has finished using the semaphore, it can use
62 to close the semaphore.
63 When all processes have finished using the semaphore,
64 it can be removed from the system using
67 .B Unnamed semaphores (memory-based semaphores)
68 An unnamed semaphore does not have a name.
69 Instead the semaphore is placed in a region of memory that
70 is shared between multiple threads (a
71 .IR "thread-shared semaphore" )
73 .IR "process-shared semaphore" ).
74 A thread-shared semaphore is placed in an area of memory shared
75 between by the threads of a process, for example, a global variable.
76 A process-shared semaphore must be placed in a shared memory region
77 (e.g., a System V shared memory segment created using
79 or a POSIX shared memory object built created using
82 Before being used, an unnamed semaphore must be initialized using
84 It can then be operated on using
88 When the semaphore is no longer required,
89 and before the memory in which it is located is deallocated,
90 the semaphore should be destroyed using
93 The remainder of this section describes some specific details
94 of the Linux implementation of POSIX semaphores.
96 Prior to kernel 2.6, Linux only supported unnamed,
97 thread-shared semaphores.
98 On a system with Linux 2.6 and a glibc that provides the NPTL
99 threading implementation,
100 a complete implementation of POSIX semaphores is provided.
102 POSIX named semaphores have kernel persistence:
105 a semaphore will exist until the system is shut down.
107 Programs using the POSIX semaphores API must be compiled with
109 to link against the real-time library,
111 .SS Accessing named semaphores via the file system
112 On Linux, named semaphores are created in a virtual file system,
113 normally mounted under
115 with names of the form
118 Since Linux 2.6.19, ACLs can be placed on files under this directory,
119 to control object permissions on a per-user and per-group basis.
126 etc.) are an older semaphore API.
127 POSIX semaphores provide a simpler, and better designed interface than
129 on the other hand POSIX semaphores are less widely available
130 (especially on older systems) than System V semaphores.
132 An example of the use of various POSIX semaphore functions is shown in
138 .BR sem_getvalue (3),