Changes: Ready for 5.13
[man-pages.git] / man3 / sem_open.3
bloba3cbea19ca76a44bcd3cc2f11e0bf7539e2d6953
1 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH SEM_OPEN 3 2021-03-22 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 sem_open \- initialize and open a named semaphore
28 .SH SYNOPSIS
29 .nf
30 .BR "#include <fcntl.h>" "           /* For O_* constants */"
31 .BR "#include <sys/stat.h>" "        /* For mode constants */"
32 .B #include <semaphore.h>
33 .PP
34 .BI "sem_t *sem_open(const char *" name ", int " oflag );
35 .BI "sem_t *sem_open(const char *" name ", int " oflag ,
36 .BI "                mode_t " mode ", unsigned int " value );
37 .fi
38 .PP
39 Link with \fI\-pthread\fP.
40 .SH DESCRIPTION
41 .BR sem_open ()
42 creates a new POSIX semaphore or opens an existing semaphore.
43 The semaphore is identified by
44 .IR name .
45 For details of the construction of
46 .IR name ,
47 see
48 .BR sem_overview (7).
49 .PP
50 The
51 .I oflag
52 argument specifies flags that control the operation of the call.
53 (Definitions of the flags values can be obtained by including
54 .IR <fcntl.h> .)
56 .B O_CREAT
57 is specified in
58 .IR oflag ,
59 then the semaphore is created if
60 it does not already exist.
61 The owner (user ID) of the semaphore is set to the effective
62 user ID of the calling process.
63 The group ownership (group ID) is set to the effective group ID
64 of the calling process.
65 .\" In reality the filesystem IDs are used on Linux.
66 If both
67 .B O_CREAT
68 and
69 .B O_EXCL
70 are specified in
71 .IR oflag ,
72 then an error is returned if a semaphore with the given
73 .I name
74 already exists.
75 .PP
77 .B O_CREAT
78 is specified in
79 .IR oflag ,
80 then two additional arguments must be supplied.
81 The
82 .I mode
83 argument specifies the permissions to be placed on the new semaphore,
84 as for
85 .BR open (2).
86 (Symbolic definitions for the permissions bits can be obtained by including
87 .IR <sys/stat.h> .)
88 The permissions settings are masked against the process umask.
89 Both read and write permission should be granted to each class of
90 user that will access the semaphore.
91 The
92 .I value
93 argument specifies the initial value for the new semaphore.
95 .B O_CREAT
96 is specified, and a semaphore with the given
97 .I name
98 already exists, then
99 .I mode
101 .I value
102 are ignored.
103 .SH RETURN VALUE
104 On success,
105 .BR sem_open ()
106 returns the address of the new semaphore;
107 this address is used when calling other semaphore-related functions.
108 On error,
109 .BR sem_open ()
110 returns
111 .BR SEM_FAILED ,
112 with
113 .I errno
114 set to indicate the error.
115 .SH ERRORS
117 .B EACCES
118 The semaphore exists, but the caller does not have permission to
119 open it.
121 .B EEXIST
122 Both
123 .B O_CREAT
125 .B O_EXCL
126 were specified in
127 .IR oflag ,
128 but a semaphore with this
129 .I name
130 already exists.
132 .B EINVAL
133 .I value
134 was greater than
135 .BR SEM_VALUE_MAX .
137 .B EINVAL
138 .I name
139 consists of just "/", followed by no other characters.
141 .B EMFILE
142 The per-process limit on the number of open file descriptors has been reached.
144 .B ENAMETOOLONG
145 .I name
146 was too long.
148 .B ENFILE
149 The system-wide limit on the total number of open files has been reached.
151 .B ENOENT
153 .B O_CREAT
154 flag was not specified in
155 .IR oflag
156 and no semaphore with this
157 .I name
158 exists;
160 .\" this error can occur if we have a name of the (nonportable) form
161 .\" /dir/name, and the directory /dev/shm/dir does not exist.
162 .B O_CREAT
163 was specified, but
164 .I name
165 wasn't well formed.
167 .B ENOMEM
168 Insufficient memory.
169 .SH ATTRIBUTES
170 For an explanation of the terms used in this section, see
171 .BR attributes (7).
172 .ad l
175 allbox;
176 lbx lb lb
177 l l l.
178 Interface       Attribute       Value
180 .BR sem_open ()
181 T}      Thread safety   MT-Safe
185 .sp 1
186 .SH CONFORMING TO
187 POSIX.1-2001, POSIX.1-2008.
188 .SH SEE ALSO
189 .BR sem_close (3),
190 .BR sem_getvalue (3),
191 .BR sem_post (3),
192 .BR sem_unlink (3),
193 .BR sem_wait (3),
194 .BR sem_overview (7)