2 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
3 .\" Copyright 1989 AT&T
4 .\" Portions Copyright (c) 1992, X/Open Company Limited. 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 http://www.opengroup.org/bookstore/.
6 .\" 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
7 .\" 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
8 .\" 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.
9 .\" This notice shall appear on any product containing this material.
10 .\" 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. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
11 .\" See the License for the specific language governing permissions and limitations under the License. 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
12 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 .TH SEM_OPEN 3C "Jul 9, 2009"
15 sem_open \- initialize/open a named semaphore
19 #include <semaphore.h>
21 \fBsem_t *\fR\fBsem_open\fR(\fBconst char *\fR\fIname\fR, \fBint\fR \fIoflag\fR,
22 \fB/* unsigned long\fR \fImode\fR, \fBunsigned int\fR \fIvalue\fR */ ...);
28 The \fBsem_open()\fR function establishes a connection between a named
29 semaphore and a process (or LWP or thread). Following a call to
30 \fBsem_open()\fR with semaphore name \fIname\fR, the process may reference the
31 semaphore associated with \fIname\fR using the address returned from the call.
32 This semaphore may be used in subsequent calls to \fBsem_wait\fR(3C),
33 \fBsem_trywait\fR(3C), \fBsem_post\fR(3C), and \fBsem_close\fR(3C). The
34 semaphore remains usable by this process until the semaphore is closed by a
35 successful call to \fBsem_close\fR(3C), \fB_Exit\fR(2), or one of the
39 The \fIoflag\fR argument controls whether the semaphore is created or merely
40 accessed by the call to \fBsem_open()\fR. The following flag bits may be set in
48 This flag is used to create a semaphore if it does not already exist. If
49 \fBO_CREAT\fR is set and the semaphore already exists, then \fBO_CREAT\fR has
50 no effect, except as noted under \fB\fR\fBO_EXCL.\fR Otherwise,
51 \fBsem_open()\fR creates a named semaphore. The \fBO_CREAT\fR flag requires a
52 third and a fourth argument: \fImode\fR, which is of type \fBmode_t\fR, and
53 \fIvalue\fR, which is of type \fBunsigned int\fR. The semaphore is created with
54 an initial value of \fIvalue\fR. Valid initial values for semaphores are less
55 than or equal to \fBSEM_VALUE_MAX.\fR
57 The user \fBID\fR of the semaphore is set to the effective user \fBID\fR of the
58 process; the group \fBID\fR of the semaphore is set to a system default group
59 \fBID\fR or to the effective group \fBID\fR of the process. The permission bits
60 of the semaphore are set to the value of the \fImode\fR argument except those
61 set in the file mode creation mask of the process (see \fBumask\fR(2)). When
62 bits in \fImode\fR other than the file permission bits are specified, the
63 effect is unspecified.
65 After the semaphore named \fIname\fR has been created by \fBsem_open()\fR with
66 the \fBO_CREAT\fR flag, other processes can connect to the semaphore by calling
67 \fBsem_open()\fR with the same value of \fIname\fR.
76 If \fBO_EXCL\fR and \fBO_CREAT\fR are set, \fBsem_open()\fR fails if the
77 semaphore \fIname\fR exists. The check for the existence of the semaphore and
78 the creation of the semaphore if it does not exist are atomic with respect to
79 other processes executing \fBsem_open()\fR with \fBO_EXCL\fR and \fBO_CREAT\fR
80 set. If \fBO_EXCL\fR is set and \fBO_CREAT\fR is not set, the effect is
86 If flags other than \fBO_CREAT\fR and \fBO_EXCL\fR are specified in the
87 \fIoflag\fR parameter, the effect is unspecified.
90 The \fIname\fR argument points to a string naming a semaphore object. It is
91 unspecified whether the name appears in the file system and is visible to
92 functions that take pathnames as arguments. The \fIname\fR argument conforms to
93 the construction rules for a pathname. The first character of \fIname\fR must
94 be a slash (/) character and the remaining characters of \fIname\fR cannot
95 include any slash characters. For maximum portability, \fIname\fR should
96 include no more than 14 characters, but this limit is not enforced.
99 If a process makes multiple successful calls to \fBsem_open()\fR with the same
100 value for \fIname\fR, the same semaphore address is returned for each such
101 successful call, provided that there have been no calls to \fBsem_unlink\fR(3C)
105 References to copies of the semaphore produce undefined results.
108 The \fBsem_init\fR(3C) function is used with unnamed semaphores.
112 Upon successful completion, the function returns the address of the semaphore.
113 Otherwise, it will return a value of \fBSEM_FAILED\fR and set \fBerrno\fR to
114 indicate the error. The symbol \fBSEM_FAILED\fR is defined in the header
115 \fB<semaphore.h>\fR\&. No successful return from \fBsem_open()\fR will return
116 the value \fB\fR\fBSEM_FAILED.\fR
120 If any of the following conditions occur, the \fBsem_open()\fR function will
121 return \fBSEM_FAILED\fR and set \fBerrno\fR to the corresponding value:
128 The named semaphore exists and the \fBO_RDWR\fR permissions are denied, or the
129 named semaphore does not exist and permission to create the named semaphore is
139 \fBO_CREAT\fR and \fBO_EXCL\fR are set and the named semaphore already exists.
148 The \fBsem_open()\fR function was interrupted by a signal.
157 The \fBsem_open()\fR operation is not supported for the given name, or
158 \fBO_CREAT\fR was set in \fIoflag\fR and \fIvalue\fR is greater than
168 The number of open semaphore descriptors in this process exceeds
169 \fBSEM_NSEMS_MAX\fR, or the number of open file descriptors in this process
170 exceeds \fBOPEN_MAX\fR.
176 \fB\fBENAMETOOLONG\fR\fR
179 The length of \fIname\fR string exceeds \fIPATH_MAX\fR, or a pathname component
180 is longer than \fINAME_MAX\fR while \fB_POSIX_NO_TRUNC\fR is in effect.
189 Too many semaphores are currently open in the system.
198 \fBO_CREAT\fR is not set and the named semaphore does not exist.
207 There is insufficient space for the creation of the new named semaphore.
216 The \fBsem_open()\fR function is not supported by the system.
222 See \fBattributes\fR(5) for descriptions of the following attributes:
230 ATTRIBUTE TYPE ATTRIBUTE VALUE
232 Interface Stability Committed
236 Standard See \fBstandards\fR(5).
242 \fBexec\fR(2), \fBexit\fR(2), \fBumask\fR(2), \fBsem_close\fR(3C),
243 \fBsem_init\fR(3C), \fBsem_post\fR(3C), \fBsem_unlink\fR(3C),
244 \fBsem_wait\fR(3C), \fBsysconf\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)