2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
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.
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .TH SEM_OVERVIEW 7 2006-03-25 "Linux 2.6.16" "Linux Programmer's Manual"
25 sem_overview \- Overview of POSIX semaphores
27 POSIX semaphores allow processes and threads to synchronise their actions.
29 A semaphore is an integer whose value is never allowed to fall below zero.
30 Two operations can be performed on semaphores:
31 increment the semaphore value by one
33 and decrement the semaphore value by one
35 If the value of a semaphore is currently zero, then a
37 operation will block until the value becomes greater than zero.
39 POSIX semaphores come in two forms: named semaphores and
42 A named semaphore is identified by a name of the form
44 Two processes can operate on the same named semaphore by passing
50 function creates a new named semaphore or opens an existing
52 After the semaphore has been opened, it can be operated on using
56 When a process has finished using the semaphore, it can use
58 to close the semaphore.
59 When all processes have finished using the semaphore,
60 it can be removed from the system using
62 .SS Unnamed semaphores (memory-based semaphores)
63 An unnamed semaphore does not have a name.
64 Instead the semaphore is placed in a region of memory that
65 is shared between multiple threads (a
66 .IR "thread-shared semaphore" )
68 .IR "process-shared semaphore" ).
69 A thread-shared semaphore is placed in an area of memory shared
70 between by the threads of a process, for example, a global variable.
71 A process-shared semaphore must be placed in a shared memory region
72 (e.g., a System V shared memory segment created using
74 or a POSIX shared memory object built created using
77 Before being used, an unnamed semaphore must be initialised using
79 It can then be operated on using
83 When the semaphore is no longer required,
84 and before the memory in which it is located is deallocated,
85 the semaphore should be destroyed using
87 .SH LINUX SPECIFIC DETAILS
89 Prior to kernel 2.6, Linux only supported unnamed,
90 thread-shared semaphores.
91 On a system with Linux 2.6 and a glibc that provides the NPTL
92 threading implementation,
93 a complete implementation of POSIX semaphores is provided.
95 POSIX named semaphores have kernel persistence:
98 a semaphore will exist until the system is shut down.
100 Programs using the POSIX semaphores API must be compiled with
102 to link against the real-time library,
104 .SS Accessing named semaphores via the file system
105 On Linux, named semaphores are created in a virtual file system,
106 normally mounted under
108 with names of the form
116 etc.) are an older semaphore API.
117 POSIX semaphores provide a simpler, and better designed interface than
119 on the other hand POSIX semaphores are less widely available
120 (especially on older systems) than System V semaphores.
122 An example of the use of various POSIX semaphore functions is shown in
128 .BR sem_getvalue (3),