]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/sem_overview.7
Put SEE ALSO section into alphabetical order.
[thirdparty/man-pages.git] / man7 / sem_overview.7
1 '\" t
2 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\"
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .TH SEM_OVERVIEW 7 2008-06-15 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 sem_overview \- Overview of POSIX semaphores
29 .SH DESCRIPTION
30 POSIX semaphores allow processes and threads to synchronize their actions.
31
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
35 .RB ( sem_post (3));
36 and decrement the semaphore value by one
37 .RB ( sem_wait (3)).
38 If the value of a semaphore is currently zero, then a
39 .BR sem_wait (3)
40 operation will block until the value becomes greater than zero.
41
42 POSIX semaphores come in two forms: named semaphores and
43 unnamed semaphores.
44 .TP
45 .B Named semaphores
46 A named semaphore is identified by a name of the form
47 .IR /somename .
48 Two processes can operate on the same named semaphore by passing
49 the same name to
50 .BR sem_open (3).
51
52 The
53 .BR sem_open (3)
54 function creates a new named semaphore or opens an existing
55 named semaphore.
56 After the semaphore has been opened, it can be operated on using
57 .BR sem_post (3)
58 and
59 .BR sem_wait (3).
60 When a process has finished using the semaphore, it can use
61 .BR sem_close (3)
62 to close the semaphore.
63 When all processes have finished using the semaphore,
64 it can be removed from the system using
65 .BR sem_unlink (3).
66 .TP
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" )
72 or processes (a
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
78 .BR semget (2),
79 or a POSIX shared memory object built created using
80 .BR shm_open (3)).
81
82 Before being used, an unnamed semaphore must be initialized using
83 .BR sem_init (3).
84 It can then be operated on using
85 .BR sem_post (3)
86 and
87 .BR sem_wait (3).
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
91 .BR sem_destroy (3).
92 .PP
93 The remainder of this section describes some specific details
94 of the Linux implementation of POSIX semaphores.
95 .SS Versions
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.
101 .SS Persistence
102 POSIX named semaphores have kernel persistence:
103 if not removed by
104 .BR sem_unlink (3),
105 a semaphore will exist until the system is shut down.
106 .SS Linking
107 Programs using the POSIX semaphores API must be compiled with
108 .I cc \-lrt
109 to link against the real-time library,
110 .IR librt .
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
114 .IR /dev/shm ,
115 with names of the form
116 .IR \fBsem.\fPname .
117
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.
120 .SH "CONFORMING TO"
121 POSIX.1-2001.
122 .SH NOTES
123 System V semaphores
124 .RB ( semget (2),
125 .BR semop (2),
126 etc.) are an older semaphore API.
127 POSIX semaphores provide a simpler, and better designed interface than
128 System V semaphores;
129 on the other hand POSIX semaphores are less widely available
130 (especially on older systems) than System V semaphores.
131 .SH EXAMPLE
132 An example of the use of various POSIX semaphore functions is shown in
133 .BR sem_wait (3).
134 .SH "SEE ALSO"
135 .BR sem_close (3),
136 .BR sem_destroy (3),
137 .BR sem_getvalue (3),
138 .BR sem_init (3),
139 .BR sem_open (3),
140 .BR sem_post (3),
141 .BR sem_unlink (3),
142 .BR sem_wait (3),
143 .BR pthreads (7)