Chapter Five Semaphore: Intruduction
Chapter Five Semaphore: Intruduction
Chapter Five Semaphore: Intruduction
Chapter Five
Semaphore
Intruduction
To get rid of the problem of wasting the wake-up signals, Dijkstra proposed an approach which involves
storing all the wake-up calls. Dijkstra states that, instead of giving the wake-up calls directly to the
consumer, producer can store the wake-up call in a variable. Any of the consumers can read it whenever
it needs to do so.
A semaphore is a simple integer variable used to provide synchronization among multiple processes
running concurrently. The variables stores the entire wake up calls that are being transferred from
producer to consumer. It is a variable on which read, modify and update happens automatically in kernel
mode.
Semaphore cannot be implemented in the user mode because race condition may always arise when
two or more processes try to access the variable simultaneously. It always needs support from the
operating system to be implemented.
According to the demand of the situation, Semaphore can be divided into two categories.
A. Counting Semaphore
There are the scenarios in which more than one processes need to execute in critical section
simultaneously. However, counting semaphore can be used when we need to have more than one
process in the critical section at the same time.
The programming code of semaphore implementation is shown below which includes the structure of
semaphore and the logic using which the entry and the exit can be performed in the critical section.
struct semaphore
{
int value;
Queue type L;
}
Wait (semaphore s)
{
s.value = s.value - 1;
if (s.value < 0)
{
put process (PCB) in L;
sleep();
}
else
return;
}
Signal (semaphore s)
{
s.value = s.value + 1;
if (s.value <=0 ) {
select a process (PCB) from L;
wake up();
}
}
Explanation:- The above implementation of counting semaphore has been explained in the following
points-
Point-01:
o An integer value
o An associated waiting list (usually a queue)
Point-02:
o Positive value indicates the number of processes that can be present in the critical section at the
same time.
o Negative value indicates the number of processes that are blocked in the waiting list.
Point-03:
o The waiting list of counting semaphore contains the processes that got blocked when trying to
enter the critical section.
o In waiting list, the blocked processes are put to sleep.
o The waiting list is usually implemented using a queue data structure.
o Using a queue as waiting list ensures bounded waiting.
o This is because the process which arrives first in the waiting queue gets the chance to enter the
critical section first.
Point-04:
o The wait operation is executed when a process tries to enter the critical section.
o Wait operation decrements the value of counting semaphore by 1.
o If the resulting value of counting semaphore is greater than or equal to 0, process is allowed to
enter the critical section.
Case-02: Counting Semaphore Value < 0
o If the resulting value of counting semaphore is less than 0, process is not allowed to enter the
critical section.
Point-05:
o The signal operation is executed when a process takes exit from the critical section.
o Signal operation increments the value of counting semaphore by 1.
o If the resulting value of counting semaphore is less than or equal to 0, a process is chosen from
the waiting list and wake up to execute.
Point-06:
o By adjusting the value of counting semaphore, the number of processes that can enter the
critical section can be adjusted.
o If the value of counting semaphore is initialized with N, then maximum N processes can be
present in the critical section at any given time.
Point-07:
Point-08:
In a system,
o Consider n units of a particular non-shareable resource are available.
o Then, n processes can use these n units at the same time.
o So, the access to these units is kept in the critical section.
o The value of counting semaphore is initialized with ‘n’.
o When a process enters the critical section, the value of counting semaphore decrements by 1.
o When a process exits the critical section, the value of counting semaphore increments by 1.
Note: In such scenarios, value of counting semaphore is initialized with value greater than 1.
Point-09:
o Other names by which wait operation may be referred : Down operation, P operation.
o Other names by which signal operation may be referred : Up operation, V operation, Release
operation.
Summary:-
In this mechanism, the entry and exit in the critical section are performed on the basis of the value of
counting semaphore. The value of counting semaphore at any point of time indicates the maximum
number of processes that can enter in the critical section at the same time.
A process which wants to enter in the critical section first decrease the semaphore value by 1 and then
check whether it gets negative or not. If it gets negative then the process is pushed in the list of blocked
processes (i.e. q) otherwise it gets enter in the critical section.
When a process exits from the critical section, it increases the counting semaphore by 1 and then checks
whether it is negative or zero. If it is negative then that means that at least one process is waiting in the
blocked state hence, to ensure bounded waiting, the first process among the list of blocked processes
will wake up and gets enter in the critical section.
The processes in the blocked list will get waked in the order in which they slept. If the value of counting
semaphore is negative then it states the number of processes in the blocked state while if it is positive
then it states the number of slots available in the critical section.
Signal → Inc → Up → V
Q1. A Counting Semaphore was initialized to 12. then 10P (wait) and 4V (Signal) operations were
computed on this semaphore. What is the result?
Solution
S = 12 (initial)
10 p (wait) :
CS= S -10 = 12 - 10 = 2
then 4 V :
CS = S + 4 =2 + 4 = 6
Q2. A counting semaphore S is initialized to 10. Then, 6 P operations and 4 V operations are performed
on S. What is the final value of S?
Solution-
S = 10 (initial)
6 p (wait) :
CS= S - 6 = 10 - 6 = 4
then 4 V :
CS = S + 4 = 4 + 4 = 8
Solution-
S = 7 (initial)
20 p (wait) :
CS= S - 20 = 7 - 20 = -13
then 15 V :
CS = S + 4 = (-13) + 15 = 2
However, Binary Semaphore strictly provides mutual exclusion. Here, instead of having more than 1
slots available in the critical section, we can only have at most 1 process in the critical section. The
semaphore can have only two values, 0 or 1.
Explanation- The above implementation of binary semaphore has been explained in the following
points-
Point-01:
Point-02:
o The waiting list of binary semaphore contains the processes that got blocked when trying to
enter the critical section.
o In waiting list, the blocked processes are put to sleep.
o The waiting list is usually implemented using a queue data structure.
o Using a queue as waiting list ensures bounded waiting.
o This is because the process which arrives first in the waiting queue gets the chance to enter the
critical section first.
Point-03:
o The wait operation is executed when a process tries to enter the critical section.
o Then, there are two cases possible-
o The process is blocked and not allowed to enter the critical section.
o The process is put to sleep in the waiting list.
Point-04:
o The signal operation is executed when a process takes exit from the critical section.
o Then, there are two cases possible-
o If the waiting list is not empty, a process is chosen from the waiting list and wake up to execute.
Point-05:
Example-
Signal (S2)
In this example-
var
a,b,c,d,e,f,g,h,i,j,k : semaphore;
begin
cobegin
coend
end;
Solution-
Precedence graph will be as shown below-