You are on page 1of 39

Semaphore

Properties

ZAIB UNNISA UNIVERSITY OF GUJRAT


2
Wait() & Signal()
ZAIB UNNISA UNIVERSITY OF GUJRAT
Semaphore

5
ZAIB UNNISA UNIVERSITY OF GUJRAT
Value of Semaphore
ZAIB UNNISA

 Zero

Positive
UNIVERSITY OF GUJRAT

 Negative

6
Another name of Semaphore
ZAIB UNNISA

OS based solution to Critical section Problem.


UNIVERSITY OF GUJRAT

7
Implementation
ZAIB UNNISA
UNIVERSITY OF GUJRAT

 signal ()

s.increment () ; s.signal(); s.V();


s.increment_and_wake_a_waiting_process_if_any();

8
Implementation
ZAIB UNNISA

 Wait ()

s.decrement (); s.wait(); s.P();


UNIVERSITY OF GUJRAT

s.decrement_and_block_if_the_result_is_negative();

9
N Process CSP Solution Using
ZAIB UNNISA

Semaphores
Binary Semaphore
mutex locks mutual
exclusion
UNIVERSITY OF GUJRAT

10
Implementation
ZAIB UNNISA

// entry to CS of a process is controlled semaphore mutex = 1;


by wait operation Process Pi;
do {
wait (mutex);
UNIVERSITY OF GUJRAT

<CS>
// exit from CS is signaled by signal signal ( mutex);
operation <RS>
}while(1);

11
ZAIB UNNISA
UNIVERSITY OF GUJRAT

void signal(semaphore s)
{
void wait(semaphore s)
s.value ++ ;
Type of struct {
if( s.value >=0)
{ s.value - -;
{
int value; if( s.value < 0){
remove process P
struct process * L; add caller process
from list;
} semaphore ; to list L;
wakeup(P) ;
12 }}
block();
}
}
Implementation

13
ZAIB UNNISA UNIVERSITY OF GUJRAT
Uses of Semaphores

14
ZAIB UNNISA UNIVERSITY OF GUJRAT
Semaphores
Counting
ZAIB UNNISA UNIVERSITY OF GUJRAT
Counting Semaphore

16
ZAIB UNNISA UNIVERSITY OF GUJRAT
Semaphore Here is a semaphore initialized to 2 for
ZAIB UNNISA

resource control.

from railway Value can be 2, 1, or 0.

analogy
UNIVERSITY OF GUJRAT
Example
ZAIB UNNISA

• Consider two processes Pi and Pj with statements A and B in them respectively.


• It is required that statement B in Pj should be executed after statement A in Pi.
• Give a Semaphore based solution.

Pj
UNIVERSITY OF GUJRAT

Pi
.
.
.
.
wait();
A
B
signal();
.
.
.
.

18
Classroom Activity
 Consider three processes P1, P2, and P3.
Instruction A in P1 executes after
instruction B in P2 is executed.
 Instruction P2 executes after instruction C
is executed in P3.
 Give a semaphore-based solution (C<B<A)

ZAIB UNNISA UNIVERSITY OF GUJRAT


Solution

P3
P2 P1
.
.
.
Wait(B); Wait(A);
C
. .
Signal(B);
B; .
.
Signal(A); A;
.
.

ZAIB UNNISA UNIVERSITY OF GUJRAT


20
Problems with Semaphores

21
ZAIB UNNISA UNIVERSITY OF GUJRAT
Problems with
ZAIB UNNISA

semaphores
UNIVERSITY OF GUJRAT

 Wrong initialization or placement of


wait and signal may cause the
following problems
 Violation of M.E.
 Deadlock
 Starvation
Violation of Mutual Exclusion
ZAIB UNNISA

P0
 By mistake if the programmer has placed the signal(s);
signal operation before the wait operation in .
P0. .
wait(s);
.
UNIVERSITY OF GUJRAT

P1
wait(s);
.
signal(s);
.

23
Dead Lock
ZAIB UNNISA

P0
wait(s1)
wait(s2)
….
signal(s1)
signal(s2)

UNIVERSITY OF GUJRAT

signal(s1) P1
wait(s2)
wait(s1)
P0 P1 ….
signal(s2)
signal(s1)
signal(s2)

24
Starvation
ZAIB UNNISA

P0 P1
Wait(s); Wait(s);
UNIVERSITY OF GUJRAT

.. ..
.. ..
Nothing/wait(s) signal(s)
.. ..

25
ZAIB UNNISA

Classic Problems of
Synchronization
UNIVERSITY OF GUJRAT
Bounded Buffer Problem
ZAIB UNNISA

• This is a generalization of the producer-consumer problem wherein access is controlled to a shared


group of buffers of a limited size.
• In this solution, the two counting semaphores "full" and "empty" keep track of the current number
of full and empty buffers respectively ( and initialized to 0 and N respectively. )
UNIVERSITY OF GUJRAT

• The binary semaphore mutex controls access to the critical section.


• The producer and consumer processes are nearly identical – One can think of the producer as
producing full buffers, and the consumer as producing empty buffers.

27
28
ZAIB UNNISA UNIVERSITY OF GUJRAT
Bounded Buffer-Producer
ZAIB UNNISA

do
{ •Looking at the code for a
// wait until empty > 0 and then decrement
producer, we can see that a
'empty'
wait(empty); producer first waits until
// acquire lock there is at least one empty
wait(mutex); slot.
UNIVERSITY OF GUJRAT

/* perform the insert operation in a slot */


•Then it decrements
// release lock the empty semaphore
signal(mutex); because, there will now be
// increment 'full' one less empty slot, since the
signal(full); producer is going to insert
}
data in one of those slots.
while(TRUE)
• Then, it acquires a lock on the buffer, so that the consumer cannot
access the buffer until the producer completes its operation.
• After performing the insert operation, the lock is released and the value
of full is incremented because the producer has just filled a slot in the
buffer.

ZAIB UNNISA UNIVERSITY OF GUJRAT


30
Bounded Buffer-Consumer
ZAIB UNNISA

do
{
// wait until full > 0 and then decrement 'full' The consumer waits until there
wait(full); is at least one full slot in the
// acquire the lock buffer.
wait(mutex);

/* perform the remove operation in a slot */ Then it decrements


UNIVERSITY OF GUJRAT

the full semaphore because the


// release the lock
signal(mutex); number of occupied slots will
// increment 'empty' be decreased by one after the
signal(empty); consumer completes its
}
while(TRUE); operation.
Bounded Buffer-Consumer
ZAIB UNNISA

• After that, the consumer acquires a lock on the buffer.


• Following that, the consumer completes the removal operation so
that the data from one of the full slots are removed.
Then, the consumer releases the lock.
UNIVERSITY OF GUJRAT

• Finally, the empty semaphore is incremented by 1, because the


consumer has just removed data from an occupied slot, thus making
it empty.

32
Dining Philosophers Problem

The dining philosophers’ problem is another


classic synchronization problem that is used
to evaluate situations where there is a need of
allocating multiple resources to multiple
processes.

ZAIB UNNISA UNIVERSITY OF GUJRAT


Problem Statement
ZAIB UNNISA

 At any instant, a philosopher is either eating or thinking.


 When a philosopher wants to eat, he uses two chopsticks - one from
their left and one from their right. When a philosopher wants to
UNIVERSITY OF GUJRAT

think, he keeps down both chopsticks in their original place.

34
Solution
ZAIB UNNISA

 From the problem statement, it is clear that a philosopher can think


for an indefinite amount of time.
 But when a philosopher starts eating, he has to stop at some point in
time.
UNIVERSITY OF GUJRAT

 The philosopher is in an endless cycle of thinking and eating.


 An array of five semaphores, stick[5], for each of the five chopsticks.

35
Code

36
ZAIB UNNISA UNIVERSITY OF GUJRAT
Dining Philosophers Problem
ZAIB UNNISA

 When a philosopher wants to eat the rice, he will wait for the chopstick at his left and picks up
that chopstick. Then he waits for the right chopstick to be available, and then picks it too. After
eating, he puts both the chopsticks down.
 But if all five philosophers are hungry simultaneously, and each of them pickup one chopstick,
UNIVERSITY OF GUJRAT

then a deadlock situation occurs because they will be waiting for another chopstick forever. The
possible solutions for this are:
• A philosopher must be allowed to pick up the chopsticks only if both the left and right chopsticks
are available.
• Allow only four philosophers to sit at the table. That way, if all the four philosophers pick up four
chopsticks, there will be one chopstick left on the table. So, one philosopher can start eating and
eventually, two chopsticks will be available. In this way, deadlocks can be avoided.
37
Monitors

https://www.tutorialandexample.com/monitors-in-operating-
system#:~:text=In%20other%20words%2C%20monitors%20are,between%20the%20concurrent%20procedure%20invocation
s
ZAIB UNNISA UNIVERSITY OF GUJRAT

You might also like