summaryrefslogtreecommitdiff
path: root/usr/src/man/man3c/sem_open.3c
blob: bda42d12fa5ea8bc2f143e151b01145248c009dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
.\"
.\" 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/.
.\"
.\" 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 SunOS 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.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" 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.
.\" 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 the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2009, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH SEM_OPEN 3C "Jul 9, 2009"
.SH NAME
sem_open \- initialize/open a named semaphore
.SH SYNOPSIS
.LP
.nf
#include <semaphore.h>

\fBsem_t *\fR\fBsem_open\fR(\fBconst char *\fR\fIname\fR, \fBint\fR \fIoflag\fR,
     \fB/* unsigned long\fR \fImode\fR, \fBunsigned int\fR \fIvalue\fR */ ...);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBsem_open()\fR function establishes a connection between a named
semaphore and a process (or LWP or thread). Following a call to
\fBsem_open()\fR with semaphore name \fIname\fR, the process may reference the
semaphore associated with \fIname\fR using the address returned from the call.
This semaphore may be used in subsequent calls to \fBsem_wait\fR(3C),
\fBsem_trywait\fR(3C), \fBsem_post\fR(3C), and \fBsem_close\fR(3C). The
semaphore remains usable by this process until the semaphore is closed by a
successful call to \fBsem_close\fR(3C), \fB_Exit\fR(2), or one of the
\fBexec\fR functions.
.sp
.LP
The \fIoflag\fR argument controls whether the semaphore is created or merely
accessed by the call to \fBsem_open()\fR. The following flag bits may be set in
\fIoflag\fR:
.sp
.ne 2
.na
\fB\fBO_CREAT\fR\fR
.ad
.RS 11n
This flag is used to create a semaphore if it does not already exist. If
\fBO_CREAT\fR is set and the semaphore already exists, then \fBO_CREAT\fR has
no effect, except as noted under \fB\fR\fBO_EXCL.\fR Otherwise,
\fBsem_open()\fR creates a named semaphore. The \fBO_CREAT\fR flag requires a
third and a fourth argument: \fImode\fR, which is of type \fBmode_t\fR, and
\fIvalue\fR, which is of type \fBunsigned int\fR. The semaphore is created with
an initial value of \fIvalue\fR. Valid initial values for semaphores are less
than or equal to \fBSEM_VALUE_MAX.\fR
.sp
The user \fBID\fR of the semaphore is set to the effective user \fBID\fR of the
process; the group \fBID\fR of the semaphore is set to a system default group
\fBID\fR or to the effective group \fBID\fR of the process. The permission bits
of the semaphore are set to the value of the \fImode\fR argument except those
set in the file mode creation mask of the process (see \fBumask\fR(2)). When
bits in \fImode\fR other than the file permission bits are specified, the
effect is unspecified.
.sp
After the semaphore named \fIname\fR has been created by \fBsem_open()\fR with
the \fBO_CREAT\fR flag, other processes can connect to the semaphore by calling
\fBsem_open()\fR with the same value of \fIname\fR.
.RE

.sp
.ne 2
.na
\fB\fBO_EXCL\fR\fR
.ad
.RS 11n
If \fBO_EXCL\fR and \fBO_CREAT\fR are set, \fBsem_open()\fR fails if the
semaphore \fIname\fR exists. The check for the existence of the semaphore and
the creation of the semaphore if it does not exist are atomic with respect to
other processes executing \fBsem_open()\fR with \fBO_EXCL\fR and \fBO_CREAT\fR
set. If \fBO_EXCL\fR is set and \fBO_CREAT\fR is not set, the effect is
undefined.
.RE

.sp
.LP
If flags other than \fBO_CREAT\fR and \fBO_EXCL\fR are specified in the
\fIoflag\fR parameter, the effect is unspecified.
.sp
.LP
The \fIname\fR argument points to a string naming a semaphore object. It is
unspecified whether the name appears in the file system and is visible to
functions that take pathnames as arguments. The \fIname\fR argument conforms to
the construction rules for a pathname. The first character of  \fIname\fR must
be a slash  (/) character and the remaining characters of  \fIname\fR cannot
include any slash characters.  For maximum portability,  \fIname\fR should
include no more than 14 characters, but this limit is not enforced.
.sp
.LP
If a process makes multiple successful calls to \fBsem_open()\fR with the same
value for \fIname\fR, the same semaphore address is returned for each such
successful call, provided that there have been no calls to \fBsem_unlink\fR(3C)
for this semaphore.
.sp
.LP
References to copies of the semaphore produce undefined results.
.sp
.LP
The \fBsem_init\fR(3C) function is used with unnamed semaphores.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, the function returns the address of the semaphore.
Otherwise, it will return a value of \fBSEM_FAILED\fR and set \fBerrno\fR to
indicate the error. The symbol \fBSEM_FAILED\fR is defined in the header
\fB<semaphore.h>\fR\&. No successful return from \fBsem_open()\fR will return
the value \fB\fR\fBSEM_FAILED.\fR
.SH ERRORS
.sp
.LP
If any of the following conditions occur, the \fBsem_open()\fR function will
return \fBSEM_FAILED\fR and set \fBerrno\fR to the corresponding value:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
The named semaphore exists and the \fBO_RDWR\fR permissions are denied, or the
named semaphore does not exist and permission to create the named semaphore is
denied.
.RE

.sp
.ne 2
.na
\fB\fBEEXIST\fR\fR
.ad
.RS 16n
\fBO_CREAT\fR and  \fBO_EXCL\fR are set and the named semaphore already exists.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
The \fBsem_open()\fR function was interrupted by a signal.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The \fBsem_open()\fR operation is not supported for the given name, or
\fBO_CREAT\fR was set in \fIoflag\fR and \fIvalue\fR is greater than
\fBSEM_VALUE_MAX\fR.
.RE

.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 16n
The number of open semaphore descriptors in this process exceeds
\fBSEM_NSEMS_MAX\fR, or the number of open file descriptors in this process
exceeds  \fBOPEN_MAX\fR.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
The length of \fIname\fR string exceeds \fIPATH_MAX\fR, or a pathname component
is longer than \fINAME_MAX\fR while \fB_POSIX_NO_TRUNC\fR is in effect.
.RE

.sp
.ne 2
.na
\fB\fBENFILE\fR\fR
.ad
.RS 16n
Too many semaphores are currently open in the system.
.RE

.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
\fBO_CREAT\fR is not set and the named semaphore does not exist.
.RE

.sp
.ne 2
.na
\fB\fBENOSPC\fR\fR
.ad
.RS 16n
There is insufficient space for the creation of the new named semaphore.
.RE

.sp
.ne 2
.na
\fB\fBENOSYS\fR\fR
.ad
.RS 16n
The \fBsem_open()\fR function is not supported by the system.
.RE

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	MT-Safe
_
Standard	See \fBstandards\fR(5).
.TE

.SH SEE ALSO
.sp
.LP
\fBexec\fR(2), \fBexit\fR(2), \fBumask\fR(2), \fBsem_close\fR(3C),
\fBsem_init\fR(3C), \fBsem_post\fR(3C), \fBsem_unlink\fR(3C),
\fBsem_wait\fR(3C), \fBsysconf\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)