Professional Documents
Culture Documents
Properties
5
ZAIB UNNISA UNIVERSITY OF GUJRAT
Value of Semaphore
ZAIB UNNISA
Zero
Positive
UNIVERSITY OF GUJRAT
Negative
6
Another name of Semaphore
ZAIB UNNISA
7
Implementation
ZAIB UNNISA
UNIVERSITY OF GUJRAT
signal ()
8
Implementation
ZAIB UNNISA
Wait ()
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
<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.
analogy
UNIVERSITY OF GUJRAT
Example
ZAIB UNNISA
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)
P3
P2 P1
.
.
.
Wait(B); Wait(A);
C
. .
Signal(B);
B; .
.
Signal(A); A;
.
.
21
ZAIB UNNISA UNIVERSITY OF GUJRAT
Problems with
ZAIB UNNISA
semaphores
UNIVERSITY OF GUJRAT
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
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
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);
32
Dining Philosophers Problem
34
Solution
ZAIB UNNISA
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