You are on page 1of 49

Process

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

As P1 is only waiting in while loop, so this time is wasted User mode

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.

The count is the variable checking how


many numbers are present in the buffer at
any point in time.

Count = 2.

The producer will keep adding the


numbers if it will be scheduled by the
scheduler.
Producer Consumer Problem
Buffer
When the buffer becomes full, the producer should
5 go to sleep mode.
6
4 When the producer will go to sleep/block state, it
8 will never be scheduled by the scheduler until it is
9 called by the wakeup() system call.
3
4
The consumer is the process, that will wake up the
7
producer.
1
5
When the value of the count will become N-1, the
consumer will wake up the producer as space is
produced for one more number.
Producer Consumer Problem
Buffer
When the value of the count will become N-1, the consumer
will wake up the producer as space is produced for one more
6 number.
4
8 Due to the wakeup() system call, the producer will go to the
9 ready state from the sleep/block state.
3
4
7 C Ready State
1
5 P Block State
Producer Consumer Problem
Buffer

Whenever the producer will be scheduled it will add a number


6
in the buffer
4
8
Whenever a consumer will be scheduled it will consume/delete
9
one of the numbers from the buffer.
3
4
7 C P Ready State
1
5 Block State
Producer Consumer Problem
Buffer

What if consumer is scheduled for a long time and there is no


more number in the buffer, i.e. count = 0.

Then consumer should go to sleep

P Ready State

C Block State
Producer Consumer Problem
Buffer

10
Now the producer will be scheduled.

One number is added to the buffer, the value of count = 1.

When the value of the count was 0, the producer understands


that the consumer will be sleeping. So, on getting count =1, it
wakes up the consumer.

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.

• If this execution of instructions would be in kernel mode, then deadlock won’t


happen, as there will be no preemption.

• Simple sleep() and wakeup () is not enough.

• We must implement this decision of preemption in kernel mode.


Semaphore
Structure

Struct node Struct Semaphore


{ {
Int a; Boolean Value;
Float b: Queue type L;
} }
Struct node S; Struct node S;

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

• In computing, a system call (commonly abbreviated to


While (1)
syscall() ) is the programmatic way in which a computer {
program requests a service from the kernel of the operating Wait (S);
system on which it is executed. CS
Signal(S);
• If wait() and signal() were implemented in user mode, then
}
preemptions can happen.

• As wait() and Signal() are systems calls therefore


preemptions cannot happen in kernel mode.

• If preemptions won't happen, we will not come across


problems like dead lock.
Semaphore Wait (Semaphore S)
{
If ( S.Value ==1)
Initial phase S.Value = 0;
else
S {
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
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)
{

P1 will set the value to 0 as it


It means no process is in the

wants to go to the critical


If ( S.Value ==1)
Four processes S.Value = 0;
P1, P2, P3, P4

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)
{

Process P2 will go to else part


section. So in wait system call,
S.Value = 0, it means another

Else part will put P2 in the


If ( S.Value ==1)

process is using its critical


S.Value = 0;

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)
{

Process P2 will go to sleep().


If ( S.Value ==1)
S.Value = 0;
else
P1: 1) 2) | P2: 1) {
Put the process’s PCB in S.L;
Ready Sleep();
state }
}
While (1)
P1, P3, P4 {

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)
{

Process P3 will go to sleep().


If ( S.Value ==1)
S.Value = 0;
else
P1: 1) 2) | P2: 1) | P3: 1) {
Put the process’s PCB in S.L;
Ready Sleep();
state }
}
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
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

You might also like