OS Process Synchronization LECTURE
OS Process Synchronization LECTURE
Process Synchronization
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Atomic Transactions
Background
Co-Operating Process: that can affect or be affected by other
processes executing in system
Concurrent access to shared data may result in data inconsistency
Process Synchronization: Ensures coordination among processes and
maintains Data Consistency
Process P1 Process P2
1. X=5
2. X=5+2
1. read(x);
2. x=x+5;
3. Printf( x);
Producer Consumer Problem
There can be two situations:
2. Bounderd Buffer:
1. Limited buffer size
Producer Consumer Problem
Bounderd Buffer:
while (true) {
while (count == 0) // buffer empty
{
; // do nothing
}
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count- -;
Process P1 Process P2
1. reads i=10
2. i=i+1 =11
Remainder Section
} until false.
Solution to: Critical Section
1. Mutual Exclusion:
It states that if one process is executing in its critical section, then
no other process can execute in its critical section.
2. Bounded Wait:
It states that if a process makes a request to enter its critical
section and before that request is granted, there is a limit on
number of times other processes are allowed to enter that
critical section.
Solution to: Critical Section
3. Progress:
It states that process cannot stop other process from entering
their critical sections, if it is not executing in its CS.
i.e. Decision of entry into Critical Section is made by
processes in Entry Section By Setting Lock in Entry
Section
b) if (shared variable == 2)
P2 can enter to Critical Section
2 Process S/w Approaches For C.S
Let shared variable is turn i.e. turn : 1 or 2
(Let i=1 and j=2)
Pi:
while (turn != i)
{ do skip; } // P2 is executing
P1 is in Critical Section;
turn=j; // turn=2;
do {
flag[ i ] =true; // Pi is ready to enter C.S
turn=j;
while(flag[j] && turn ==j); // (flag and turn of Pj is True, Until
this condition is true, Pi can not enter C.S)
CRITICAL SECTION
flag[i]=false;
REMAINDER SECTION
}while(1);
Synchronization Hardware
Hardware Solution to C.S.
Many systems provide hardware support for critical section code
Interrupt Disabling
Repeat
Disable interrupts
C.S
Enable interrupts
Remainder section
Hardware Solution to C.S.
2. Hardware instructions
1. Machines provide instructions that can read, modify
and store memory word
2. Common inst. are:
1. Test and Set
This inst. Provides action of testing a variable and set
its value
It executes atomically
Test and Set instructions operates on single Boolean
variable.
Hardware Solution to C.S.
//initially lock is
FALSE
Swap Instruction
Definition:
Types of Semaphores:
Counting semaphore – when integer value can be
any non-negative value
Binary semaphore – integer value can range only
between 0 and 1
Also known as mutex locks
Semaphore and Busy Waiting
Disadvantage of Semaphore: Busy Waiting
Two operations:
block – place the process invoking the operation on the
waiting queue.
wakeup – remove one of processes in the waiting queue and
place it in the ready queue.
Semaphore Implementation with no Busy
waiting
Implementation of wait:
S=1
wait (S){
value--;
if (value < 0) {
add process P to waiting queue
block();
}
C.S
}
Semaphore Implementation with no Busy
waiting
Implementation of signal:
Signal (S){
value++; //“value” has -ve value i.e -1 here
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
Deadlock and Starvation
mutex= 1
full= 0
empty= N
Bounded Buffer Problem (Cont.)
// produce an item
wait (empty);
wait (mutex);
signal (mutex);
signal (full);
}
Bounded Buffer Problem (Cont.)
while (true) {
wait (full);
wait (mutex); // mutex=1
Shared Data
Data set
For Readers: Semaphore mutex initialized to 1.
For Writers: Semaphore wrt initialized to 1.
Integer readcount initialized to 0.
Readers-Writers Problem (Cont.)
while (true) {
wait (wrt) ;
// writing is performed
signal (wrt) ;
}
Readers-Writers Problem (Cont.)
The structure of a reader process
while (true) {
wait (mutex) ;
readcount ++ ;
if (readcount == 1)
wait (wrt) ; //( don’t allow writers)
signal (mutex); //(allow other readers to come)
// reading is performed
Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
Dining-Philosophers Problem (Cont.)
While (true) {
wait ( chopstick[i] );
wait ( chopstick[ (i + 1) % 5] ); // =1
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
}
Problems with Semaphores
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
…
procedure Pn (…) {……}
Initialization code ( ….) { … }
…
}
}
Monitors
Schematic view of a Monitor
Let a process made the request to C.S. but no critical data is available, So
we would not go back to check all the conditions again to run the request,
As we have checked some conditions so processes will be lined up for
excess to C.S.
Monitor with Condition Variables
Solution to Dining Philosophers (cont)
Each philosopher ‘ i ’ invokes the operations pickup() and putdown() in
the following sequence:
dp.pickup (i)
EAT
dp.putdown (i)
Solution to Dining Philosophers
monitor DP
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
initialization_code() {
for (int i = 1; i <= 5; i++)
state[i] = THINKING;
}
}
MCQ Questions
Which process can be affected by other
processes executing in the system?
a) cooperating process
b) child process
c) parent process
d) init process
MCQ Questions
1a
MCQ Questions
When several processes access the same
data concurrently and the outcome of
the execution depends on the particular
order in which the access takes place,
is called
a) dynamic condition
b) race condition
c) essential condition
d) critical condition
MCQ Questions
2b
MCQ Questions
If a process is executing in its critical
section, then no other processes can
be executing in their critical section.
This condition is called
a) mutual exclusion
b) critical exclusion
c) synchronous exclusion
d) asynchronous exclusion
MCQ Questions
3a
MCQ Questions
Which one of the following is a
synchronization tool?
a) thread
b) pipe
c) semaphore
d) socket
MCQ Questions
4c
MCQ Questions
A semaphore is a shared integer variable
a) that can not drop below zero
b) that can not be more than zero
c) that can not drop below one
d) that can not be more than one
MCQ Questions
5a
MCQ Questions
Mutual exclusion can be provided by the
a) mutex locks
b) binary semaphores
c) both mutex locks and binary
semaphores
d) none of the mentioned
MCQ Questions
6c