Semaphores made simple

Here are some key concepts which will help in understanding semaphores for a begineer as well as advanced users.

Semaphores are simple counters that indicate the status of a shared resource. This counter is a variable protected by kernel thus cannot be accessed by the user directly. If counter is greater that 0, then the resource is available, and if the counter is 0 or less than 0, then that resource is busy or being used.This simple mechanism helps in synchronizing multithreaded and multiprocess based applications. Semaphores were invented and proposed by Edsger Dijkstra.

Semaphores can be operated on individual unit or on elements in a set.System V IPC semaphores takes a large array to accomodate themselves, also called set of counting semaphores, they are extremely heavy weight. Lighter weight semaphores are called POSIX semaphores. Threads library semaphores must be used with mapped memory . A semaphore set consists of a control structure and an array of individual semaphores.

The System V semaphores have some unnecessary complications. For example, the semaphore created ,is not just a single counter,rather a set of semaphore counters . This introduces the concept that the semaphore object created consists of 0 to n semaphores in a set with an identical semaphore ID.

There are two types of semaphores :-

1.Binary semaphore.

2.Counting semaphore.


1.System V implementation of semaphore is set of counting semaphores.

2.If there is only one resource is shared among processes we need only one semaphore.In that case, the semaphore is referred as a binary semaphore.

3.If there are multiple no of resources needs to shared among multiple processes we need more than one semaphores also called counting semaphores.

4.System V semaphores are heavy weight semaphores.

5.Maximum no of preocesses which can use a semaphore is 256.

6.Maximum limit of a counting semaphore is 32767 for Posix Semaphores.

7.No of semaphores in a set can be maximum 25 in System V.

The semaphore set must be initialized using semget(),its ownership or permissions can be changed using semctl(),and semaphore operations are performed via the semop() function.

Semaphores important functions are explained below:-

int semget(key_t key, int nsems, int semflg);


Used for identifying the semaphore.


Number of semaphores needed in the set.


Indicates the how semaphore needs to be created.

It could be one of the following types:

IPC_CREAT: Creates a new semaphore if the key does not already exist.

IPC_EXCL: If the key exists, it will cause the function to fail.

Example:-int semid=semget(IPC_PRIVATE,1,IPC_CREAT);

System V semaphores provide a different key, which is identified by IPC_PRIVATE. When this key is used, every time a semget()call is made, it creates a new set of semaphores identified by the semaphore ID.


Once a semaphore is created, System V semaphores require that those semaphores that were created in a set must be initialized. Creation and Initialization of semaphore is not an atomic operation and initialization

needs to be done separately by the user. The function that does  is

int semctl(int  semid, int semnum, int cmd,union semun


The semaphore identifier.


The n’th semaphore in the set identified by semid.


This defines the type of operation performed on the semaphore. For initialization purposes, the flag used is SETVAL .

Depending on what the chosen cmd requires, a fourth argument could be also passed–type union semun–but this is an optional argument. To use this structure, the user must explicitly define it as follows:

union semun {
int  val;
struct  semid_ds *buf;
ushort  *array;
} arg;

The following code snippet sets the first semaphore of the set identified by the semaphore semid to value .

semun init;

Example:-init.val = 1;
int i = semctl(semid,1,SETVAL,init);
Once a semaphore is created and initialized, the user
is ready to perform operations on this set of semaphores.
int semop(int  semid, struct sembuf *sops, size_t nsops);


The semaphore identifier.


It is a pointer to a user-defined array of semaphore structure. However, the documentation suggests that this structure shouldn’t be extended. The structure is:

struct  sembuf {
ushort sem_num; /* identifies which semaphore
in the set */
short sem_op;
/* could be positive,negative or zero*/
short sem_flg; /*coud be IPC_NOWAIT ,SEM_UNDO*/


Determines how many sembuf you are passing. The nsops argument is provided in case the operation needs to be performed on bunch of semaphores at one time.

Semaphores can be cleaned up by following code:-

int ret =  semctl(semid,0,IPC_RMID);

Leave a Reply