2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
25 .\" This notice shall appear on any product containing this material.
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
43 .\" Portions Copyright (c) 1995 IEEE. All Rights Reserved.
44 .\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved.
45 .\" Portions Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
47 .TH SEMAPHORE 3C "Feb 5, 2008"
49 semaphore, sema_init, sema_destroy, sema_wait, sema_trywait, sema_post \-
54 cc [ \fIflag\fR... ] \fIfile\fR... -lthread -lc [ \fIlibrary\fR... ]
57 \fBint\fR \fBsema_init\fR(\fBsema_t *\fR\fIsp\fR, \fBunsigned int\fR \fIcount\fR, \fBint\fR \fItype\fR,
58 \fBvoid *\fR \fIarg\fR);
63 \fBint\fR \fBsema_destroy\fR(\fBsema_t *\fR\fIsp\fR);
68 \fBint\fR \fBsema_wait\fR(\fBsema_t *\fR\fIsp\fR);
73 \fBint\fR \fBsema_trywait\fR(\fBsema_t *\fR\fIsp\fR);
78 \fBint\fR \fBsema_post\fR(\fBsema_t *\fR\fIsp\fR);
84 A semaphore is a non-negative integer count and is generally used to coordinate
85 access to resources. The initial semaphore count is set to the number of free
86 resources, then threads slowly increment and decrement the count as resources
87 are added and removed. If the semaphore count drops to 0, which means no
88 available resources, threads attempting to decrement the semaphore will block
89 until the count is greater than 0.
92 Semaphores can synchronize threads in this process and other processes if they
93 are allocated in writable memory and shared among the cooperating processes
94 (see \fBmmap\fR(2)), and have been initialized for this purpose.
97 Semaphores must be initialized before use; semaphores pointed to by \fIsp\fR to
98 \fIcount\fR are initialized by \fBsema_init()\fR. The \fItype\fR argument can
99 assign several different types of behavior to a semaphore. No current type uses
100 \fIarg\fR, although it may be used in the future.
103 The \fItype\fR argument may be one of the following:
107 \fB\fBUSYNC_PROCESS\fR \fR
110 The semaphore can synchronize threads in this process and other processes.
111 Initializing the semaphore should be done by only one process. A semaphore
112 initialized with this type must be allocated in memory shared between
113 processes, either in Sys V shared memory (see \fBshmop\fR(2)), or in memory
114 mapped to a file (see \fBmmap\fR(2)). It is illegal to initialize the object
115 this way and not allocate it in such shared memory. \fIarg\fR is ignored.
121 \fB\fBUSYNC_THREAD\fR \fR
124 The semaphore can synchronize threads only in this process. The \fIarg\fR
125 argument is ignored. \fBUSYNC_THREAD\fR does not support multiple mappings to
126 the same logical synch object. If you need to \fBmmap()\fR a synch object to
127 different locations within the same address space, then the synch object should
128 be initialized as a shared object \fBUSYNC_PROCESS\fR for Solaris threads and
129 \fBPTHREAD_PROCESS_PRIVATE\fR for POSIX threads.
134 A semaphore must not be simultaneously initialized by multiple threads, nor
135 re-initialized while in use by other threads.
138 Default semaphore initialization (intra-process):
144 sema_init(&sp, count, 0, NULL);
154 sema_init(&sp, count, USYNC_THREAD, NULL);
160 Customized semaphore initialization (inter-process):
166 sema_init(&sp, count, USYNC_PROCESS, NULL);\fR
172 The \fBsema_destroy()\fR function destroys any state related to the semaphore
173 pointed to by \fIsp\fR. The semaphore storage space is not released.
176 The \fBsema_wait()\fR function blocks the calling thread until the semaphore
177 count pointed to by \fIsp\fR is greater than 0, and then it atomically
178 decrements the count.
181 The \fBsema_trywait()\fR function atomically decrements the semaphore count
182 pointed to by \fIsp\fR, if the count is greater than 0; otherwise, it returns
186 The \fBsema_post()\fR function atomically increments the semaphore count
187 pointed to by \fIsp\fR. If there are any threads blocked on the semaphore, one
191 The semaphore functionality described on this man page is for the Solaris
192 threads implementation. For the POSIX-conforming semaphore interface
193 documentation, see \fBsem_close\fR(3C), \fBsem_destroy\fR(3C),
194 \fBsem_getvalue\fR(3C), \fBsem_init\fR(3C), \fBsem_open\fR(3C),
195 \fBsem_post\fR(3C), \fBsem_unlink\fR(3C), and \fBsem_wait\fR(3C).
199 Upon successful completion, \fB0\fR is returned; otherwise, a non-zero value
204 These functions will fail if:
211 The \fIsp\fR argument does not refer to a valid semaphore.
220 Either the \fIsp\fR or \fIarg\fR argument points to an illegal address.
225 The \fBsema_wait()\fR function will fail if:
232 The wait was interrupted by a signal or \fBfork()\fR.
237 The \fBsema_trywait()\fR function will fail if:
244 The semaphore pointed to by \fIsp\fR has a 0 count.
249 The \fBsema_post()\fR function will fail if:
253 \fB\fBEOVERFLOW\fR \fR
256 The semaphore value pointed to by \fIsp\fR exceeds \fBSEM_VALUE_MAX\fR.
261 \fBExample 1 \fRThe customer waiting-line in a bank is analogous to the
262 synchronization scheme of a semaphore using \fBsema_wait()\fR and
263 \fBsema_trywait()\fR:
267 /* cc [ flag \|.\|.\|. ] file \|.\|.\|. -lthread [ library \|.\|.\|. ] */
270 sema_t tellers; /* semaphore */
271 int banking_hours(), deposit_withdrawal;
272 void*customer(), do_business(), skip_banking_today();
275 sema_init(&tellers, TELLERS, USYNC_THREAD, NULL);
276 /* 10 tellers available */
277 while(banking_hours())
278 pthread_create(NULL, NULL, customer, deposit_withdrawal);
282 customer(int deposit_withdrawal)
284 int this_customer, in_a_hurry = 50;
285 this_customer = rand() % 100;
287 if (this_customer == in_a_hurry) {
288 if (sema_trywait(&tellers) != 0)
289 if (errno == EBUSY){ /* no teller available */
290 skip_banking_today(this_customer);
292 } /* else go immediately to available teller and
296 sema_wait(&tellers); /* wait for next teller, then
297 proceed, and decrement tellers */
299 do_business(deposit_withdrawal);
300 sema_post(&tellers); /* increment tellers; this_customer's
301 teller is now available */
309 See \fBattributes\fR(5) for descriptions of the following attributes:
317 ATTRIBUTE TYPE ATTRIBUTE VALUE
319 MT-Level Async-Signal-Safe
325 \fBmmap\fR(2), \fBshmop\fR(2), \fBsem_close\fR(3C), \fBsem_destroy\fR(3C),
326 \fBsem_getvalue\fR(3C), \fBsem_init\fR(3C), \fBsem_open\fR(3C),
327 \fBsem_post\fR(3C), \fBsem_unlink\fR(3C), \fBsem_wait\fR(3C),
328 \fBattributes\fR(5), \fBstandards\fR(5)
332 These functions are also available by way of:
342 By default, there is no defined order of unblocking for multiple threads
343 waiting for a semaphore.