Introduzione à i Semafori in Linux

A traduzzione di l'articulu hè stata preparata à a vigilia di l'iniziu di u corsu "Amministratore Linux.Basic".

Introduzione à i Semafori in Linux

Un semaforu hè un mecanismu chì permette à i prucessi è i fili cuncurrenti di sparte risorse è aiuta cù diversi prublemi di sincronizazione, cum'è e razze, i blocchi (locks mutuali) è i fili misbehaving.

Per risolve questi prublemi, u kernel furnisce strumenti cum'è mutex, semafori, signali è barrieri.

Ci sò trè tippi di semafori:

  1. Semafori binari
  2. Semafori-contatori (conta di semafori)
  3. Arrays di semafori (set di semafori)

Vede u Statu IPC

I seguenti cumandamenti furniscenu infurmazioni nantu à u statu attuale di a cumunicazione inter-processu (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 messages

Array attivi di semafori

Mostra l'infurmazioni nantu à l'arrays di semafori attivi.

# ipcs -s
------ Semaphore Arrays --------
key semid owner perms nsems

Segmenti di memoria spartuti

Vede l'infurmazioni nantu à i segmenti di memoria spartuti attivi.

# 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

Limiti

squadra ipcs -l mostra i limiti di memoria, semaforu è missaghju spartuti.

# 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) = 65536

Memoria spartuta

U cumandamentu sottu mostra a memoria spartuta.

# 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 dest

Creatori di risorse

U cumandimu mostra l'utilizatore è u gruppu di u pruprietariu è u creatore di a risorsa.

# 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 root

Utilizà i strumenti IPC

In l'esempiu sottu, u paràmetru -u mostra un riassuntu di l'usu di tutti i strumenti 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 bytes

Quandu i servizii sò fermati, i semafori è i segmenti di memoria spartuti anu ancu esse eliminati. Se ùn sò micca eliminati, questu pò esse fattu cù u cumandimu ipcrm, passendu l'identificatore di l'ughjettu IPC.

# ipcs -a
# ipcrm -s < sem id>

Pudete ancu cambià i limiti di u semaforu usendu sysctl.

# /sbin/sysctl -w kernel.sem=250

Introduzione à i Semafori in Linux

Source: www.habr.com

Add a comment