Professional Documents
Culture Documents
Synchronization
Part 4
Disabling Interrupts
While(1)
{
Disable Interrupts
CS
Enable interrupt
}
Quiz Question
Synchronization Mechanism
Busy waiting
Disadvantages of Busy Waiting Synchronization
Non-Busy Waiting Scheduling Algorithms
Sleep wake-up / Block wake-up Synchronization Mechanism
P1 P1
{ {
While (P2 is in CS); If (P2 is inside CS)
{
CS Sleep();
-- }
--
--}
CS
Wakeup(P2);
} Kernel Mode
P1 | P2 | P10 | P1 | P5 | P1
RAM
Important Point
Sleep wakeup P1
{
If (P2 is inside CS)
{
Sleep();
}
CS
Wakeup(P2); P2
} {
If (P1 is inside CS)
{
Sleep();
}
CS
Wakeup(P1);
}
Producer Consumer Problem
Producer Consumer Problem
Buffer
Producer Consumer Problem
Buffer
Every time while loop runs a number is
added to the buffer.
5
6 The consumer is removing one of the
numbers from the buffer.
Count = 2.
P Ready State
C Block State
Producer Consumer Problem
Buffer
10
Now the producer will be scheduled.
P C Ready State
Block State
DEAD LOCK
Dead lock Void consumer()
{
Void Producer() Int num;
While(1)
{ {
int num if (count ==0)
while (1) sleep();
{ Num = remove_num();
count = count -1;
num = produce_num();
If ( count = N-1)
If (count ==N) If the producer will be
wakeup (Producer);
preempted here.
Sleep(); }
Insert.num(num) // num is added into buffer. }
Count = count + 1;
If (count ==1) If the producer will be preempted after that line of code, the consumer will start
wakeup(consumer); executing and eventually go to sleep.
When the producer will start executing, it will start from sleep() instruction and
} therefore will go to sleep as well. Both producer and consumer will be in a block state
and no one is there to wake them up.
Dead lock
• This decision of preemption happened in user mode that’s why deadlock occurs.
a b S
b
int flaot int flaot
a b
Semaphore
Struct Semaphore
{
Boolean Value;
Queue type L;
}
Struct node S;
Semaphore
Wait (Semaphore S) Signal (Semaphore S)
{ {
If ( S.Value ==1) If ( S.L is empty)
S.Value = 0; S.Value = 1;
else else
{ {
Put the process’s PCB in S.L; select a process from S.L;
Sleep(); wakeup();
} }
} }
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0;
else
S {
Put the process’s PCB in S.L;
Sleep();
}
Value
}
Signal (Semaphore S)
L {
If ( S.L is empty)
S.Value = 1;
S.Value (this is value) else
S.L ( Queue of processes) {
select a process from S.L;
wakeup();
}
}
Semaphore
While (1)
{
Wait (S);
CS
Signal(S);
}
Semaphore
Signal (Semaphore S)
L {
If ( S.L is empty)
S.Value = 1;
S.Value (this is value) else
For S = 1, it means the critical section is free {
For S = 0, there is a process in the critical section select a process from S.L;
wakeup();
S.L ( Queue of processes) }
}
Semaphore Wait (Semaphore S)
{
S.Value = 1
else
section.
S
CS
{
Put the process’s PCB in S.L;
Sleep();
}
1 Value
}
Signal (Semaphore S)
L {
If ( S.L is empty)
S.Value = 1;
S.Value (this is value) else
S.L ( Queue of processes) {
select a process from S.L;
wakeup();
}
}
Semaphore Wait (Semaphore S)
{
of the function.
else
queue
P1: 1) 2) | P2: 1) {
Put the process’s PCB in S.L;
Sleep();
}
}
While (1)
{
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
2. CS (P1) If ( S.L is empty)
3.Signal(S); S.Value = 1;
} else
{
select a process from S.L;
S wakeup();
0 Value }
}
P2 L
Semaphore Wait (Semaphore S)
{
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P2 2. CS (P1) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P2 L
Semaphore Wait (Semaphore S)
{
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P2,P3 2. CS (P1) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
P2->P3 }
L
Semaphore Wait (Semaphore S)
{ Process P1 is
If ( S.Value ==1) scheduled
S.Value = 0; It executes its CS and
else comes out.
P1: 1) 2) | P2: 1) | P3: 1)| P1: { Checks if S.L is empty.
Put the process’s PCB in S.L; (NO)
Ready Sleep(); Then it will select a
state } process from the
} queue and wake it up.
While (1)
P1, P4 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P2,P3 2. CS (P1) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P2->P3 L
Question
Answer
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0;
else
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3) { P2 is wakened up by
Put the process’s PCB in S.L; the process P1.
Ready Sleep();
state }
}
While (1)
P1, P4,P2 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P3 2. CS (P1) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P3 L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0; P2 is wakened up by
else the process P1.
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3) { P2 will go to CS when
Put the process’s PCB in S.L; it wakes up. As next
Sleep(); instruction after sleep
Ready
} is CS.
state
}
While (1)
P1, P4,P2 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P3 2. CS (P2) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P3 L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0;
else
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) { P4 is scheduled
Put the process’s PCB in S.L;
Ready Sleep();
state }
}
While (1)
P1, P4, P2 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P3 2. CS (P2) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P3 L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0; P4 will check if the
else value of S=1, it is not
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) { so P4 will be added to
Put the process’s PCB in S.L; the queue and go to
Ready Sleep(); sleep.
state }
}
While (1)
P1, P2 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P3,P4 2. CS (P2) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P3 -> P4 L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0;
else P2 is scheduled. It
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) { executes its critical
Put the process’s PCB in S.L; section and then go
Sleep(); to signal(S).
Ready
state }
}
While (1)
P1, P2 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P3,P4 2. CS If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P3 -> P4 L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0;
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) else
{ P3 is scheduled.
P3: 2)
Put the process’s PCB in S.L;
Ready Sleep();
state }
}
While (1)
P1,P2, P3 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P4 2. CS (P3) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P4 L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1) P3 is scheduled.
S.Value = 0; It executes its critic
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) else section. After that, it
{ calls signal().
P3: 2) 3)
Put the process’s PCB in S.L; Here it checks the
Ready Sleep(); queue. It is not empty
state } so it wakes up P4.
}
While (1)
P1,P2, P3 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
P4 2. CS If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
P4 L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0;
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) else P4 is moved from
{ block to ready state.
P3: 2) 3) P4’s next instruction
Put the process’s PCB in S.L;
Sleep(); to be executed is CS.
Ready
state }
}
P1,P2, P3, While (1)
P4 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
2. CS (P4) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0;
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) else P4 is scheduled.
{ It executes its critical
P3: 2) 3) | P4: 2) section and goes to
Put the process’s PCB in S.L;
Sleep(); signal().
Ready
state }
}
P1,P2, P3, While (1)
P4 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
2. CS (P4) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0;
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) else P4 will check the list,
{ it is empty now.
P3: 2) 3) | P4: 2) 3) So the “value” will be
Put the process’s PCB in S.L;
Sleep(); set to 1.
Ready
state }
}
P1,P2, P3, While (1)
P4 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
2. CS If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
1 Value }
}
L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0; P1 is scheduled again.
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) else It will turn S.value =
{ 0.
P3: 2) 3) | P4: 2) 3) | P1: 1) 2)
Put the process’s PCB in S.L; It will execute its
Ready Sleep(); critical section.
state }
}
P1,P2, P3, While (1)
P4 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
2. CS (P1) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0; P1 will execute 3rd
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) else instruction, signal().
{ It will check if the
P3: 2) 3) | P4: 2) 3) | P1: 1) 2) 3)
Put the process’s PCB in S.L; queue is empty, then
Ready Sleep(); set s.value to 1.
state }
}
P1,P2, P3, While (1)
P4 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
2. CS (P1) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
0 Value }
}
L
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
S.Value = 0; P1 will execute 3rd
P1: 1) 2) | P2: 1) | P3: 1)| P1: 2) 3)| P4: 1) | P2: 2) 3) else instruction, signal().
{ It will check if the
P3: 2) 3) | P4: 2) 3) | P1: 1) 2) 3)
Put the process’s PCB in S.L; queue is empty, then
Ready Sleep(); set s.value to 1.
state }
}
P1,P2, P3, While (1)
P4 {
Four processes
P1, P2, P3, P4
1. Wait (S); Signal (Semaphore S)
{
2. CS (P1) If ( S.L is empty)
3.Signal(S); S.Value = 1;
Block } else
state {
select a process from S.L;
S wakeup();
1 Value }
}
L
Thank You