| 12
 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
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 
 | '\" et
.TH SEM_OPEN "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.SH NAME
sem_open
\(em initialize and open a named semaphore
.SH SYNOPSIS
.LP
.nf
#include <semaphore.h>
.P
sem_t *sem_open(const char *\fIname\fP, int \fIoflag\fP, ...);
.fi
.SH DESCRIPTION
The
\fIsem_open\fR()
function shall establish a connection between a named semaphore
and a process. Following a call to
\fIsem_open\fR()
with semaphore name
.IR name ,
the process may reference the semaphore associated with
.IR name
using the address returned from the call. This semaphore may be used
in subsequent calls to
\fIsem_wait\fR(),
\fIsem_timedwait\fR(),
\fIsem_trywait\fR(),
\fIsem_post\fR(),
and
\fIsem_close\fR().
The semaphore remains usable by this process until the semaphore is
closed by a successful call to
\fIsem_close\fR(),
\fI_exit\fR(),
or one of the
.IR exec
functions.
.P
The
.IR oflag
argument controls whether the semaphore is created or merely accessed
by the call to
\fIsem_open\fR().
The following flag bits may be set in
.IR oflag :
.IP O_CREAT 10
This flag is used to create a semaphore if it does not already exist.
If O_CREAT is set and the semaphore already exists, then O_CREAT has no
effect, except as noted under O_EXCL. Otherwise,
\fIsem_open\fR()
creates a named semaphore. The O_CREAT flag requires a third and a
fourth argument:
.IR mode ,
which is of type
.BR mode_t ,
and
.IR value ,
which is of type
.BR unsigned .
The semaphore is created with an initial value of
.IR value .
Valid initial values for semaphores are less than or equal to
{SEM_VALUE_MAX}.
.RS 10 
.P
The user ID of the semaphore shall be set to the effective user ID of
the process. The group ID of the semaphore shall be set to the effective
group ID of the process; however, if the
.IR name
argument is visible in the file system, the group ID may be set to
the group ID of the containing directory. The permission bits of the
semaphore are set to the value of the
.IR mode
argument except those set in the file mode creation mask of the
process. When bits in
.IR mode
other than the file permission bits are specified, the effect is
unspecified.
.P
After the semaphore named
.IR name
has been created by
\fIsem_open\fR()
with the O_CREAT flag, other processes can connect to the semaphore by
calling
\fIsem_open\fR()
with the same value of
.IR name .
.RE
.IP O_EXCL 10
If O_EXCL and O_CREAT are set,
\fIsem_open\fR()
fails if the semaphore
.IR name
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
\fIsem_open\fR()
with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set,
the effect is undefined.
.RS 10 
.P
If flags other than O_CREAT and O_EXCL are specified in the
.IR oflag
parameter, the effect is unspecified.
.RE
.P
The
.IR name
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
.IR name
argument conforms to the construction rules for a pathname, except
that the interpretation of
<slash>
characters other than the leading
<slash>
character in
.IR name
is implementation-defined, and that the length limits for the
.IR name
argument are implementation-defined and need not be the same as
the pathname limits
{PATH_MAX}
and
{NAME_MAX}.
If
.IR name
begins with the
<slash>
character, then processes calling
\fIsem_open\fR()
with the same value of
.IR name
shall refer to the same semaphore object, as long as that name has not
been removed. If
.IR name
does not begin with the
<slash>
character, the effect is implementation-defined.
.P
If a process makes multiple successful calls to
\fIsem_open\fR()
with the same value for
.IR name ,
the same semaphore address shall be returned for each such successful
call, provided that there have been no calls to
\fIsem_unlink\fR()
for this semaphore, and at least one previous successful
\fIsem_open\fR()
call for this semaphore has not been matched with a
\fIsem_close\fR()
call.
.P
References to copies of the semaphore produce undefined results.
.SH "RETURN VALUE"
Upon successful completion, the
\fIsem_open\fR()
function shall return the address of the semaphore. Otherwise, it
shall return a value of SEM_FAILED and set
.IR errno
to indicate the error. The symbol SEM_FAILED is defined in the
.IR <semaphore.h> 
header. No successful return from
\fIsem_open\fR()
shall return the value SEM_FAILED.
.SH ERRORS
If any of the following conditions occur, the
\fIsem_open\fR()
function shall return SEM_FAILED and set
.IR errno
to the corresponding value:
.TP
.BR EACCES
The named semaphore exists and the permissions specified by
.IR oflag
are denied, or the named semaphore does not exist and permission to
create the named semaphore is denied.
.TP
.BR EEXIST
O_CREAT and O_EXCL are set and the named semaphore already exists.
.TP
.BR EINTR
The
\fIsem_open\fR()
operation was interrupted by a signal.
.TP
.BR EINVAL
The
\fIsem_open\fR()
operation is not supported for the given name, or O_CREAT was specified
in
.IR oflag
and
.IR value
was greater than
{SEM_VALUE_MAX}.
.TP
.BR EMFILE
Too many semaphore descriptors or file descriptors are currently in use
by this process.
.TP
.BR ENFILE
Too many semaphores are currently open in the system.
.TP
.BR ENOENT
O_CREAT is not set and the named semaphore does not exist.
.TP
.BR ENOMEM
There is insufficient memory for the creation of the new named
semaphore.
.TP
.BR ENOSPC
There is insufficient space on a storage device for the creation of the
new named semaphore.
.P
If any of the following conditions occur, the
\fIsem_open\fR()
function may return SEM_FAILED and set
.IR errno
to the corresponding value:
.TP
.BR ENAMETOOLONG
.br
The length of the
.IR name
argument exceeds
{_POSIX_PATH_MAX}
on systems that do not support the XSI option
or exceeds
{_XOPEN_PATH_MAX}
on XSI systems,
or has a pathname component that is longer than
{_POSIX_NAME_MAX}
on systems that do not support the XSI option
or longer than
{_XOPEN_NAME_MAX}
on XSI systems.
.LP
.IR "The following sections are informative."
.SH EXAMPLES
None.
.SH "APPLICATION USAGE"
None.
.SH RATIONALE
Early drafts required an error return value of \(mi1 with the type
.BR "sem_t *"
for the
\fIsem_open\fR()
function, which is not guaranteed to be portable across
implementations. The revised text provides the symbolic error code
SEM_FAILED to eliminate the type conflict.
.SH "FUTURE DIRECTIONS"
A future version might require the
\fIsem_open\fR()
and
\fIsem_unlink\fR()
functions to have semantics similar to normal file system operations.
.SH "SEE ALSO"
.ad l
.IR "\fIsemctl\fR\^(\|)",
.IR "\fIsemget\fR\^(\|)",
.IR "\fIsemop\fR\^(\|)",
.IR "\fIsem_close\fR\^(\|)",
.IR "\fIsem_post\fR\^(\|)",
.IR "\fIsem_timedwait\fR\^(\|)",
.IR "\fIsem_trywait\fR\^(\|)",
.IR "\fIsem_unlink\fR\^(\|)"
.ad b
.P
The Base Definitions volume of POSIX.1\(hy2008,
.IR "\fB<semaphore.h>\fP"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
event of any discrepancy between this version 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.unix.org/online.html .
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
 |