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