About Semaphores In Linux ? Use ipcs Command With Examples

April 30, 2011 | By
| Reply More

 

A semaphore is a mechanism that allows contending process or thread to alter, monitor queries, and control shared system resources. A semaphore is a solution to race condition in multiprocessing system. Race condition occurs when multiple processes try to access shared resources. If the multi-threaded feature is needed by the application, then it comes with a set of issues such as race condition, deadlocks, and incorrect behavior of threads. To solve race condition, deadlocks, incorrect behavior of threads, the Kernel provides a set of tools like mutex, semaphores, singals and barriers that are handy in solving multithreaded multiprocessor issues.

 

There are three types of Semaphores.
1) Binary Semaphores
2) Counting Semaphores
3) Semaphore Sets

 

Example: 1

[root@localhost bin]# 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

 

The above commands show XSI inter-process communication facilities status.

 

Example: 2

[root@localhost ~]# ipcs -s
------ Semaphore Arrays --------
key semid owner perms nsems
Print information about active semaphore sets.

 

Example: 3

[root@localhost ~]# 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

Print information about active shared memory segments.

 

Example: 4

[root@localhost ~]# 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

The IPCS –l shows limits of shared memory, semaphores, and messages.

 

Example: 5

[root@localhost bin]# 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

Above command lists the shared memories.

 

Example: 6

[root@localhost bin]# 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

Above command lists creator's user ID and group ID as well as owner's user ID and group ID.

 

Example : 7

[root@localhost bin]# 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

In the above option ,'u' displays current usage for all the IPC facilities.

 

When you stop all the services, semaphores and shared memory segments have to be removed. If not, you will be required to use the ‘ipcs’ command.  The segment ID can be removed using ‘ipcrm’ command.

 

[root@localhost bin]#ipcs -a

[root@localhost bin]#ipcrm -s < sem id>

You can increase the semaphore values in sysctl file.

[root@localhost bin]# /sbin/sysctl -w kernel.sem=250

Filed Under : LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.