2 - OS Process Synchronization
2 - OS Process Synchronization
Synchronization Hardware
Hardware Solution to C.S.
Many systems provide hardware support for critical section code
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:
While (true){
Key=TRUE;
While(key==TRUE)
Swap(&lock, &key);
// critical section
lock=FALSE;
//remainder section
}
Semaphore
Synchronization tool that maintains concurrency using variables
Semaphore S is a integer variable which can take positive values
including 0. It is accessed from 2 operations only.
Operations On Semaphore:
wait() and signal()
signal (S) {
S++; }
Semaphore as General Synchronization Tool
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=3
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
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
Deadlock and Starvation
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
…
procedure Pn (…) {……}
Initialization code ( ….) { … }
…
}
}
Monitors
Schematic view of a Monitor
mutex= 1
full= 0
empty= N
Bounded Buffer Problem (Cont.)
The structure of the producer process
while (true) {
// 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
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
Dining-Philosophers Problem (Cont.)
The structure of Philosopher i:
While (true) {
wait ( chopstick[i] );
wait ( chopstick[ (i + 1) % 5] ); // =1
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
}
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