Giới thiệu về Semaphores trong Linux

Bản dịch của bài báo đã được chuẩn bị vào đêm trước khi bắt đầu khóa học "Quản trị viên Linux.Basic".

Giới thiệu về Semaphores trong Linux

Semaphore là một cơ chế cho phép các quy trình và luồng cạnh tranh chia sẻ tài nguyên và giúp giải quyết các sự cố đồng bộ hóa khác nhau như chủng tộc, bế tắc (khóa lẫn nhau) và các luồng hoạt động sai.

Để giải quyết những vấn đề này, kernel cung cấp các công cụ như mutexes, semaphores, signal và barrier.

Có ba loại semaphore:

  1. Semaphores nhị phân
  2. Bộ đếm semaphores (đếm semaphore)
  3. Mảng semaphores (bộ semaphore)

Xem trạng thái IPC

Các lệnh sau đây cung cấp thông tin về trạng thái hiện tại của giao tiếp giữa các quá trình (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

Mảng hoạt động của semaphores

Hiển thị thông tin về mảng semaphore đang hoạt động.

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

Phân đoạn bộ nhớ dùng chung

Xem thông tin về các phân đoạn bộ nhớ dùng chung đang hoạt động.

# 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

Giới hạn

Đội ipcs -l hiển thị bộ nhớ dùng chung, semaphore và giới hạn tin nhắn.

# 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

Bộ nhớ dùng chung

Lệnh bên dưới hiển thị bộ nhớ dùng chung.

# 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

Người tạo tài nguyên

Lệnh hiển thị người dùng và nhóm của chủ sở hữu và người tạo tài nguyên.

# 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

Sử dụng công cụ IPC

Trong ví dụ dưới đây, tham số -u hiển thị một bản tóm tắt về việc sử dụng tất cả các công cụ 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

Khi các dịch vụ bị dừng, các semaphore và phân đoạn bộ nhớ dùng chung cũng phải bị xóa. Nếu chúng không bị xóa, thì điều này có thể được thực hiện bằng cách sử dụng lệnh ipcrm, chuyển mã định danh của đối tượng IPC.

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

Bạn cũng có thể thay đổi giới hạn semaphore bằng cách sử dụng sysctl.

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

Giới thiệu về Semaphores trong Linux

Nguồn: www.habr.com

Thêm một lời nhận xét