1SEM_OPEN(3P) POSIX Programmer's Manual SEM_OPEN(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 sem_open — initialize and open a named semaphore
14
16 #include <semaphore.h>
17
18 sem_t *sem_open(const char *name, int oflag, ...);
19
21 The sem_open() function shall establish a connection between a named
22 semaphore and a process. Following a call to sem_open() with semaphore
23 name name, the process may reference the semaphore associated with name
24 using the address returned from the call. This semaphore may be used in
25 subsequent calls to sem_wait(), sem_timedwait(), sem_trywait(),
26 sem_post(), and sem_close(). The semaphore remains usable by this
27 process until the semaphore is closed by a successful call to
28 sem_close(), _exit(), or one of the exec functions.
29
30 The oflag argument controls whether the semaphore is created or merely
31 accessed by the call to sem_open(). The following flag bits may be set
32 in oflag:
33
34 O_CREAT This flag is used to create a semaphore if it does not
35 already exist. If O_CREAT is set and the semaphore already
36 exists, then O_CREAT has no effect, except as noted under
37 O_EXCL. Otherwise, sem_open() creates a named semaphore. The
38 O_CREAT flag requires a third and a fourth argument: mode,
39 which is of type mode_t, and value, which is of type
40 unsigned. The semaphore is created with an initial value of
41 value. Valid initial values for semaphores are less than or
42 equal to {SEM_VALUE_MAX}.
43
44 The user ID of the semaphore shall be set to the effective
45 user ID of the process. The group ID of the semaphore shall
46 be set to the effective group ID of the process; however, if
47 the name argument is visible in the file system, the group ID
48 may be set to the group ID of the containing directory. The
49 permission bits of the semaphore are set to the value of the
50 mode argument except those set in the file mode creation mask
51 of the process. When bits in mode other than the file permis‐
52 sion bits are specified, the effect is unspecified.
53
54 After the semaphore named name has been created by sem_open()
55 with the O_CREAT flag, other processes can connect to the
56 semaphore by calling sem_open() with the same value of name.
57
58 O_EXCL If O_EXCL and O_CREAT are set, sem_open() fails if the sema‐
59 phore name exists. The check for the existence of the sema‐
60 phore and the creation of the semaphore if it does not exist
61 are atomic with respect to other processes executing
62 sem_open() with O_EXCL and O_CREAT set. If O_EXCL is set and
63 O_CREAT is not set, the effect is undefined.
64
65 If flags other than O_CREAT and O_EXCL are specified in the
66 oflag parameter, the effect is unspecified.
67
68 The name argument points to a string naming a semaphore object. It is
69 unspecified whether the name appears in the file system and is visible
70 to functions that take pathnames as arguments. The name argument con‐
71 forms to the construction rules for a pathname, except that the inter‐
72 pretation of <slash> characters other than the leading <slash> charac‐
73 ter in name is implementation-defined, and that the length limits for
74 the name argument are implementation-defined and need not be the same
75 as the pathname limits {PATH_MAX} and {NAME_MAX}. If name begins with
76 the <slash> character, then processes calling sem_open() with the same
77 value of name shall refer to the same semaphore object, as long as that
78 name has not been removed. If name does not begin with the <slash>
79 character, the effect is implementation-defined.
80
81 If a process makes multiple successful calls to sem_open() with the
82 same value for name, the same semaphore address shall be returned for
83 each such successful call, provided that there have been no calls to
84 sem_unlink() for this semaphore, and at least one previous successful
85 sem_open() call for this semaphore has not been matched with a
86 sem_close() call.
87
88 References to copies of the semaphore produce undefined results.
89
91 Upon successful completion, the sem_open() function shall return the
92 address of the semaphore. Otherwise, it shall return a value of
93 SEM_FAILED and set errno to indicate the error. The symbol SEM_FAILED
94 is defined in the <semaphore.h> header. No successful return from
95 sem_open() shall return the value SEM_FAILED.
96
98 If any of the following conditions occur, the sem_open() function shall
99 return SEM_FAILED and set errno to the corresponding value:
100
101 EACCES The named semaphore exists and the permissions specified by
102 oflag are denied, or the named semaphore does not exist and per‐
103 mission to create the named semaphore is denied.
104
105 EEXIST O_CREAT and O_EXCL are set and the named semaphore already
106 exists.
107
108 EINTR The sem_open() operation was interrupted by a signal.
109
110 EINVAL The sem_open() operation is not supported for the given name, or
111 O_CREAT was specified in oflag and value was greater than
112 {SEM_VALUE_MAX}.
113
114 EMFILE Too many semaphore descriptors or file descriptors are currently
115 in use by this process.
116
117 ENFILE Too many semaphores are currently open in the system.
118
119 ENOENT O_CREAT is not set and the named semaphore does not exist.
120
121 ENOMEM There is insufficient memory for the creation of the new named
122 semaphore.
123
124 ENOSPC There is insufficient space on a storage device for the creation
125 of the new named semaphore.
126
127 If any of the following conditions occur, the sem_open() function may
128 return SEM_FAILED and set errno to the corresponding value:
129
130 ENAMETOOLONG
131 The length of the name argument exceeds {_POSIX_PATH_MAX} on
132 systems that do not support the XSI option or exceeds
133 {_XOPEN_PATH_MAX} on XSI systems, or has a pathname component
134 that is longer than {_POSIX_NAME_MAX} on systems that do not
135 support the XSI option or longer than {_XOPEN_NAME_MAX} on XSI
136 systems.
137
138 The following sections are informative.
139
141 None.
142
144 None.
145
147 Early drafts required an error return value of −1 with the type sem_t *
148 for the sem_open() function, which is not guaranteed to be portable
149 across implementations. The revised text provides the symbolic error
150 code SEM_FAILED to eliminate the type conflict.
151
153 A future version might require the sem_open() and sem_unlink() func‐
154 tions to have semantics similar to normal file system operations.
155
157 semctl(), semget(), semop(), sem_close(), sem_post(), sem_timedwait(),
158 sem_trywait(), sem_unlink()
159
160 The Base Definitions volume of POSIX.1‐2008, <semaphore.h>
161
163 Portions of this text are reprinted and reproduced in electronic form
164 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
165 -- Portable Operating System Interface (POSIX), The Open Group Base
166 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
167 cal and Electronics Engineers, Inc and The Open Group. (This is
168 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
169 event of any discrepancy between this version and the original IEEE and
170 The Open Group Standard, the original IEEE and The Open Group Standard
171 is the referee document. The original Standard can be obtained online
172 at http://www.unix.org/online.html .
173
174 Any typographical or formatting errors that appear in this page are
175 most likely to have been introduced during the conversion of the source
176 files to man page format. To report such errors, see https://www.ker‐
177 nel.org/doc/man-pages/reporting_bugs.html .
178
179
180
181IEEE/The Open Group 2013 SEM_OPEN(3P)