OSDN Git Service

(split) LDP_man-pages: update original to v3.34.
[linuxjm/LDP_man-pages.git] / original / 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 2010-05-22 "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 that is, a null-terminated string of up to
49 .BI NAME_MAX \-4
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
62 the same name to
63 .BR sem_open (3).
64
65 The
66 .BR sem_open (3)
67 function creates a new named semaphore or opens an existing
68 named semaphore.
69 After the semaphore has been opened, it can be operated on using
70 .BR sem_post (3)
71 and
72 .BR sem_wait (3).
73 When a process has finished using the semaphore, it can use
74 .BR sem_close (3)
75 to close the semaphore.
76 When all processes have finished using the semaphore,
77 it can be removed from the system using
78 .BR sem_unlink (3).
79 .TP
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" )
85 or processes (a
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
91 .BR shmget (2),
92 or a POSIX shared memory object built created using
93 .BR shm_open (3)).
94
95 Before being used, an unnamed semaphore must be initialized using
96 .BR sem_init (3).
97 It can then be operated on using
98 .BR sem_post (3)
99 and
100 .BR sem_wait (3).
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
104 .BR sem_destroy (3).
105 .PP
106 The remainder of this section describes some specific details
107 of the Linux implementation of POSIX semaphores.
108 .SS Versions
109 Prior to kernel 2.6, Linux only supported 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.
114 .SS Persistence
115 POSIX named semaphores have kernel persistence:
116 if not removed by
117 .BR sem_unlink (3),
118 a semaphore will exist until the system is shut down.
119 .SS Linking
120 Programs using the POSIX semaphores API must be compiled with
121 .I cc \-lrt
122 to link against the real-time library,
123 .IR librt .
124 .SS Accessing named semaphores via the file system
125 On Linux, named semaphores are created in a virtual file system,
126 normally mounted under
127 .IR /dev/shm ,
128 with names of the form
129 .IR \fBsem.\fPsomename .
130 (This is the reason that semaphore names are limited to
131 .BI NAME_MAX \-4
132 rather than
133 .B NAME_MAX
134 characters.)
135
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.
138 .SH "CONFORMING TO"
139 POSIX.1-2001.
140 .SH NOTES
141 System V semaphores
142 .RB ( semget (2),
143 .BR semop (2),
144 etc.) are an older semaphore API.
145 POSIX semaphores provide a simpler, and better designed interface than
146 System V semaphores;
147 on the other hand POSIX semaphores are less widely available
148 (especially on older systems) than System V semaphores.
149 .SH EXAMPLE
150 An example of the use of various POSIX semaphore functions is shown in
151 .BR sem_wait (3).
152 .SH "SEE ALSO"
153 .BR sem_close (3),
154 .BR sem_destroy (3),
155 .BR sem_getvalue (3),
156 .BR sem_init (3),
157 .BR sem_open (3),
158 .BR sem_post (3),
159 .BR sem_unlink (3),
160 .BR sem_wait (3),
161 .BR pthreads (7)