9103 opengroup acknowledgement should be properly formatted in man pages
[unleashed.git] / usr / src / man / man3c / semaphore.3c
blobbfc5b84869b4d3eead1606fbf3259491f0d0668b
1 .\"
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/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
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.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
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.
30 .\"
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.
35 .\"
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]
41 .\"
42 .\"
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.
46 .\"
47 .TH SEMAPHORE 3C "Feb 5, 2008"
48 .SH NAME
49 semaphore, sema_init, sema_destroy, sema_wait, sema_trywait, sema_post \-
50 semaphores
51 .SH SYNOPSIS
52 .LP
53 .nf
54 cc [ \fIflag\fR... ] \fIfile\fR... -lthread  -lc [ \fIlibrary\fR... ]
55 #include <synch.h>
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);
59 .fi
61 .LP
62 .nf
63 \fBint\fR \fBsema_destroy\fR(\fBsema_t *\fR\fIsp\fR);
64 .fi
66 .LP
67 .nf
68 \fBint\fR \fBsema_wait\fR(\fBsema_t *\fR\fIsp\fR);
69 .fi
71 .LP
72 .nf
73 \fBint\fR \fBsema_trywait\fR(\fBsema_t *\fR\fIsp\fR);
74 .fi
76 .LP
77 .nf
78 \fBint\fR \fBsema_post\fR(\fBsema_t *\fR\fIsp\fR);
79 .fi
81 .SH DESCRIPTION
82 .sp
83 .LP
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.
90 .sp
91 .LP
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.
95 .sp
96 .LP
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:
105 .ne 2
107 \fB\fBUSYNC_PROCESS\fR \fR
109 .RS 18n
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.
119 .ne 2
121 \fB\fBUSYNC_THREAD\fR \fR
123 .RS 18n
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):
140 .in +2
142 sema_t sp;
143 int count  =  1;
144 sema_init(&sp, count, 0, NULL);
146 .in -2
152 .in +2
154 sema_init(&sp, count, USYNC_THREAD, NULL);
156 .in -2
160 Customized semaphore initialization (inter-process):
162 .in +2
164 \fBsema_t sp;
165 int count  =  1;
166 sema_init(&sp, count, USYNC_PROCESS, NULL);\fR
168 .in -2
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
183 an error.
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
188 will be unblocked.
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).
196 .SH RETURN VALUES
199 Upon successful completion,  \fB0\fR is returned; otherwise, a non-zero value
200 indicates an error.
201 .SH ERRORS
204 These functions will fail if:
206 .ne 2
208 \fB\fBEINVAL\fR \fR
210 .RS 11n
211 The \fIsp\fR argument does not refer to a valid semaphore.
215 .ne 2
217 \fB\fBEFAULT\fR \fR
219 .RS 11n
220 Either the \fIsp\fR or \fIarg\fR argument points to an illegal address.
225 The \fBsema_wait()\fR function will fail if:
227 .ne 2
229 \fB\fBEINTR\fR \fR
231 .RS 10n
232 The wait was interrupted by a signal or \fBfork()\fR.
237 The \fBsema_trywait()\fR function will fail if:
239 .ne 2
241 \fB\fBEBUSY\fR \fR
243 .RS 10n
244 The semaphore pointed to by \fIsp\fR has a 0 count.
249 The \fBsema_post()\fR function will fail if:
251 .ne 2
253 \fB\fBEOVERFLOW\fR \fR
255 .RS 14n
256 The semaphore value pointed to by \fIsp\fR exceeds \fBSEM_VALUE_MAX\fR.
259 .SH EXAMPLES
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:
265 .in +2
267 /* cc [ flag \|.\|.\|. ] file \|.\|.\|. -lthread [ library \|.\|.\|. ] */
268 #include <errno.h>
269 #define TELLERS 10
270 sema_t     tellers;     /* semaphore */
271 int banking_hours(), deposit_withdrawal;
272 void*customer(), do_business(), skip_banking_today();
273 \&.\|.\|.
275 sema_init(&tellers, TELLERS, USYNC_THREAD, NULL);
276     /* 10 tellers available */
277 while(banking_hours())
278     pthread_create(NULL, NULL, customer, deposit_withdrawal);
279 \&.\|.\|.
281 void *
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);
291                   return;
292          } /* else go immediately to available teller and
293                                 decrement tellers */
294       }
295       else
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 */
304 .in -2
306 .SH ATTRIBUTES
309 See \fBattributes\fR(5) for descriptions of the following attributes:
314 box;
315 c | c
316 l | l .
317 ATTRIBUTE TYPE  ATTRIBUTE VALUE
319 MT-Level        Async-Signal-Safe
322 .SH SEE ALSO
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)
329 .SH NOTES
332 These functions are also available by way of:
334 .in +2
336 #include <thread.h>
338 .in -2
342 By default, there is no defined order of unblocking for multiple threads
343 waiting for a semaphore.