semget(2)

โœจ semget โ€” bestie we're getting semaphore set IDs โœจ

okay so this is the semget(2) page โ€” it's a system call, it lives in the System Calls Manual, and it's about grabbing yourself a System V semaphore set identifier. sit down, hydrate, let's go ๐Ÿ’–

๐Ÿ“š LIBRARY

Standard C library (libc, -lc) โ€” yeah the classic, nothing exotic here ๐ŸŽ€

๐Ÿฆ‹ SYNOPSIS

#include <sys/sem.h>

int semget(key_t key, int nsems, int semflg);

three args, one int back, that's the whole vibe.

๐Ÿ’… DESCRIPTION

so semget() returns the System V semaphore set identifier that's associated with key. two modes, pick your fighter:

  • you're fetching the ID of a semaphore set somebody already made โ€” that's when semflg is zero and key is not IPC_PRIVATE
  • or you're creating a brand new set, main character energy

A new set of nsems semaphores gets created when either key has the value IPC_PRIVATE, or there's no existing semaphore set tied to key and you passed IPC_CREAT in semflg.

Now here's the spicy bit ๐Ÿ”ฅ if semflg has both IPC_CREAT and IPC_EXCL and a semaphore set already exists for key, then semget() fails and errno is set to EEXIST. (it's giving exactly the same energy as O_CREAT | O_EXCL does for open(2).)

At creation time, the least significant 9 bits of semflg define the permissions โ€” owner, group, and others โ€” for the semaphore set. Those bits have the same format and same meaning as the mode argument of open(2). two little catches tho: execute permissions are not meaningful for semaphores, and write permissions mean permission to alter semaphore values.

When a new semaphore set is being created, semget() initializes the set's associated data structure, semid_ds (peep semctl(2)), like this:

  • sem_perm.cuid and sem_perm.uid get set to the effective user ID of the calling process.
  • sem_perm.cgid and sem_perm.gid get set to the effective group ID of the calling process.
  • the least significant 9 bits of sem_perm.mode get set to the least significant 9 bits of semflg.
  • sem_nsems gets set to the value of nsems.
  • sem_otime gets set to 0.
  • sem_ctime gets set to the current time. โฐ

nsems can be 0 (a don't care) when you're not creating a semaphore set. Otherwise nsems has to 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. periodt.

๐Ÿซถ RETURN VALUE

slay on success: semget() returns the semaphore set identifier โ€” a nonnegative integer.

on failure: -1 comes back and errno is set to tell you what went wrong. ๐Ÿ˜ญ

๐Ÿ’€ ERRORS

  • EACCES โ€” a semaphore set exists for key, but your calling process doesn't have permission to access the set, and doesn't hold the CAP_IPC_OWNER capability in the user namespace that governs its IPC namespace. rude but fair.
  • EEXIST โ€” IPC_CREAT and IPC_EXCL were both 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. (yes, EINVAL twice, that's just how it is bestie)
  • 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 doesn't 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.

๐ŸŒˆ STANDARDS

POSIX.1-2024. certified โœจ

๐Ÿ‘‘

HISTORY

SVr4, POSIX.1-2001. an elder, respectfully.

๐ŸŒธ NOTES

IPC_PRIVATE is not a flag field โ€” it's a key_t type. if you use this special value for key, the system call ignores all but the least significant 9 bits of semflg and creates a new semaphore set (on success). iykyk.

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.)

and yes, even though 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. no cap, don't be lazy about it ๐Ÿ™ˆ

you can do that initialization using the semctl(2) SETVAL or SETALL operation. and if you've got multiple peers and none of them know who's gonna 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. team work, but make it threadsafe ๐Ÿ’œ

Semaphore limits

these 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.

๐Ÿ› BUGS

the name choice IPC_PRIVATE was perhaps unfortunate โ€” IPC_NEW would more clearly show its function. ghosting the naming committee fr fr ๐Ÿ’€

๐ŸŽ€ EXAMPLES

the program 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. ๐Ÿ‘‡

first we create two files that'll 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

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 ALSO

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


and that's the whole page bestie, you ate it ๐Ÿ’… this was served fresh from Linux man-pages 6.20, dated 2026-02-08 โ€” go forth and semaphore responsibly ๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms