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>
  21 
  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
  25 
  26 .LP
  27 .nf
  28 \fBint\fR \fBsema_destroy\fR(\fBsema_t *\fR\fIsp\fR);
  29 .fi
  30 
  31 .LP
  32 .nf
  33 \fBint\fR \fBsema_wait\fR(\fBsema_t *\fR\fIsp\fR);
  34 .fi
  35 
  36 .LP
  37 .nf
  38 \fBint\fR \fBsema_trywait\fR(\fBsema_t *\fR\fIsp\fR);
  39 .fi
  40 
  41 .LP
  42 .nf
  43 \fBint\fR \fBsema_post\fR(\fBsema_t *\fR\fIsp\fR);
  44 .fi
  45 
  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
  82 
  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
  96 
  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.
 101 .sp
 102 .LP
 103 Default semaphore initialization (intra-process):
 104 .sp
 105 .in +2
 106 .nf
 107 sema_t sp;
 108 int count  =  1;
 109 sema_init(&sp, count, NULL, NULL);
 110 .fi
 111 .in -2
 112 
 113 .sp
 114 .LP
 115 or
 116 .sp
 117 .in +2
 118 .nf
 119 sema_init(&sp, count, USYNC_THREAD, NULL);
 120 .fi
 121 .in -2
 122 
 123 .sp
 124 .LP
 125 Customized semaphore initialization (inter-process):
 126 .sp
 127 .in +2
 128 .nf
 129 \fBsema_t sp;
 130 int count  =  1;
 131 sema_init(&sp, count, USYNC_PROCESS, NULL);\fR
 132 .fi
 133 .in -2
 134 
 135 .sp
 136 .LP
 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.
 139 .sp
 140 .LP
 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.
 144 .sp
 145 .LP
 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.
 149 .sp
 150 .LP
 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.
 154 .sp
 155 .LP
 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
 162 .sp
 163 .LP
 164 Upon successful completion,  \fB0\fR is returned; otherwise, a non-zero value
 165 indicates an error.
 166 .SH ERRORS
 167 .sp
 168 .LP
 169 These functions will fail if:
 170 .sp
 171 .ne 2
 172 .na
 173 \fB\fBEINVAL\fR \fR
 174 .ad
 175 .RS 11n
 176 The \fIsp\fR argument does not refer to a valid semaphore.
 177 .RE
 178 
 179 .sp
 180 .ne 2
 181 .na
 182 \fB\fBEFAULT\fR \fR
 183 .ad
 184 .RS 11n
 185 Either the \fIsp\fR or \fIarg\fR argument points to an illegal address.
 186 .RE
 187 
 188 .sp
 189 .LP
 190 The \fBsema_wait()\fR function will fail if:
 191 .sp
 192 .ne 2
 193 .na
 194 \fB\fBEINTR\fR \fR
 195 .ad
 196 .RS 10n
 197 The wait was interrupted by a signal or \fBfork()\fR.
 198 .RE
 199 
 200 .sp
 201 .LP
 202 The \fBsema_trywait()\fR function will fail if:
 203 .sp
 204 .ne 2
 205 .na
 206 \fB\fBEBUSY\fR \fR
 207 .ad
 208 .RS 10n
 209 The semaphore pointed to by \fIsp\fR has a 0 count.
 210 .RE
 211 
 212 .sp
 213 .LP
 214 The \fBsema_post()\fR function will fail if:
 215 .sp
 216 .ne 2
 217 .na
 218 \fB\fBEOVERFLOW\fR \fR
 219 .ad
 220 .RS 14n
 221 The semaphore value pointed to by \fIsp\fR exceeds \fBSEM_VALUE_MAX\fR.
 222 .RE
 223 
 224 .SH EXAMPLES
 225 .LP
 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:
 229 .sp
 230 .in +2
 231 .nf
 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 \&.\|.\|.
 239 
 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 \&.\|.\|.
 245 
 246 void *
 247 customer(int deposit_withdrawal)
 248 {
 249      int this_customer, in_a_hurry = 50;
 250      this_customer = rand() % 100;
 251 
 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 */
 263 
 264       do_business(deposit_withdrawal);
 265       sema_post(&tellers); /* increment tellers; this_customer's
 266                               teller is now available */
 267 }
 268 .fi
 269 .in -2
 270 
 271 .SH ATTRIBUTES
 272 .sp
 273 .LP
 274 See \fBattributes\fR(5) for descriptions of the following attributes:
 275 .sp
 276 
 277 .sp
 278 .TS
 279 box;
 280 c | c
 281 l | l .
 282 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 283 _
 284 MT-Level        Async-Signal-Safe
 285 .TE
 286 
 287 .SH SEE ALSO
 288 .sp
 289 .LP
 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
 295 .sp
 296 .LP
 297 These functions are also available by way of:
 298 .sp
 299 .in +2
 300 .nf
 301 #include <thread.h>
 302 .fi
 303 .in -2
 304 
 305 .sp
 306 .LP
 307 By default, there is no defined order of unblocking for multiple threads
 308 waiting for a semaphore.