1sem_open(3C) Standard C Library Functions sem_open(3C)
2
3
4
6 sem_open - initialize/open a named semaphore
7
9 #include <semaphore.h>
10
11 sem_t *sem_open(const char *name, int oflag,
12 /* unsigned long mode, unsigned int value */ ...);
13
14
16 The sem_open() function establishes a connection between a named sema‐
17 phore and a process (or LWP or thread). Following a call to sem_open()
18 with semaphore name name, the process may reference the semaphore asso‐
19 ciated with name using the address returned from the call. This sema‐
20 phore may be used in subsequent calls to sem_wait(3C), sem_trywait(3C),
21 sem_post(3C), and sem_close(3C). The semaphore remains usable by this
22 process until the semaphore is closed by a successful call to
23 sem_close(3C), _Exit(2), or one of the exec functions.
24
25
26 The oflag argument controls whether the semaphore is created or merely
27 accessed by the call to sem_open(). The following flag bits may be set
28 in oflag:
29
30 O_CREAT This flag is used to create a semaphore if it does not
31 already exist. If O_CREAT is set and the semaphore already
32 exists, then O_CREAT has no effect, except as noted under
33 O_EXCL. Otherwise, sem_open() creates a named semaphore. The
34 O_CREAT flag requires a third and a fourth argument: mode,
35 which is of type mode_t, and value, which is of type
36 unsigned int. The semaphore is created with an initial value
37 of value. Valid initial values for semaphores are less than
38 or equal to SEM_VALUE_MAX.
39
40 The user ID of the semaphore is set to the effective user ID
41 of the process; the group ID of the semaphore is set to a
42 system default group ID or to the effective group ID of the
43 process. The permission bits of the semaphore are set to the
44 value of the mode argument except those set in the file mode
45 creation mask of the process (see umask(2)). When bits in
46 mode other than the file permission bits are specified, the
47 effect is unspecified.
48
49 After the semaphore named name has been created by
50 sem_open() with the O_CREAT flag, other processes can con‐
51 nect to the semaphore by calling sem_open() with the same
52 value of name.
53
54
55 O_EXCL If O_EXCL and O_CREAT are set, sem_open() fails if the sema‐
56 phore name exists. The check for the existence of the sema‐
57 phore and the creation of the semaphore if it does not exist
58 are atomic with respect to other processes executing
59 sem_open() with O_EXCL and O_CREAT set. If O_EXCL is set and
60 O_CREAT is not set, the effect is undefined.
61
62
63
64 If flags other than O_CREAT and O_EXCL are specified in the oflag
65 parameter, the effect is unspecified.
66
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. The first character of
72 name must be a slash (/) character and the remaining characters of
73 name cannot include any slash characters. For maximum portability,
74 name should include no more than 14 characters, but this limit is not
75 enforced.
76
77
78 If a process makes multiple successful calls to sem_open() with the
79 same value for name, the same semaphore address is returned for each
80 such successful call, provided that there have been no calls to
81 sem_unlink(3C) for this semaphore.
82
83
84 References to copies of the semaphore produce undefined results.
85
86
87 The sem_init(3C) function is used with unnamed semaphores.
88
90 Upon successful completion, the function returns the address of the
91 semaphore. Otherwise, it will return a value of SEM_FAILED and set
92 errno to indicate the error. The symbol SEM_FAILED is defined in the
93 header <semaphore.h>. No successful return from sem_open() will return
94 the value SEM_FAILED.
95
97 If any of the following conditions occur, the sem_open() function will
98 return SEM_FAILED and set errno to the corresponding value:
99
100 EACCES The named semaphore exists and the O_RDWR permissions
101 are denied, or the named semaphore does not exist and
102 permission to create the named semaphore is denied.
103
104
105 EEXIST O_CREAT and O_EXCL are set and the named semaphore
106 already exists.
107
108
109 EINTR The sem_open() function was interrupted by a signal.
110
111
112 EINVAL The sem_open() operation is not supported for the given
113 name, or O_CREAT was set in oflag and value is greater
114 than SEM_VALUE_MAX.
115
116
117 EMFILE The number of open semaphore descriptors in this
118 process exceeds SEM_NSEMS_MAX, or the number of open
119 file descriptors in this process exceeds OPEN_MAX.
120
121
122 ENAMETOOLONG The length of name string exceeds PATH_MAX, or a path‐
123 name component is longer than NAME_MAX while
124 _POSIX_NO_TRUNC is in effect.
125
126
127 ENFILE Too many semaphores are currently open in the system.
128
129
130 ENOENT O_CREAT is not set and the named semaphore does not
131 exist.
132
133
134 ENOSPC There is insufficient space for the creation of the new
135 named semaphore.
136
137
138 ENOSYS The sem_open() function is not supported by the system.
139
140
142 See attributes(5) for descriptions of the following attributes:
143
144
145
146
147 ┌─────────────────────────────┬─────────────────────────────┐
148 │ATTRIBUTE TYPE │ATTRIBUTE VALUE │
149 ├─────────────────────────────┼─────────────────────────────┤
150 │Interface Stability │Committed │
151 ├─────────────────────────────┼─────────────────────────────┤
152 │MT-Level │MT-Safe │
153 ├─────────────────────────────┼─────────────────────────────┤
154 │Standard │See standards(5). │
155 └─────────────────────────────┴─────────────────────────────┘
156
158 exec(2), exit(2), umask(2), sem_close(3C), sem_init(3C), sem_post(3C),
159 sem_unlink(3C), sem_wait(3C), sysconf(3C), attributes(5), standards(5)
160
161
162
163SunOS 5.11 9 Jul 2009 sem_open(3C)