รู้เบื้องต้นเกี่ยวกับ Semaphores ใน Linux

การแปลบทความจัดทำขึ้นในวันเริ่มต้นหลักสูตร "ผู้ดูแลระบบ Linux.Basic".

รู้เบื้องต้นเกี่ยวกับ Semaphores ใน Linux

เซมาฟอร์เป็นกลไกที่ช่วยให้กระบวนการและเธรดที่แข่งขันกันแบ่งปันทรัพยากรและช่วยแก้ปัญหาการซิงโครไนซ์ต่างๆ เช่น การแข่งขัน การหยุดชะงัก (การล็อกระหว่างกัน) และเธรดที่ทำงานผิดปกติ

เพื่อแก้ปัญหาเหล่านี้ เคอร์เนลจัดเตรียมเครื่องมือต่างๆ เช่น mutexes, semaphores, Signals และ Barriers

เซมาฟอร์มีสามประเภท:

  1. สัญญาณไบนารี
  2. Semaphores-counters (การนับสัญญาณ)
  3. อาร์เรย์ของสัญญาณ (ชุดสัญญาณ)

ดูสถานะ IPC

คำสั่งต่อไปนี้ให้ข้อมูลเกี่ยวกับสถานะปัจจุบันของการสื่อสารระหว่างกระบวนการ (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

อาร์เรย์ของสัญญาณที่ใช้งานอยู่

แสดงข้อมูลเกี่ยวกับอาร์เรย์สัญญาณที่ใช้งานอยู่

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

ส่วนหน่วยความจำที่ใช้ร่วมกัน

ดูข้อมูลเกี่ยวกับเซ็กเมนต์หน่วยความจำที่ใช้ร่วมกันที่ใช้งานอยู่

# 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

ขีด จำกัด

ทีม ipcs -l แสดงหน่วยความจำที่ใช้ร่วมกัน เซมาฟอร์ และขีดจำกัดของข้อความ

# 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

หน่วยความจำที่ใช้ร่วมกัน

คำสั่งด้านล่างแสดงหน่วยความจำที่ใช้ร่วมกัน

# 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

ผู้สร้างทรัพยากร

คำสั่งแสดงผู้ใช้และกลุ่มของเจ้าของและผู้สร้างรีซอร์ส

# 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

การใช้เครื่องมือ IPC

ในตัวอย่างด้านล่าง พารามิเตอร์ -u แสดงสรุปการใช้งานเครื่องมือ 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

เมื่อบริการหยุดลง เซมาฟอร์และเซ็กเมนต์หน่วยความจำที่ใช้ร่วมกันจะต้องถูกลบด้วย หากไม่ถูกลบออก ก็สามารถทำได้โดยใช้คำสั่ง ipcrm โดยส่งผ่านตัวระบุของวัตถุ IPC

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

คุณยังสามารถเปลี่ยนขีด จำกัด สัญญาณโดยใช้ sysctl.

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

รู้เบื้องต้นเกี่ยวกับ Semaphores ใน Linux

ที่มา: will.com

เพิ่มความคิดเห็น