30 April 2011
2 mins read

Understanding Semaphores in Linux

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 the race condition in a multiprocessing system. A 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

Show XSI inter-process communication

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

# 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

Display Active semaphore sets

Print information about active semaphore sets.

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

Shared memory segments

Print information about active shared memory segments.

# 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

Shows limits

The ipcs -l shows limits of shared memory, semaphores, and messages.

# 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

Lists the shared memories

The below command lists the shared memories.

# 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

Lists creator’s ID

Command lists creator’s user ID and group ID as well as owner’s user ID and group ID.

# 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

Usage of IPC facilities

In the below option ,’u’ displays current usage for all the IPC facilities.

# 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

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.

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

You can increase the semaphore values in sysctl file.

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

Bobbin Zachariah

Bobbin Zachariah is the editor-in-chief of Linoxide and has an experienced team of Linux enthusiastic authors who makes this blog awesome. Linoxide is one of the top 20 Linux Blog by whizlabs.

Leave a Reply

Your email address will not be published.

Previous Story

How to Check NFS Client Connection with Rpcinfo Command

Next Story

How to Give Root Privileges to a User in Linux

Latest from Blog

Top 8 Reasons to Use Garuda Linux

Have you been going back and forth between multiple Linux flavors in search of an exciting experience? Or perhaps you are coming from a Windows or MAC environment and want to try

How to Rename Multiple Files in Linux

In a Linux system, you can easily rename a file using mv command. But, if you have multiple files which you want to rename, in this situation you need some extra tools

How to Install TensorFlow on Ubuntu 20.04

Tensorflow is an open-source platform for machine learning and artificial intelligence. It is developed by the Google Brain team. It contains tools, libraries, and community resources for developers to build ML powered
Go toTop