مقدمة إلى Semaphores في Linux

تم إعداد ترجمة المقال عشية بدء الدورة "مسؤول Linux.Basic".

مقدمة إلى Semaphores في Linux

السيمافور هي آلية تسمح للعمليات المتنافسة والخيوط بمشاركة الموارد وتساعد في حل مشكلات المزامنة المختلفة مثل الأجناس والأقفال (الأقفال المتبادلة) وسوء التصرف في الخيوط.

لحل هذه المشكلات ، توفر النواة أدوات مثل كائنات المزامنة ، والإشارات ، والإشارات ، والحواجز.

هناك ثلاثة أنواع من الإشارات:

  1. إشارات ثنائية
  2. عدادات الإشارات (عد الإشارة)
  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

المصدر: www.habr.com

إضافة تعليق