semget(2) System Calls Manual semget(2) semget - System V LIBRARY Standard C library (libc, -lc) #include int semget(key_t key, int nsems, int semflg); semget() System V, key. ( semflg key IPC_PRIVATE) . nsems , key IPC_PRIVATE key , semflg IPC_CREAT. semflg IPC_CREAT IPC_EXCL key , semget() errno EEXIST ( O_CREAT | O_EXCL open(2)). 9 semflg (, .) . mode open(2) ( , ). semget() semid_ds (. semctl(2)) : o sem_perm.cuid sem_perm.uid . o sem_perm.cgid sem_perm.gid . o 9 sem_perm.mode 9 semflg. o sem_nsems nsems. o sem_otime 0. o sem_ctime . , nsems 0 ( ). nsems 0 (SEMMSL). , . On success, semget() returns the semaphore set identifier (a nonnegative integer). On failure, -1 is returned, and errno is set to indicate the error. EACCES key , CAP_IPC_OWNER , IPC. EEXIST semflg IPC_CREAT IPC_EXCL, key. EINVAL nsems 0 (SEMMSL). EINVAL , key, , nsems , . ENOENT key semflg IPC_CREAT. ENOMEM , . ENOSPC , (SEMMNI) (SEMMNS). POSIX.1-2008. SVr4, POSIX.1-2001. IPC_PRIVATE , key_t. key , 9- semflg ( ). ( POSIX.1-2001 POSIX.1-2008 , POSIX.1-2008 , 0). Linux, , 0, : . semctl(2) SETVAL SETALL. , , sem_otime , semctl(2) IPC_STAT. , semget(): 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 : ( Linux /proc/sys/kernel/sem). , SEMMSL SEMMNI. IPC_PRIVATE, , , IPC_NEW . The 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 2 Next, 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 = 9 Finally, 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 /* t_semget.c Licensed under GNU General Public License v2 or later. */ #include #include #include #include #include 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); } semctl(2), semop(2), ftok(3), capabilities(7), sem_overview(7), sysvipc(7) Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , <>. Linux man-pages 6.8 2 2024 . semget(2)