You are on page 1of 12

Process Synchronisation is one of the major areas or job of the opperating system.

So, for further


knowledge on synchronisation every student is to study the following:

- Peterson Solution Mechanism

- Synchronization Mechanism without busy waiting

- Sleep and Wake (Producer Consumer problem)

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:

A counting semaphore has two components-

o An integer value
o An associated waiting list (usually a queue)

Point-02:

The value of counting semaphore may be positive or negative.

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.

Then, following two cases are possible-

Case-01: Counting Semaphore Value >= 0

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.

In this case, process is put to sleep in the waiting list.

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.

Then, following two cases are possible-

Case-01: Counting Semaphore <= 0

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.

Case-02: Counting Semaphore > 0

o If the resulting value of counting semaphore is greater than 0, no action is taken.

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:

o To implement mutual exclusion, the value of counting semaphore is initialized with 1.


o It ensures that only one process can be present in the critical section at any given time.

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.

Problem on Counting Semaphore


Generally the questions are very simple that contains only subtraction and addition.

Wait → Decre → Down → P

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

Hence, the final value of counting semaphore is 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

Hence, the final value of counting semaphore is 8.


Q3. A counting semaphore S is initialized to 7. Then, 20 P operations and 15 V operations are performed
on S. What is the final value of S?

Solution-

S = 7 (initial)

20 p (wait) :

CS= S - 20 = 7 - 20 = -13

then 15 V :

CS = S + 4 = (-13) + 15 = 2

Hence, the final value of counting semaphore is 2.

B. Binary Semaphore or Mutex


In counting semaphore, Mutual exclusion was not provided because we has the set of processes which
required to execute in the critical section simultaneously.

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.

Let's see the programming implementation of Binary Semaphore.


struct semaphore
{
enum value (0,1);
Queue type L;
}
Wait (semaphore s)
{
if (s.value == 1)
{
s.value=0;
}
else
{
put process (PCB) in s.L;
sleep();
}
}
Signal (semaphore s)
{
if (s.L is empty)
{
s.value=1;
}
else
{
select a process (PCB) from s.L;
wake up();
}
}

Explanation- The above implementation of binary semaphore has been explained in the following
points-

Point-01:

A binary semaphore has two components-

o An integer value which can be either 0 or 1


o An associated waiting list (usually a queue)

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-

Case-01: Binary Semaphore Value = 1

If the value of binary semaphore is 1,

o The value of binary semaphore is set to 0.


o The process is allowed to enter the critical section.

Case-02: Binary Semaphore Value = 0

If the value of binary semaphore is 0,

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-

Case-01: Waiting List is Empty-

o If the waiting list is empty, the value of binary semaphore is set to 1.


Case-02: Waiting List is Not Empty-

o If the waiting list is not empty, a process is chosen from the waiting list and wake up to execute.

Point-05:

Binary semaphores are mainly used for two purposes-

o To ensure mutual exclusion.


o To implement the order in which the processes must execute.

Example-

Wait (S1) Process P2 Wait (S2)

Process P1 Signal (S1) Process P3

Signal (S2)

In this example-

o Two binary semaphores S1 and S2 both initialized with 0 are used.


o The execution order of the processes is P2 → P1 → P3.

Problem on Binary Semaphore


Consider the following program segment for concurrent processing using semaphore operators P and V
for synchronization. Draw the precedence graph for the statements S1 to S9.

var
a,b,c,d,e,f,g,h,i,j,k : semaphore;

begin

cobegin

begin S1; V(a); V(b) end;

begin P(a); S2; V(c); V(d) end;

begin P(c); S4; V(e) end;

begin P(d); S5; V(f) end;

begin P(e); P(f); S7; V(k) end

begin P(b); S3; V(g); V(h) end;

begin P(g); S6; V(i) end;

begin P(h); P(i); S8; V(j) end;

begin P(j); P(k); S9 end;

coend

end;

Solution-
Precedence graph will be as shown below-

You might also like