summaryrefslogtreecommitdiff
path: root/man3/sem_open.3
blob: 829a81c9f389e67841940339bb49e3a5ffc7d0d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH SEM_OPEN 3 2008-05-29 "Linux" "Linux Programmer's Manual"
.SH NAME
sem_open \- initialize and open a named semaphore
.SH SYNOPSIS
.nf
.BR "#include <fcntl.h>" "           /* For O_* constants */"
.BR "#include <sys/stat.h>" "        /* For mode constants */"
.B #include <semaphore.h>
.sp
.BI "sem_t *sem_open(const char *" name ", int " oflag );
.BI "sem_t *sem_open(const char *" name ", int " oflag ", "
.BI "                mode_t " mode ", unsigned int " value );
.fi
.sp
Link with \fI\-lrt\fP or \fI\-pthread\fP.
.SH DESCRIPTION
.BR sem_open ()
creates a new POSIX semaphore or opens an existing semaphore.
The semaphore is identified by
.IR name .
For details of the construction of
.IR name ,
see
.BR sem_overview (7).

The
.I oflag
argument specifies flags that control the operation of the call.
(Definitions of the flags values can be obtained by including
.IR <fcntl.h> .)
If
.B O_CREAT
is specified in
.IR oflag ,
then the semaphore is created if
it does not already exist.
The owner (user ID) of the semaphore is set to the effective
user ID of the calling process.
The group ownership (group ID) is set to the effective group ID
of the calling process.
.\" In reality the file system IDs are used on Linux.
If both
.B O_CREAT
and
.B O_EXCL
are specified in
.IR oflag ,
then an error is returned if a semaphore with the given
.I name
already exists.
.PP
If
.B O_CREAT
is specified in
.IR oflag ,
then two additional arguments must be supplied.
The
.I mode
argument specifies the permissions to be placed on the new semaphore,
as for
.BR open (2).
(Symbolic definitions for the permissions bits can be obtained by including
.IR <sys/stat.h> .)
The permissions settings are masked against the process umask.
Both read and write permission should be granted to each class of
user that will access the semaphore.
The
.I value
argument specifies the initial value for the new semaphore.
If
.B O_CREAT
is specified, and a semaphore with the given
.I name
already exists, then
.I mode
and
.I value
are ignored.
.SH RETURN VALUE
On success,
.BR sem_open ()
returns the address of the new semaphore;
this address is used when calling other semaphore-related functions.
On error,
.BR sem_open ()
returns
.BR SEM_FAILED ,
with
.I errno
set to indicate the error.
.SH ERRORS
.TP
.B EACCES
The semaphore exists, but the caller does not have permission to
open it.
.TP
.B EEXIST
Both
.B O_CREAT
and
.B O_EXCL
were specified in
.IR oflag ,
but a semaphore with this
.I name
already exists.
.TP
.B EINVAL
.I value
was greater than
.BR SEM_VALUE_MAX .
.TP
.B EMFILE
The process already has the maximum number of files and open.
.TP
.B ENAMETOOLONG
.I name
was too long.
.TP
.B ENFILE
The system limit on the total number of open files has been reached.
.TP
.B ENOENT
The
.B O_CREAT
flag was not specified in
.IR oflag ,
and no semaphore with this
.I name
exists.
.TP
.B ENOMEM
Insufficient memory.
.SH CONFORMING TO
POSIX.1-2001.
.SH "SEE ALSO"
.BR sem_close (3),
.BR sem_getvalue (3),
.BR sem_post (3),
.BR sem_unlink (3),
.BR sem_wait (3),
.BR sem_overview (7)