1sem_open(3) Library Functions Manual sem_open(3)
2
3
4
6 sem_open - initialize and open a named semaphore
7
9 POSIX threads library (libpthread, -lpthread)
10
12 #include <fcntl.h> /* For O_* constants */
13 #include <sys/stat.h> /* For mode constants */
14 #include <semaphore.h>
15
16 sem_t *sem_open(const char *name, int oflag);
17 sem_t *sem_open(const char *name, int oflag,
18 mode_t mode, unsigned int value);
19
21 sem_open() creates a new POSIX semaphore or opens an existing sema‐
22 phore. The semaphore is identified by name. For details of the con‐
23 struction of name, see sem_overview(7).
24
25 The oflag argument specifies flags that control the operation of the
26 call. (Definitions of the flags values can be obtained by including
27 <fcntl.h>.) If O_CREAT is specified in oflag, then the semaphore is
28 created if it does not already exist. The owner (user ID) of the sema‐
29 phore is set to the effective user ID of the calling process. The
30 group ownership (group ID) is set to the effective group ID of the
31 calling process. If both O_CREAT and O_EXCL are specified in oflag,
32 then an error is returned if a semaphore with the given name already
33 exists.
34
35 If O_CREAT is specified in oflag, then two additional arguments must be
36 supplied. The mode argument specifies the permissions to be placed on
37 the new semaphore, as for open(2). (Symbolic definitions for the per‐
38 missions bits can be obtained by including <sys/stat.h>.) The permis‐
39 sions settings are masked against the process umask. Both read and
40 write permission should be granted to each class of user that will ac‐
41 cess the semaphore. The value argument specifies the initial value for
42 the new semaphore. If O_CREAT is specified, and a semaphore with the
43 given name already exists, then mode and value are ignored.
44
46 On success, sem_open() returns the address of the new semaphore; this
47 address is used when calling other semaphore-related functions. On er‐
48 ror, sem_open() returns SEM_FAILED, with errno set to indicate the er‐
49 ror.
50
52 EACCES The semaphore exists, but the caller does not have permission to
53 open it.
54
55 EEXIST Both O_CREAT and O_EXCL were specified in oflag, but a semaphore
56 with this name already exists.
57
58 EINVAL value was greater than SEM_VALUE_MAX.
59
60 EINVAL name consists of just "/", followed by no other characters.
61
62 EMFILE The per-process limit on the number of open file descriptors has
63 been reached.
64
65 ENAMETOOLONG
66 name was too long.
67
68 ENFILE The system-wide limit on the total number of open files has been
69 reached.
70
71 ENOENT The O_CREAT flag was not specified in oflag and no semaphore
72 with this name exists; or, O_CREAT was specified, but name
73 wasn't well formed.
74
75 ENOMEM Insufficient memory.
76
78 For an explanation of the terms used in this section, see at‐
79 tributes(7).
80
81 ┌────────────────────────────────────────────┬───────────────┬─────────┐
82 │Interface │ Attribute │ Value │
83 ├────────────────────────────────────────────┼───────────────┼─────────┤
84 │sem_open() │ Thread safety │ MT-Safe │
85 └────────────────────────────────────────────┴───────────────┴─────────┘
86
88 POSIX.1-2008.
89
91 POSIX.1-2001.
92
94 sem_close(3), sem_getvalue(3), sem_post(3), sem_unlink(3), sem_wait(3),
95 sem_overview(7)
96
97
98
99Linux man-pages 6.04 2023-03-30 sem_open(3)