Print this page
4829 sema_init(3C) botches the arguments in an example
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/man/man3c/semaphore.3c
+++ new/usr/src/man/man3c/semaphore.3c
1 1 '\" te
2 2 .\" Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
3 3 .\" Portions Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
4 4 .\" Portions Copyright (c) 1995 IEEE. All Rights Reserved
5 5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
6 6 .\" http://www.opengroup.org/bookstore/.
7 7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
8 8 .\" This notice shall appear on any product containing this material.
9 9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
10 10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
11 11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 12 .TH SEMAPHORE 3C "Feb 5, 2008"
13 13 .SH NAME
14 14 semaphore, sema_init, sema_destroy, sema_wait, sema_trywait, sema_post \-
15 15 semaphores
16 16 .SH SYNOPSIS
17 17 .LP
18 18 .nf
19 19 cc [ \fIflag\fR... ] \fIfile\fR... -lthread -lc [ \fIlibrary\fR... ]
20 20 #include <synch.h>
21 21
22 22 \fBint\fR \fBsema_init\fR(\fBsema_t *\fR\fIsp\fR, \fBunsigned int\fR \fIcount\fR, \fBint\fR \fItype\fR,
23 23 \fBvoid *\fR \fIarg\fR);
24 24 .fi
25 25
26 26 .LP
27 27 .nf
28 28 \fBint\fR \fBsema_destroy\fR(\fBsema_t *\fR\fIsp\fR);
29 29 .fi
30 30
31 31 .LP
32 32 .nf
33 33 \fBint\fR \fBsema_wait\fR(\fBsema_t *\fR\fIsp\fR);
34 34 .fi
35 35
36 36 .LP
37 37 .nf
38 38 \fBint\fR \fBsema_trywait\fR(\fBsema_t *\fR\fIsp\fR);
39 39 .fi
40 40
41 41 .LP
42 42 .nf
43 43 \fBint\fR \fBsema_post\fR(\fBsema_t *\fR\fIsp\fR);
44 44 .fi
45 45
46 46 .SH DESCRIPTION
47 47 .sp
48 48 .LP
49 49 A semaphore is a non-negative integer count and is generally used to coordinate
50 50 access to resources. The initial semaphore count is set to the number of free
51 51 resources, then threads slowly increment and decrement the count as resources
52 52 are added and removed. If the semaphore count drops to 0, which means no
53 53 available resources, threads attempting to decrement the semaphore will block
54 54 until the count is greater than 0.
55 55 .sp
56 56 .LP
57 57 Semaphores can synchronize threads in this process and other processes if they
58 58 are allocated in writable memory and shared among the cooperating processes
59 59 (see \fBmmap\fR(2)), and have been initialized for this purpose.
60 60 .sp
61 61 .LP
62 62 Semaphores must be initialized before use; semaphores pointed to by \fIsp\fR to
63 63 \fIcount\fR are initialized by \fBsema_init()\fR. The \fItype\fR argument can
64 64 assign several different types of behavior to a semaphore. No current type uses
65 65 \fIarg\fR, although it may be used in the future.
66 66 .sp
67 67 .LP
68 68 The \fItype\fR argument may be one of the following:
69 69 .sp
70 70 .ne 2
71 71 .na
72 72 \fB\fBUSYNC_PROCESS\fR \fR
73 73 .ad
74 74 .RS 18n
75 75 The semaphore can synchronize threads in this process and other processes.
76 76 Initializing the semaphore should be done by only one process. A semaphore
77 77 initialized with this type must be allocated in memory shared between
78 78 processes, either in Sys V shared memory (see \fBshmop\fR(2)), or in memory
79 79 mapped to a file (see \fBmmap\fR(2)). It is illegal to initialize the object
80 80 this way and not allocate it in such shared memory. \fIarg\fR is ignored.
81 81 .RE
82 82
83 83 .sp
84 84 .ne 2
85 85 .na
86 86 \fB\fBUSYNC_THREAD\fR \fR
87 87 .ad
88 88 .RS 18n
89 89 The semaphore can synchronize threads only in this process. The \fIarg\fR
90 90 argument is ignored. \fBUSYNC_THREAD\fR does not support multiple mappings to
91 91 the same logical synch object. If you need to \fBmmap()\fR a synch object to
92 92 different locations within the same address space, then the synch object should
93 93 be initialized as a shared object \fBUSYNC_PROCESS\fR for Solaris threads and
94 94 \fBPTHREAD_PROCESS_PRIVATE\fR for POSIX threads.
95 95 .RE
96 96
97 97 .sp
98 98 .LP
↓ open down ↓ |
98 lines elided |
↑ open up ↑ |
99 99 A semaphore must not be simultaneously initialized by multiple threads, nor
100 100 re-initialized while in use by other threads.
101 101 .sp
102 102 .LP
103 103 Default semaphore initialization (intra-process):
104 104 .sp
105 105 .in +2
106 106 .nf
107 107 sema_t sp;
108 108 int count = 1;
109 -sema_init(&sp, count, NULL, NULL);
109 +sema_init(&sp, count, 0, NULL);
110 110 .fi
111 111 .in -2
112 112
113 113 .sp
114 114 .LP
115 115 or
116 116 .sp
117 117 .in +2
118 118 .nf
119 119 sema_init(&sp, count, USYNC_THREAD, NULL);
120 120 .fi
121 121 .in -2
122 122
123 123 .sp
124 124 .LP
125 125 Customized semaphore initialization (inter-process):
126 126 .sp
127 127 .in +2
128 128 .nf
129 129 \fBsema_t sp;
130 130 int count = 1;
131 131 sema_init(&sp, count, USYNC_PROCESS, NULL);\fR
132 132 .fi
133 133 .in -2
134 134
135 135 .sp
136 136 .LP
137 137 The \fBsema_destroy()\fR function destroys any state related to the semaphore
138 138 pointed to by \fIsp\fR. The semaphore storage space is not released.
139 139 .sp
140 140 .LP
141 141 The \fBsema_wait()\fR function blocks the calling thread until the semaphore
142 142 count pointed to by \fIsp\fR is greater than 0, and then it atomically
143 143 decrements the count.
144 144 .sp
145 145 .LP
146 146 The \fBsema_trywait()\fR function atomically decrements the semaphore count
147 147 pointed to by \fIsp\fR, if the count is greater than 0; otherwise, it returns
148 148 an error.
149 149 .sp
150 150 .LP
151 151 The \fBsema_post()\fR function atomically increments the semaphore count
152 152 pointed to by \fIsp\fR. If there are any threads blocked on the semaphore, one
153 153 will be unblocked.
154 154 .sp
155 155 .LP
156 156 The semaphore functionality described on this man page is for the Solaris
157 157 threads implementation. For the POSIX-conforming semaphore interface
158 158 documentation, see \fBsem_close\fR(3C), \fBsem_destroy\fR(3C),
159 159 \fBsem_getvalue\fR(3C), \fBsem_init\fR(3C), \fBsem_open\fR(3C),
160 160 \fBsem_post\fR(3C), \fBsem_unlink\fR(3C), and \fBsem_wait\fR(3C).
161 161 .SH RETURN VALUES
162 162 .sp
163 163 .LP
164 164 Upon successful completion, \fB0\fR is returned; otherwise, a non-zero value
165 165 indicates an error.
166 166 .SH ERRORS
167 167 .sp
168 168 .LP
169 169 These functions will fail if:
170 170 .sp
171 171 .ne 2
172 172 .na
173 173 \fB\fBEINVAL\fR \fR
174 174 .ad
175 175 .RS 11n
176 176 The \fIsp\fR argument does not refer to a valid semaphore.
177 177 .RE
178 178
179 179 .sp
180 180 .ne 2
181 181 .na
182 182 \fB\fBEFAULT\fR \fR
183 183 .ad
184 184 .RS 11n
185 185 Either the \fIsp\fR or \fIarg\fR argument points to an illegal address.
186 186 .RE
187 187
188 188 .sp
189 189 .LP
190 190 The \fBsema_wait()\fR function will fail if:
191 191 .sp
192 192 .ne 2
193 193 .na
194 194 \fB\fBEINTR\fR \fR
195 195 .ad
196 196 .RS 10n
197 197 The wait was interrupted by a signal or \fBfork()\fR.
198 198 .RE
199 199
200 200 .sp
201 201 .LP
202 202 The \fBsema_trywait()\fR function will fail if:
203 203 .sp
204 204 .ne 2
205 205 .na
206 206 \fB\fBEBUSY\fR \fR
207 207 .ad
208 208 .RS 10n
209 209 The semaphore pointed to by \fIsp\fR has a 0 count.
210 210 .RE
211 211
212 212 .sp
213 213 .LP
214 214 The \fBsema_post()\fR function will fail if:
215 215 .sp
216 216 .ne 2
217 217 .na
218 218 \fB\fBEOVERFLOW\fR \fR
219 219 .ad
220 220 .RS 14n
221 221 The semaphore value pointed to by \fIsp\fR exceeds \fBSEM_VALUE_MAX\fR.
222 222 .RE
223 223
224 224 .SH EXAMPLES
225 225 .LP
226 226 \fBExample 1 \fRThe customer waiting-line in a bank is analogous to the
227 227 synchronization scheme of a semaphore using \fBsema_wait()\fR and
228 228 \fBsema_trywait()\fR:
229 229 .sp
230 230 .in +2
231 231 .nf
232 232 /* cc [ flag \|.\|.\|. ] file \|.\|.\|. -lthread [ library \|.\|.\|. ] */
233 233 #include <errno.h>
234 234 #define TELLERS 10
235 235 sema_t tellers; /* semaphore */
236 236 int banking_hours(), deposit_withdrawal;
237 237 void*customer(), do_business(), skip_banking_today();
238 238 \&.\|.\|.
239 239
240 240 sema_init(&tellers, TELLERS, USYNC_THREAD, NULL);
241 241 /* 10 tellers available */
242 242 while(banking_hours())
243 243 pthread_create(NULL, NULL, customer, deposit_withdrawal);
244 244 \&.\|.\|.
245 245
246 246 void *
247 247 customer(int deposit_withdrawal)
248 248 {
249 249 int this_customer, in_a_hurry = 50;
250 250 this_customer = rand() % 100;
251 251
252 252 if (this_customer == in_a_hurry) {
253 253 if (sema_trywait(&tellers) != 0)
254 254 if (errno == EBUSY){ /* no teller available */
255 255 skip_banking_today(this_customer);
256 256 return;
257 257 } /* else go immediately to available teller and
258 258 decrement tellers */
259 259 }
260 260 else
261 261 sema_wait(&tellers); /* wait for next teller, then
262 262 proceed, and decrement tellers */
263 263
264 264 do_business(deposit_withdrawal);
265 265 sema_post(&tellers); /* increment tellers; this_customer's
266 266 teller is now available */
267 267 }
268 268 .fi
269 269 .in -2
270 270
271 271 .SH ATTRIBUTES
272 272 .sp
273 273 .LP
274 274 See \fBattributes\fR(5) for descriptions of the following attributes:
275 275 .sp
276 276
277 277 .sp
278 278 .TS
279 279 box;
280 280 c | c
281 281 l | l .
282 282 ATTRIBUTE TYPE ATTRIBUTE VALUE
283 283 _
284 284 MT-Level Async-Signal-Safe
285 285 .TE
286 286
287 287 .SH SEE ALSO
288 288 .sp
289 289 .LP
290 290 \fBmmap\fR(2), \fBshmop\fR(2), \fBsem_close\fR(3C), \fBsem_destroy\fR(3C),
291 291 \fBsem_getvalue\fR(3C), \fBsem_init\fR(3C), \fBsem_open\fR(3C),
292 292 \fBsem_post\fR(3C), \fBsem_unlink\fR(3C), \fBsem_wait\fR(3C),
293 293 \fBattributes\fR(5), \fBstandards\fR(5)
294 294 .SH NOTES
295 295 .sp
296 296 .LP
297 297 These functions are also available by way of:
298 298 .sp
299 299 .in +2
300 300 .nf
301 301 #include <thread.h>
302 302 .fi
303 303 .in -2
304 304
305 305 .sp
306 306 .LP
307 307 By default, there is no defined order of unblocking for multiple threads
308 308 waiting for a semaphore.
↓ open down ↓ |
189 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX