Njoftohuni me semaforët në Linux

Përkthimi i artikullit është përgatitur para fillimit të kursit «Administrator Linux.Basic».

Njoftohuni me semaforët në Linux

Semaphore është një mekanizëm që lejon procese dhe rrjedha konkuruese të punojnë me burime të përbashkëta dhe ndihmon në zgjidhjen e problemeve të ndryshme të sinkronizimit si gara, bllokimet e ndërsjella dhe sjellja e gabuar e rrjedhave.

Për të zgjidhur këto probleme, në bërthamë ekzistojnë mjete si mutexat, semaphore, sinjalet dhe barrierat.

Ekzistojnë tre lloje semaforësh:

  1. Semaforë binarë (binary semaphore)
  2. Semaforë numërues (counting semaphore)
  3. Grupet e semaforëve (semaphore set)

Shikimi i gjendjes IPC

Me ndihmën e komandave të mëposhtme, mund të merrni informacion rreth gjendjes aktuale të mjeteve të ndërlidhjes midis proceseve (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 messages

Grupet aktive të semaforëve

Shfaqja e informacionit për grupet aktive të semaforëve.

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

Segmente të memorjes së ndarë

Shikimi i informacionit për segmente aktive të memorjes së ndarë.

# 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

Kufijtë

Ekipa ipcs -l tregon kufijtë e memorjes së ndarë, semaforëve dhe mesazheve.

# 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 e ndarë

Komanda më poshtë tregon memorjen e ndarë.

# 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

Krijuesit e burimeve

Komanda tregon përdoruesin dhe grupin e pronarit dhe krijuesit të burimit.

# 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

Përdorimi i mjeteve IPC

Në shembullin e dhënë më poshtë, parametri -u tregon një përmbledhje të përdorimit të të gjitha mjeteve 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

Kur ndaloni shërbimet, semaforët dhe segmentet e memorjes së ndarë duhet gjithashtu të fshihen. Nëse ato nuk fshihen, mund të bëni këtë me komandën ipcrm, duke kaluar identifikuesin e objektit IPC.

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

Mund të ndryshoni gjithashtu kufijtë e semaforëve duke përdorur sysctl.

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

Njoftohuni me semaforët në Linux

Burimi: habr.com

Blini hosting tĂ« besueshĂ«m pĂ«r faqe interneti me mbrojtje nga DDoS, serverĂ« VPS VDS đŸ”„ Blini hosting tĂ« besueshĂ«m pĂ«r faqe interneti me mbrojtje nga DDoS, serverĂ« VPS VDS | ProHoster