Q1. What are Semaphores?

Ans. It is an integer variable to count the number of wakeups saved up for future use.

A semaphore could have the value 0, indicating the no wakeups were saved, or some positive value if one or more wakeups were pending.


There are two operations performed on a semaphore, they are, down and up.

The down operation on a semaphore checks to see if the value is greater than 0. If so, it decrements the value (that is, uses up one stored wakeup) and just continues. If the value is 0, the process is put to sleep without completing the down for the moment. Checking the value, changing it, and possibly going to sleep, are all done as a single, indivisible atomic action. It is guaranteed that once a semaphore operation has started, no other process can access the semaphore until the operation has completed or block. This atomicity is absolutely essential to solving synchronization problems and avoiding race conditions.

The up operation increments the value of semaphore addressed. If one or more processes were sleeping on that semaphore, unable to complete an earlier down operation, one of them is chosen by the system and is allowed to complete its down. Thus, after an up on a semaphore with processes sleeping on it, the semaphore will still be 0, but there will be one fewer process sleeping on it. The operation of incrementing the semaphore and working up one process also indivisible.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: