セマフォについて学ぶ Linux

記事の翻訳はコース開始前夜に準備されました 管理者 Linux.基本».

セマフォについて学ぶ Linux

セマフォは、競合するプロセスやスレッドがリソースを共有できるようにし、競合状態、デッドロック、スレッドの誤動作などのさまざまな同期の問題を解決するのに役立つメカニズムです。

これらの問題を解決するために、カーネルはミューテックス、セマフォ、シグナル、バリアなどの機能を提供します。

セマフォには次の 3 つの種類があります。

  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

サービスが停止されると、セマフォと共有メモリ セグメントも削除する必要があります。削除されていない場合は、IPC オブジェクト識別子を渡して ipcrm コマンドを使用して削除できます。

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

セマフォ制限を変更するには、 sysctl.

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

セマフォについて学ぶ Linux

出所: habr.com

DDoS 保護機能を備えた信頼性の高いサイト用ホスティング、VPS VDS サーバーを購入する 🔥 DDoS攻撃対策付きの信頼性の高いウェブサイトホスティング、VPS/VDSサーバーを購入しましょう | ProHoster