1SEMGET(2) Linux Programmer's Manual SEMGET(2)
2
3
4
6 semget - get a semaphore set identifier
7
9 #include <sys/types.h>
10 #include <sys/ipc.h>
11 #include <sys/sem.h>
12
13 int semget(key_t key, int nsems, int semflg);
14
16 The semget() system call returns the semaphore set identifier associ‐
17 ated with the argument key. A new set of nsems semaphores is created
18 if key has the value IPC_PRIVATE or if no existing semaphore set is
19 associated with key and IPC_CREAT is specified in semflg.
20
21 If semflg specifies both IPC_CREAT and IPC_EXCL and a semaphore set
22 already exists for key, then semget() fails with errno set to EEXIST.
23 (This is analogous to the effect of the combination O_CREAT | O_EXCL
24 for open(2).)
25
26 Upon creation, the least significant 9 bits of the argument semflg
27 define the permissions (for owner, group and others) for the semaphore
28 set. These bits have the same format, and the same meaning, as the
29 mode argument of open(2) (though the execute permissions are not mean‐
30 ingful for semaphores, and write permissions mean permission to alter
31 semaphore values).
32
33 The values of the semaphores in a newly created set are indeterminate.
34 (POSIX.1-2001 is explicit on this point.) Although Linux, like many
35 other implementations, initialises the semaphore values to 0, a porta‐
36 ble application cannot rely on this: it should explicitly initialise
37 the semaphores to the desired values.
38
39 When creating a new semaphore set, semget() initialises the set's asso‐
40 ciated data structure, semid_ds (see semctl(2)), as follows:
41
42 sem_perm.cuid and sem_perm.uid are set to the effective user ID
43 of the calling process.
44
45 sem_perm.cgid and sem_perm.gid are set to the effective group ID
46 of the calling process.
47
48 The least significant 9 bits of sem_perm.mode are set to the
49 least significant 9 bits of semflg.
50
51 sem_nsems is set to the value of nsems.
52
53 sem_otime is set to 0.
54
55 sem_ctime is set to the current time.
56
57 The argument nsems can be 0 (a don't care) when a semaphore set is not
58 being created. Otherwise nsems must be greater than 0 and less than or
59 equal to the maximum number of semaphores per semaphore set (SEMMSL).
60
61 If the semaphore set already exists, the permissions are verified.
62
64 If successful, the return value will be the semaphore set identifier (a
65 nonnegative integer), otherwise -1 is returned, with errno indicating
66 the error.
67
69 On failure errno will be set to one of the following:
70
71 EACCES A semaphore set exists for key, but the calling process does
72 not have permission to access the set, and does not have the
73 CAP_IPC_OWNER capability.
74
75 EEXIST A semaphore set exists for key and semflg specified both
76 IPC_CREAT and IPC_EXCL.
77
78 EINVAL nsems is less than 0 or greater than the limit on the number
79 of semaphores per semaphore set (SEMMSL), or a semaphore set
80 corresponding to key already exists, and nsems is larger
81 than the number of semaphores in that set.
82
83 ENOENT No semaphore set exists for key and semflg did not specify
84 IPC_CREAT.
85
86 ENOMEM A semaphore set has to be created but the system does not
87 have enough memory for the new data structure.
88
89 ENOSPC A semaphore set has to be created but the system limit for
90 the maximum number of semaphore sets (SEMMNI), or the system
91 wide maximum number of semaphores (SEMMNS), would be
92 exceeded.
93
95 IPC_PRIVATE isn't a flag field but a key_t type. If this special value
96 is used for key, the system call ignores everything but the least sig‐
97 nificant 9 bits of semflg and creates a new semaphore set (on success).
98
99 The following limits on semaphore set resources affect the semget()
100 call:
101
102 SEMMNI System wide maximum number of semaphore sets: policy depen‐
103 dent (on Linux, this limit can be read and modified via the
104 fourth field of /proc/sys/kernel/sem).
105
106 SEMMSL Maximum number of semaphores per semid: implementation
107 dependent (on Linux, this limit can be read and modified via
108 the first field of /proc/sys/kernel/sem).
109
110 SEMMNS System wide maximum number of semaphores: policy dependent
111 (on Linux, this limit can be read and modified via the sec‐
112 ond field of /proc/sys/kernel/sem). Values greater than
113 SEMMSL * SEMMNI makes it irrelevant.
114
116 The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
117 clearly show its function.
118
119 The semaphores in a set are not initialised by semget(). In order to
120 initialise the semaphores, semctl(2) must be used to perform a SETVAL
121 or a SETALL operation on the semaphore set. (Where multiple peers do
122 not know who will be the first to initialise the set, checking for a
123 non-zero sem_otime in the associated data structure retrieved by a sem‐
124 ctl() IPC_STAT operation can be used to avoid races.)
125
127 SVr4, POSIX.1-2001.
128
130 semctl(2), semop(2), ftok(3), capabilities(7), sem_overview(7),
131 svipc(7)
132
133
134
135Linux 2.6.6 2004-05-27 SEMGET(2)