semget - Online Linux Manual PageSection : 2
Updated : 2023-02-05
Source : Linux man-pages 6.03
NAMEsemget − get a System V semaphore set identifier
LIBRARYStandard C library (libc, −lc)
SYNOPSIS#include <sys/sem.h>int semget(key_t key, int nsems, int semflg);
DESCRIPTIONThe semget() system call returns the System V semaphore set identifier associated with the argument key. It may be used either to obtain the identifier of a previously created semaphore set (when semflg is zero and key does not have the value IPC_PRIVATE), or to create a new set. A new set of nsems semaphores is created if key has the value IPC_PRIVATE or if no existing semaphore set is associated with key and IPC_CREAT is specified in semflg. If semflg specifies both IPC_CREAT and IPC_EXCL and a semaphore set already exists for key, then semget() fails with errno set to EEXIST. (This is analogous to the effect of the combination O_CREAT | O_EXCL for open(2).) Upon creation, the least significant 9 bits of the argument semflg define the permissions (for owner, group, and others) for the semaphore set. These bits have the same format, and the same meaning, as the mode argument of open(2) (though the execute permissions are not meaningful for semaphores, and write permissions mean permission to alter semaphore values). When creating a new semaphore set, semget() initializes the set's associated data structure, semid_ds (see semctl(2)), as follows: • sem_perm.cuid and sem_perm.uid are set to the effective user ID of the calling process. • sem_perm.cgid and sem_perm.gid are set to the effective group ID of the calling process. • The least significant 9 bits of sem_perm.mode are set to the least significant 9 bits of semflg. • sem_nsems is set to the value of nsems. • sem_otime is set to 0. • sem_ctime is set to the current time. The argument nsems can be 0 (a don't care) when a semaphore set is not being created. Otherwise, nsems must be greater than 0 and less than or equal to the maximum number of semaphores per semaphore set (SEMMSL). If the semaphore set already exists, the permissions are verified.
RETURN VALUEOn success, semget() returns the semaphore set identifier (a nonnegative integer). On failure, −1 is returned, and errno is set to indicate the error.
ERRORSEACCES A semaphore set exists for key, but the calling process does not have permission to access the set, and does not have the CAP_IPC_OWNER capability in the user namespace that governs its IPC namespace. EEXIST IPC_CREAT and IPC_EXCL were specified in semflg, but a semaphore set already exists for key. EINVAL nsems is less than 0 or greater than the limit on the number of semaphores per semaphore set (SEMMSL). EINVAL A semaphore set corresponding to key already exists, but nsems is larger than the number of semaphores in that set. ENOENT No semaphore set exists for key and semflg did not specify IPC_CREAT. ENOMEM A semaphore set has to be created but the system does not have enough memory for the new data structure. ENOSPC A semaphore set has to be created but the system limit for the maximum number of semaphore sets (SEMMNI), or the system wide maximum number of semaphores (SEMMNS), would be exceeded.
STANDARDSSVr4, POSIX.1-2001.
NOTESIPC_PRIVATE isn't a flag field but a key_t type. If this special value is used for key, the system call ignores all but the least significant 9 bits of semflg and creates a new semaphore set (on success).
Semaphore initializationThe values of the semaphores in a newly created set are indeterminate. (POSIX.1-2001 and POSIX.1-2008 are explicit on this point, although POSIX.1-2008 notes that a future version of the standard may require an implementation to initialize the semaphores to 0.) Although Linux, like many other implementations, initializes the semaphore values to 0, a portable application cannot rely on this: it should explicitly initialize the semaphores to the desired values. Initialization can be done using semctl(2) SETVAL or SETALL operation. Where multiple peers do not know who will be the first to initialize the set, checking for a nonzero sem_otime in the associated data structure retrieved by a semctl(2) IPC_STAT operation can be used to avoid races.
Semaphore limitsThe following limits on semaphore set resources affect the semget() call: SEMMNI System-wide limit on the number of semaphore sets. Before Linux 3.19, the default value for this limit was 128. Since Linux 3.19, the default value is 32,000. On Linux, this limit can be read and modified via the fourth field of /proc/sys/kernel/sem. SEMMSL Maximum number of semaphores per semaphore ID. Before Linux 3.19, the default value for this limit was 250. Since Linux 3.19, the default value is 32,000. On Linux, this limit can be read and modified via the first field of /proc/sys/kernel/sem. SEMMNS System-wide limit on the number of semaphores: policy dependent (on Linux, this limit can be read and modified via the second field of /proc/sys/kernel/sem). Note that the number of semaphores system-wide is also limited by the product of SEMMSL and SEMMNI.
BUGSThe name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more clearly show its function.
EXAMPLESThe program shown below uses semget() to create a new semaphore set or retrieve the ID of an existing set. It generates the key for semget() using ftok(3). The first two command-line arguments are used as the pathname and proj_id arguments for ftok(3). The third command-line argument is an integer that specifies the nsems argument for semget(). Command-line options can be used to specify the IPC_CREAT (−c) and IPC_EXCL (−x) flags for the call to semget(). The usage of this program is demonstrated below. We first create two files that will be used to generate keys using ftok(3), create two semaphore sets using those files, and then list the sets using ipcs(1): $ touch mykey mykey2
$ ./t_semget −c mykey p 1
ID = 9
$ ./t_semget −c mykey2 p 2
ID = 10
$ ipcs −s
−−−−−− Semaphore Arrays −−−−−−−−
key semid owner perms nsems
0x7004136d 9 mtk 600 1
0x70041368 10 mtk 600 2Next, we demonstrate that when semctl(2) is given the same key (as generated by the same arguments to ftok(3)), it returns the ID of the already existing semaphore set: $ ./t_semget −c mykey p 1
ID = 9Finally, we demonstrate the kind of collision that can occur when ftok(3) is given different pathname arguments that have the same inode number: $ ln mykey link
$ ls −i1 link mykey
2233197 link
2233197 mykey
$ ./t_semget link p 1 # Generates same key as 'mykey'
ID = 9
Program source /* t_semget.c
Licensed under GNU General Public License v2 or later.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
static void
usage(const char *pname)
{
fprintf(stderr, "Usage: %s [−cx] pathname proj−id num−sems\n",
pname);
fprintf(stderr, " −c Use IPC_CREAT flag\n");
fprintf(stderr, " −x Use IPC_EXCL flag\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
int semid, nsems, flags, opt;
key_t key;
flags = 0;
while ((opt = getopt(argc, argv, "cx")) != −1) {
switch (opt) {
case 'c': flags |= IPC_CREAT; break;
case 'x': flags |= IPC_EXCL; break;
default: usage(argv[0]);
}
}
if (argc != optind + 3)
usage(argv[0]);
key = ftok(argv[optind], argv[optind + 1][0]);
if (key == −1) {
perror("ftok");
exit(EXIT_FAILURE);
}
nsems = atoi(argv[optind + 2]);
semid = semget(key, nsems, flags | 0600);
if (semid == −1) {
perror("semget");
exit(EXIT_FAILURE);
}
printf("ID = %d\n", semid);
exit(EXIT_SUCCESS);
}
SEE ALSOsemctl(2), semop(2), ftok(3), capabilities(7), sem_overview(7), sysvipc(7) 0
Johanes Gumabo
Data Size : 27,198 byte
man-semget.2Build : 2024-12-05, 20:55 :
Visitor Screen : x
Visitor Counter ( page / site ) : 3 / 200,780
Visitor ID : :
Visitor IP : 3.146.178.220 :
Visitor Provider : AMAZON-02 :
Provider Position ( lat x lon ) : 39.962500 x -83.006100 : x
Provider Accuracy Radius ( km ) : 1000 :
Provider City : Columbus :
Provider Province : Ohio , : ,
Provider Country : United States :
Provider Continent : North America :
Visitor Recorder : Version :
Visitor Recorder : Library :
Online Linux Manual Page : Version : Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page : Library : lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page : Library : lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base : Version : Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base : Library : lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36
Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.