summaryrefslogtreecommitdiffstats
path: root/man3p/semget.3p
blob: cbbda746c3016162bb38636b3c7210b52adb72ef (plain) (blame)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved 
.TH "SEMGET" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\" semget 
.SH NAME
semget \- get set of XSI semaphores
.SH SYNOPSIS
.LP
\fB#include <sys/sem.h>
.br
.sp
int semget(key_t\fP \fIkey\fP\fB, int\fP \fInsems\fP\fB, int\fP \fIsemflg\fP\fB);
\fP
\fB
.br
\fP
.SH DESCRIPTION
.LP
The \fIsemget\fP() function operates on XSI semaphores (see the Base
Definitions volume of IEEE\ Std\ 1003.1-2001, Section 4.15, Semaphore).
It is unspecified whether this function interoperates
with the realtime interprocess communication facilities defined in
\fIRealtime\fP .
.LP
The \fIsemget\fP() function shall return the semaphore identifier
associated with \fIkey\fP.
.LP
A semaphore identifier with its associated \fBsemid_ds\fP data structure
and its associated set of \fInsems\fP semaphores (see
\fI<sys/sem.h>\fP) is created for \fIkey\fP if one of the following
is true:
.IP " *" 3
The argument \fIkey\fP is equal to IPC_PRIVATE.
.LP
.IP " *" 3
The argument \fIkey\fP does not already have a semaphore identifier
associated with it and (\fIsemflg\fP &IPC_CREAT) is
non-zero.
.LP
.LP
Upon creation, the \fBsemid_ds\fP data structure associated with the
new semaphore identifier is initialized as follows:
.IP " *" 3
In the operation permissions structure \fIsem_perm.cuid\fP, \fIsem_perm.uid\fP,
\fIsem_perm.cgid\fP, and \fIsem_perm.gid\fP
shall be set equal to the effective user ID and effective group ID,
respectively, of the calling process.
.LP
.IP " *" 3
The low-order 9 bits of \fIsem_perm.mode\fP shall be set equal to
the low-order 9 bits of \fIsemflg\fP.
.LP
.IP " *" 3
The variable \fIsem_nsems\fP shall be set equal to the value of \fInsems\fP.
.LP
.IP " *" 3
The variable \fIsem_otime\fP shall be set equal to 0 and \fIsem_ctime\fP
shall be set equal to the current time.
.LP
.IP " *" 3
The data structure associated with each semaphore in the set shall
not be initialized. The \fIsemctl\fP() function with the command SETVAL
or SETALL can be used to initialize each
semaphore.
.LP
.SH RETURN VALUE
.LP
Upon successful completion, \fIsemget\fP() shall return a non-negative
integer, namely a semaphore identifier; otherwise, it
shall return -1 and set \fIerrno\fP to indicate the error.
.SH ERRORS
.LP
The \fIsemget\fP() function shall fail if:
.TP 7
.B EACCES
A semaphore identifier exists for \fIkey\fP, but operation permission
as specified by the low-order 9 bits of \fIsemflg\fP
would not be granted; see \fIXSI Interprocess Communication\fP .
.TP 7
.B EEXIST
A semaphore identifier exists for the argument \fIkey\fP but ((\fIsemflg\fP
&IPC_CREAT) &&(\fIsemflg\fP
&IPC_EXCL)) is non-zero.
.TP 7
.B EINVAL
The value of \fInsems\fP is either less than or equal to 0 or greater
than the system-imposed limit, or a semaphore identifier
exists for the argument \fIkey\fP, but the number of semaphores in
the set associated with it is less than \fInsems\fP and
\fInsems\fP is not equal to 0.
.TP 7
.B ENOENT
A semaphore identifier does not exist for the argument \fIkey\fP and
(\fIsemflg\fP &IPC_CREAT) is equal to 0.
.TP 7
.B ENOSPC
A semaphore identifier is to be created but the system-imposed limit
on the maximum number of allowed semaphores system-wide
would be exceeded.
.sp
.LP
\fIThe following sections are informative.\fP
.SH EXAMPLES
.SS Creating a Semaphore Identifier
.LP
The following example gets a unique semaphore key using the \fIftok\fP()
function, then
gets a semaphore ID associated with that key using the \fIsemget\fP()
function (the first call also tests to make sure the
semaphore exists). If the semaphore does not exist, the program creates
it, as shown by the second call to \fIsemget\fP(). In
creating the semaphore for the queuing process, the program attempts
to create one semaphore with read/write permission for all. It
also uses the IPC_EXCL flag, which forces \fIsemget\fP() to fail if
the semaphore already exists.
.LP
After creating the semaphore, the program uses a call to \fIsemop\fP()
to initialize it
to the values in the \fIsbuf\fP array. The number of processes that
can execute concurrently without queuing is initially set to
2. The final call to \fIsemget\fP() creates a semaphore identifier
that can be used later in the program.
.sp
.RS
.nf

\fB#include <sys/types.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <fcntl.h>
#include <limits.h>
\&...
key_t semkey;
int semid, pfd, fv;
struct sembuf sbuf;
char *lgn;
char filename[PATH_MAX+1];
struct stat outstat;
struct passwd *pw;
\&...
/* Get unique key for semaphore. */
if ((semkey = ftok("/tmp", 'a')) == (key_t) -1) {
    perror("IPC error: ftok"); exit(1);
}
.sp

/* Get semaphore ID associated with this key. */
if ((semid = semget(semkey, 0, 0)) == -1) {
.sp

    /* Semaphore does not exist - Create. */
    if ((semid = semget(semkey, 1, IPC_CREAT | IPC_EXCL | S_IRUSR |
        S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) != -1)
    {
        /* Initialize the semaphore. */
        sbuf.sem_num = 0;
        sbuf.sem_op = 2;  /* This is the number of runs
                             without queuing. */
        sbuf.sem_flg = 0;
        if (semop(semid, &sbuf, 1) == -1) {
            perror("IPC error: semop"); exit(1);
        }
    }
    else if (errno == EEXIST) {
        if ((semid = semget(semkey, 0, 0)) == -1) {
            perror("IPC error 1: semget"); exit(1);
        }
    }
    else {
        perror("IPC error 2: semget"); exit(1);
    }
}
\&...
\fP
.fi
.RE
.SH APPLICATION USAGE
.LP
The POSIX Realtime Extension defines alternative interfaces for interprocess
communication. Application developers who need to
use IPC should design their applications so that modules using the
IPC routines described in \fIXSI Interprocess Communication\fP can
be easily modified to use the alternative
interfaces.
.SH RATIONALE
.LP
None.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
\fIXSI Interprocess Communication\fP , \fIRealtime\fP , \fIsemctl\fP()
, \fIsemop\fP() , \fIsem_close\fP() , \fIsem_destroy\fP() , \fIsem_getvalue\fP()
, \fIsem_init\fP() , \fIsem_open\fP() , \fIsem_post\fP() , \fIsem_unlink\fP()
, \fIsem_wait\fP() , the Base Definitions volume of IEEE\ Std\ 1003.1-2001,
\fI<sys/sem.h>\fP
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .