Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


semget(2) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |STANDARDS |HISTORY |NOTES |BUGS |EXAMPLES |SEE ALSO |COLOPHON

semget(2)                  System Calls Manualsemget(2)

NAME        top

       semget - get a System V semaphore set identifier

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <sys/sem.h>int semget(key_tkey, intnsems, intsemflg);

DESCRIPTION        top

       Thesemget() system call returns the System V semaphore set       identifier associated with the argumentkey.  It may be used       either to obtain the identifier of a previously created semaphore       set (whensemflg is zero andkey does not have the valueIPC_PRIVATE), or to create a new set.       A new set ofnsems semaphores is created ifkey has the valueIPC_PRIVATEor if no existing semaphore set is associated withkey       andIPC_CREATis specified insemflg.       Ifsemflg specifies bothIPC_CREATandIPC_EXCLand a semaphore       set already exists forkey, thensemget() fails witherrno set toEEXIST.  (This is analogous to the effect of the combinationO_CREAT | O_EXCLforopen(2).)       Upon creation, the least significant 9 bits of the argumentsemflg       define the permissions (for owner, group, and others) for the       semaphore set.  These bits have the same format, and the same       meaning, as themode argument ofopen(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 (seesemctl(2)), as follows:       •sem_perm.cuid andsem_perm.uid are set to the effective user ID          of the calling process.       •sem_perm.cgid andsem_perm.gid are set to the effective group          ID of the calling process.       •  The least significant 9 bits ofsem_perm.mode are set to the          least significant 9 bits ofsemflg.       •sem_nsems is set to the value ofnsems.       •sem_otime is set to 0.       •sem_ctime is set to the current time.       The argumentnsems 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 VALUE        top

       On success,semget() returns the semaphore set identifier (a       nonnegative integer).  On failure, -1 is returned, anderrno is       set to indicate the error.

ERRORS        top

EACCESA semaphore set exists forkey, but the calling process              does not have permission to access the set, and does not              have theCAP_IPC_OWNERcapability in the user namespace              that governs its IPC namespace.EEXIST IPC_CREATandIPC_EXCLwere specified insemflg, but a              semaphore set already exists forkey.EINVALnsems is less than 0 or greater than the limit on the              number of semaphores per semaphore set (SEMMSL).EINVALA semaphore set corresponding tokey already exists, butnsems is larger than the number of semaphores in that set.ENOENTNo semaphore set exists forkey andsemflg did not specifyIPC_CREAT.ENOMEMA semaphore set has to be created but the system does not              have enough memory for the new data structure.ENOSPCA 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.

STANDARDS        top

       POSIX.1-2008.

HISTORY        top

       SVr4, POSIX.1-2001.

NOTES        top

IPC_PRIVATEisn't a flag field but akey_t type.  If this special       value is used forkey, the system call ignores all but the least       significant 9 bits ofsemflg and creates a new semaphore set (on       success).Semaphore initialization       The 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 usingsemctl(2)SETVALorSETALL       operation.  Where multiple peers do not know who will be the first       to initialize the set, checking for a nonzerosem_otime in the       associated data structure retrieved by asemctl(2)IPC_STAT       operation can be used to avoid races.Semaphore limits       The following limits on semaphore set resources affect thesemget() call:SEMMNISystem-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.SEMMSLMaximum 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.SEMMNSSystem-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 ofSEMMSLandSEMMNI.

BUGS        top

       The name choiceIPC_PRIVATEwas perhaps unfortunate,IPC_NEWwould       more clearly show its function.

EXAMPLES        top

       The program shown below usessemget() to create a new semaphore       set or retrieve the ID of an existing set.  It generates thekey       forsemget() usingftok(3).  The first two command-line arguments       are used as thepathname andproj_id arguments forftok(3).  The       third command-line argument is an integer that specifies thensems       argument forsemget().  Command-line options can be used to       specify theIPC_CREAT(-c) andIPC_EXCL(-x) flags for the call tosemget().  The usage of this program is demonstrated below.       We first create two files that will be used to generate keys usingftok(3), create two semaphore sets using those files, and then       list the sets usingipcs(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 whensemctl(2) is given the samekey (as       generated by the same arguments toftok(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 whenftok(3) is given differentpathname 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 = 9Program 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 ALSO        top

semctl(2),semop(2),ftok(3),capabilities(7),sem_overview(7),sysvipc(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17semget(2)

Pages that refer to this page:ipcrm(1)ipcs(1)lsipc(1)pcp-ipcs(1)ipc(2)semctl(2)semop(2)syscalls(2)umask(2)ftok(3)sem_overview(7)sysvipc(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp