semget(2) System Calls Manual semget(2) NAZWA semget - pobranie identyfikatora zestawu semaforow Systemu V BIBLIOTEKA Standardowa biblioteka C (libc, -lc) SKLADNIA #include int semget(key_t key, int nsems, int semflg); OPIS The 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. Nowy zestaw skladajacy sie z nsems semaforow zostanie utworzony, jesli parametr key bedzie miec wartosc IPC_PRIVATE lub gdy zestaw semaforow skojarzony z key nie istnieje, a w parametrze semflg zostanie przekazany znacznik IPC_CREAT. Jesli w parametrze semflg podano zarowno IPC_CREAT, jak i IPC_EXCL oraz juz istnieje zestaw semaforow o kluczu key, to semget() konczy sie bledem, ustawiajac errno na wartosc EEXIST. (Dziala to analogicznie do O_CREAT | O_EXCL w 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). Podczas tworzenia nowego zestawu semaforow semget() inicjuje zwiazana z zestawem semaforow strukture semid_ds (patrz semctl(2)) w nastepujacy sposob: o sem_perm.cuid i sem_perm.uid przyjmuja wartosc efektywnego identyfikatora wlasciciela procesu wywolujacego. o sem_perm.cgid i sem_perm.gid przyjmuja wartosc efektywnego identyfikatora grupy procesu wywolujacego. o 9 najmniej znaczacych bitow pola sem_perm.mode jest kopiowanych z 9 najmniej znaczacych bitow semflg. o sem_nsems jest ustawiane na wartosc nsems. o sem_otime przyjmie wartosc 0. o sem_ctime przypisywany jest biezacy czas. Parametr nsems moze miec wartosc 0 (nie jest brany pod uwage), jesli nie bedzie tworzony zestaw semaforow. W przeciwnym przypadku parametr nsems musi byc wiekszy od 0 i mniejszy lub rowny maksymalnej liczbie semaforow w zestawie (SEMMSL). Jezeli zestaw semaforow juz istnieje, to weryfikowane sa uprawnienia. WARTOSC ZWRACANA On success, semget() returns the semaphore set identifier (a nonnegative integer). On failure, -1 is returned, and errno is set to indicate the error. BLEDY EACCES 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 i IPC_EXCL okreslono w semflg, lecz zestaw semaforow dla key juz istnieje. EINVAL nsems jest mniejsze niz 0 lub wieksze niz ograniczenie liczby semaforow w zestawie (SEMMSL) EINVAL Zestaw semaforow, do ktorego odnosi sie key juz istnieje, lecz nsems jest wieksze niz liczba semaforow w tym zestawie. ENOENT Nie ma zestawu semaforow o identyfikatorze key i znacznik IPC_CREAT nie zostal przekazany w parametrze semflg. ENOMEM Zestaw semaforow powinien zostac utworzony, ale w systemie brak jest pamieci na utworzenie nowej struktury danych. ENOSPC Nastapila proba przekroczenia ograniczenia liczby zestawow (SEMMNI) lub lacznej liczby semaforow w systemie (SEMMNS). STANDARDY POSIX.1-2008. HISTORIA SVr4, POSIX.1-2001. UWAGI IPC_PRIVATE nie jest znacznikiem, ale szczegolna wartoscia typu key_t. Jesli wartosc ta zostanie uzyta jako parametr key, to system uwzgledni jedynie 9 najnizszych bitow parametru msgflg i (w razie powodzenia) utworzy nowy zestaw semaforow. Inicjowanie semaforow Wartosci semaforow w nowo utworzonym zestawie sa nieokreslone (POSIX.1-2001 jasno o tym mowi, choc POSIX.1-2008 okresla, ze przyszla wersja tego standardu moze wymagac implementacji inicjujacej semafory z wartoscia 0). Mimo ze Linux, tak jak i wiele innych implementacji, nadaje im wartosc poczatkowa rowna 0, to przenosne aplikacje nie powinny zalezec od tego zachowania i zamiast tego powinny wyraznie inicjowac semafory zadanymi wartosciami. Aby zainicjowac semafory, nalezy na zestawie semaforow uzyc operacji SETVAL lub SETALL wywolania semctl(2). W sytuacji gdy wiele procesow nie wie, ktory pierwszy zainicjuje zestaw semaforow, to aby uniknac sytuacji wyscigu, mozna sprawdzic, czy pole sem_otime powiazanej struktury danych zwracanej przez operacje IPC_STAT wywolania semctl(2) ma wartosc niezerowa. Limity semaforow Wywolania semget() dotycza nastepujace ograniczenia zasobow zwiazanych z zestawami semaforow: 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 Limit liczby semaforow w systemie: wartosc zalezna od lokalnych ustawien (pod Linuksem to ograniczenie mozna odczytac i zmienic, uzywajac drugiego pola pliku /proc/sys/kernel/sem). Prosze zauwazyc, ze systemowa liczba semaforow jest rowniez ograniczona przez iloczyn SEMMSL i SEMMNI. USTERKI Nazwa IPC_PRIVATE prawdopodobnie nie jest najszczesliwsza. IPC_NEW w sposob bardziej przejrzysty odzwierciedlaloby role tej wartosci. PRZYKLADY 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 Kod zrodlowy programu /* 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); } ZOBACZ TAKZE semctl(2), semop(2), ftok(3), capabilities(7), sem_overview(7), sysvipc(7) TLUMACZENIE Autorami polskiego tlumaczenia niniejszej strony podrecznika sa: Rafal Lewczuk , Andrzej Krzysztofowicz , Robert Luberda i Michal Kulach Niniejsze tlumaczenie jest wolna dokumentacja. Blizsze informacje o warunkach licencji mozna uzyskac zapoznajac sie z GNU General Public License w wersji 3 lub nowszej. Nie przyjmuje sie ZADNEJ ODPOWIEDZIALNOSCI. Bledy w tlumaczeniu strony podrecznika prosimy zglaszac na adres listy dyskusyjnej . Linux man-pages 6.06 31 pazdziernika 2023 r. semget(2)