Przekład artykułu przygotowany przed rozpoczęciem kursu .

Semafor to mechanizm, który pozwala konkurującym procesom i wątkom na pracę z wspólnymi zasobami i pomaga w rozwiązywaniu różnych problemów synchronizacji, takich jak wyścigi, blokady wzajemne (deadlock) i niewłaściwe zachowanie wątków.
Aby rozwiązać te problemy, w jądrze dostępne są takie narzędzia jak mutexy, semafory, sygnały i bariery.
Istnieją trzy rodzaje semaforów:
- Semafory binarne (binary semaphore)
- Semafory zliczające (counting semaphore)
- Zbiory semaforów (semaphore set)
Wyświetlanie stanu IPC
Za pomocą poniższych poleceń można uzyskać informacje o bieżącym stanie środków interprocesowej komunikacji (inter-process communication, IPC).
# ipcs
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 65536 root 600 393216 2 dest
0x00000000 98305 root 600 393216 2 dest
0x00000000 131074 root 600 393216 2 dest
0x00000000 163843 root 600 393216 2 dest
0x00000000 196612 root 600 393216 2 dest
0x00000000 229381 root 600 393216 2 dest
0x00000000 262150 root 600 393216 2 dest
0x00000000 294919 root 600 393216 2 dest
0x00000000 327688 root 600 393216 2 dest
------ Semaphore Arrays --------
key semid owner perms nsems
------ Message Queues --------
key msqid owner perms used-bytes messagesAktywne zbiory semaforów
Wyświetlanie informacji o aktywnych zbiorach semaforów.
# ipcs -s
------ Semaphore Arrays --------
key semid owner perms nsemsSegmenty pamięci dzielonej
Wyświetlanie informacji o aktywnych segmentach pamięci dzielonej.
# ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 65536 root 600 393216 2 dest
0x00000000 98305 root 600 393216 2 destLimity
Zespół ipcs -l wyświetla limity pamięci dzielonej, semaforów i komunikatów.
# ipcs -l
------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 4194303
max total shared memory (kbytes) = 1073741824
min seg size (bytes) = 1
------ Semaphore Limits --------
max number of arrays = 128
max semaphores per array = 250
max semaphores system wide = 32000
max ops per semop call = 32
semaphore max value = 32767
------ Messages: Limits --------
max queues system wide = 16
max size of message (bytes) = 65536
default max size of queue (bytes) = 65536Pamięć dzielona
Poniższe polecenie wyświetla pamięć dzieloną.
# ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 65536 root 600 393216 2 dest
0x00000000 98305 root 600 393216 2 dest
0x00000000 131074 root 600 393216 2 dest
0x00000000 163843 root 600 393216 2 dest
0x00000000 196612 root 600 393216 2 dest
0x00000000 229381 root 600 393216 2 dest
0x00000000 262150 root 600 393216 2 dest
0x00000000 294919 root 600 393216 2 dest
0x00000000 327688 root 600 393216 2 destTwórcy zasobów
Polecenie wyświetla użytkownika i grupę właściciela i twórcy zasobu.
# ipcs -m -c
------ Shared Memory Segment Creators/Owners --------
shmid perms cuid cgid uid gid
65536 600 root root root root
98305 600 root root root root
131074 600 root root root root
163843 600 root root root root
196612 600 root root root root
229381 600 root root root root
262150 600 root root root root
294919 600 root root root root
327688 600 root root root rootUżycie środków IPC
W przykładzie poniżej parametr -u wyświetla podsumowanie wykorzystania wszystkich środków IPC.
# ipcs -u
------ Shared Memory Status --------
segments allocated 9
pages allocated 864
pages resident 477
pages swapped 0
Swap performance: 0 attempts 0 successes
------ Semaphore Status --------
used arrays = 0
allocated semaphores = 0
------ Messages: Status --------
allocated queues = 0
used headers = 0
used space = 0 bytesPodczas zatrzymywania usług semafory i segmenty pamięci dzielonej powinny również być usuwane. Jeśli nie są usuwane, można to zrobić za pomocą polecenia ipcrm, przekazując identyfikator obiektu IPC.
# ipcs -a
# ipcrm -s < sem id> Można również zmienić limity semaforów, używając sysctl.
# /sbin/sysctl -w kernel.sem=250
Źródło: habr.com
