4829 sema_init(3C) botches the arguments in an example
[illumos-gate.git] / usr / src / man / man3c / semaphore.3c
blob28adffc86172cf32bdbab45e238a6ca2ba0fd414
1 '\" te
2 .\" Copyright (c) 2008 Sun Microsystems, Inc.  All Rights Reserved.
3 .\" Portions Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
4 .\" Portions Copyright (c) 1995 IEEE. All Rights Reserved
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 .\" http://www.opengroup.org/bookstore/.
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 .\"  This notice shall appear on any product containing this material.
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 .\" 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 .\" 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 .TH SEMAPHORE 3C "Feb 5, 2008"
13 .SH NAME
14 semaphore, sema_init, sema_destroy, sema_wait, sema_trywait, sema_post \-
15 semaphores
16 .SH SYNOPSIS
17 .LP
18 .nf
19 cc [ \fIflag\fR... ] \fIfile\fR... -lthread  -lc [ \fIlibrary\fR... ]
20 #include <synch.h>
22 \fBint\fR \fBsema_init\fR(\fBsema_t *\fR\fIsp\fR, \fBunsigned int\fR \fIcount\fR, \fBint\fR \fItype\fR,
23      \fBvoid *\fR \fIarg\fR);
24 .fi
26 .LP
27 .nf
28 \fBint\fR \fBsema_destroy\fR(\fBsema_t *\fR\fIsp\fR);
29 .fi
31 .LP
32 .nf
33 \fBint\fR \fBsema_wait\fR(\fBsema_t *\fR\fIsp\fR);
34 .fi
36 .LP
37 .nf
38 \fBint\fR \fBsema_trywait\fR(\fBsema_t *\fR\fIsp\fR);
39 .fi
41 .LP
42 .nf
43 \fBint\fR \fBsema_post\fR(\fBsema_t *\fR\fIsp\fR);
44 .fi
46 .SH DESCRIPTION
47 .sp
48 .LP
49 A semaphore is a non-negative integer count and is generally used to coordinate
50 access to resources. The initial semaphore count is set to the number of free
51 resources, then threads slowly increment and decrement the count as resources
52 are added and removed. If the semaphore count drops to 0, which means no
53 available resources,  threads attempting to decrement the semaphore will block
54 until the count is greater than 0.
55 .sp
56 .LP
57 Semaphores can synchronize threads in this process and other processes if they
58 are allocated in writable memory  and shared among the cooperating processes
59 (see \fBmmap\fR(2)), and have been initialized for this purpose.
60 .sp
61 .LP
62 Semaphores must be initialized before use; semaphores pointed to by \fIsp\fR to
63 \fIcount\fR are initialized by \fBsema_init()\fR. The \fItype\fR argument can
64 assign several different types of behavior to a semaphore. No current type uses
65 \fIarg\fR, although it may be used in the future.
66 .sp
67 .LP
68 The \fItype\fR argument may be one of the following:
69 .sp
70 .ne 2
71 .na
72 \fB\fBUSYNC_PROCESS\fR \fR
73 .ad
74 .RS 18n
75 The semaphore can synchronize threads in this process and other processes.
76 Initializing the semaphore should be done by only one process. A semaphore
77 initialized with this type must be allocated in memory shared between
78 processes, either in Sys V shared memory (see \fBshmop\fR(2)), or in memory
79 mapped to a file (see \fBmmap\fR(2)). It is illegal to initialize the object
80 this way and not allocate it in such shared memory. \fIarg\fR is ignored.
81 .RE
83 .sp
84 .ne 2
85 .na
86 \fB\fBUSYNC_THREAD\fR \fR
87 .ad
88 .RS 18n
89 The semaphore can synchronize  threads only in this process. The \fIarg\fR
90 argument is ignored. \fBUSYNC_THREAD\fR does not support multiple mappings to
91 the same logical synch object. If you need to \fBmmap()\fR a synch object to
92 different locations within the same address space, then the synch object should
93 be initialized as a shared object \fBUSYNC_PROCESS\fR for Solaris threads and
94 \fBPTHREAD_PROCESS_PRIVATE\fR for POSIX threads.
95 .RE
97 .sp
98 .LP
99 A semaphore must not be simultaneously initialized by multiple threads, nor
100 re-initialized while in use by other threads.
103 Default semaphore initialization (intra-process):
105 .in +2
107 sema_t sp;
108 int count  =  1;
109 sema_init(&sp, count, 0, NULL);
111 .in -2
117 .in +2
119 sema_init(&sp, count, USYNC_THREAD, NULL);
121 .in -2
125 Customized semaphore initialization (inter-process):
127 .in +2
129 \fBsema_t sp;
130 int count  =  1;
131 sema_init(&sp, count, USYNC_PROCESS, NULL);\fR
133 .in -2
137 The \fBsema_destroy()\fR function destroys any state related to the semaphore
138 pointed to by \fIsp\fR. The semaphore storage space is not released.
141 The \fBsema_wait()\fR function blocks the calling thread until the semaphore
142 count pointed to by \fIsp\fR is greater than 0, and then it atomically
143 decrements the count.
146 The \fBsema_trywait()\fR function atomically decrements the semaphore count
147 pointed to by \fIsp\fR, if the count is greater than 0; otherwise, it returns
148 an error.
151 The \fBsema_post()\fR function atomically increments the semaphore count
152 pointed to by \fIsp\fR. If there are any threads blocked on the semaphore, one
153 will be unblocked.
156 The semaphore functionality described on this man page is for the Solaris
157 threads implementation.  For the POSIX-conforming semaphore interface
158 documentation, see \fBsem_close\fR(3C), \fBsem_destroy\fR(3C),
159 \fBsem_getvalue\fR(3C), \fBsem_init\fR(3C), \fBsem_open\fR(3C),
160 \fBsem_post\fR(3C), \fBsem_unlink\fR(3C), and \fBsem_wait\fR(3C).
161 .SH RETURN VALUES
164 Upon successful completion,  \fB0\fR is returned; otherwise, a non-zero value
165 indicates an error.
166 .SH ERRORS
169 These functions will fail if:
171 .ne 2
173 \fB\fBEINVAL\fR \fR
175 .RS 11n
176 The \fIsp\fR argument does not refer to a valid semaphore.
180 .ne 2
182 \fB\fBEFAULT\fR \fR
184 .RS 11n
185 Either the \fIsp\fR or \fIarg\fR argument points to an illegal address.
190 The \fBsema_wait()\fR function will fail if:
192 .ne 2
194 \fB\fBEINTR\fR \fR
196 .RS 10n
197 The wait was interrupted by a signal or \fBfork()\fR.
202 The \fBsema_trywait()\fR function will fail if:
204 .ne 2
206 \fB\fBEBUSY\fR \fR
208 .RS 10n
209 The semaphore pointed to by \fIsp\fR has a 0 count.
214 The \fBsema_post()\fR function will fail if:
216 .ne 2
218 \fB\fBEOVERFLOW\fR \fR
220 .RS 14n
221 The semaphore value pointed to by \fIsp\fR exceeds \fBSEM_VALUE_MAX\fR.
224 .SH EXAMPLES
226 \fBExample 1 \fRThe customer waiting-line in a bank is analogous to the
227 synchronization scheme of a semaphore using \fBsema_wait()\fR and
228 \fBsema_trywait()\fR:
230 .in +2
232 /* cc [ flag \|.\|.\|. ] file \|.\|.\|. -lthread [ library \|.\|.\|. ] */
233 #include <errno.h>
234 #define TELLERS 10
235 sema_t     tellers;     /* semaphore */
236 int banking_hours(), deposit_withdrawal;
237 void*customer(), do_business(), skip_banking_today();
238 \&.\|.\|.
240 sema_init(&tellers, TELLERS, USYNC_THREAD, NULL);
241     /* 10 tellers available */
242 while(banking_hours())
243     pthread_create(NULL, NULL, customer, deposit_withdrawal);
244 \&.\|.\|.
246 void *
247 customer(int deposit_withdrawal)
249      int this_customer, in_a_hurry = 50;
250      this_customer = rand() % 100;
252      if (this_customer == in_a_hurry)  {
253          if (sema_trywait(&tellers) != 0)
254              if (errno == EBUSY){ /* no teller available */
255                   skip_banking_today(this_customer);
256                   return;
257          } /* else go immediately to available teller and
258                                 decrement tellers */
259       }
260       else
261          sema_wait(&tellers); /* wait for next teller, then
262                                 proceed, and decrement tellers */
264       do_business(deposit_withdrawal);
265       sema_post(&tellers); /* increment tellers; this_customer's
266                               teller is now available */
269 .in -2
271 .SH ATTRIBUTES
274 See \fBattributes\fR(5) for descriptions of the following attributes:
279 box;
280 c | c
281 l | l .
282 ATTRIBUTE TYPE  ATTRIBUTE VALUE
284 MT-Level        Async-Signal-Safe
287 .SH SEE ALSO
290 \fBmmap\fR(2), \fBshmop\fR(2), \fBsem_close\fR(3C), \fBsem_destroy\fR(3C),
291 \fBsem_getvalue\fR(3C), \fBsem_init\fR(3C), \fBsem_open\fR(3C),
292 \fBsem_post\fR(3C), \fBsem_unlink\fR(3C), \fBsem_wait\fR(3C),
293 \fBattributes\fR(5), \fBstandards\fR(5)
294 .SH NOTES
297 These functions are also available by way of:
299 .in +2
301 #include <thread.h>
303 .in -2
307 By default, there is no defined order of unblocking for multiple threads
308 waiting for a semaphore.