Professional Documents
Culture Documents
WEEK10 Synchronization 2
WEEK10 Synchronization 2
WEEK-10
Department of Software
Engineering, FOIT, UCP,
Lahore.
Process Synchronization
OS Based Solutions
• Semaphores
• Busy Waiting and its Solution
Classical Problems of Synchronization
• Bounded-Buffer Problem
• Reader-Writer Problem
• Dining-Philosophers Problem
Conditional Variables
Semaphores
Synchronization tool
Available in operating systems
Semaphore S – integer variable
that can only be accessed via
two indivisible (atomic)
operations, called wait and
signal
Semaphores
wait(S){
while(S 0); //Do nothing
S--;
}
signal(S){
S++;
}
n-Processes CS Problem
Shared data:
semaphore mutex = 1;
Structure of Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
} while (1);
Is it a Good Solution?
typedef struct {
int value;
struct process *L;
} semaphore;
Semaphore
Implementation
Assume two simple operations:
block() suspends the
process that invokes it.
wakeup(P) resumes the
execution of a blocked process
P.
Semaphore
Implementation
The negative value of S.value
indicates the number of
processes waiting for the
semaphore
A pointer in the PCB needed to
maintain a queue of processes
waiting for a semaphore
Semaphore wait()
Semaphore operations now
defined as
void wait(semaphore S){
S.value--;
if (S.value < 0) {
add this process to S.L;
block();
}
}
Semaphore signal()
void signal(semaphore S){
S.value++;
if (S.value <= 0) {
remove process P from S.L;
wakeup(P);
}
}
Analysis
Busy-waiting version is better
when critical sections are small
Queue-waiting version is better
for long critical sections (when
waiting is for longer periods of
time).
Difference between Mutex and Semaphore
P0 P1
signal(Q);
Starvation
(Infinite Blocking)
P0 P1
wait(S); wait(S);
wait(S); signal(S);
Violation of Mutual
Exclusion
P0 P1
signal(S); wait(S);
wait(S); signal(S);
Main Cause of Problem
and Solution
Cause: Programming errors due
to the tandem use of wait()
and signal() operations
Solution: Higher-level language
constructs such as critical
region (region statement) and
monitor.
Types of Semaphores
Counting semaphore – integer
value can range over an
unrestricted integer domain.
Binary semaphore – integer
value cannot be > 1; can be
simpler to implement.
Classical Problems of
Synchronization
Bounded-Buffer Problem
Readers and Writers Problem
Dining Philosophers Problem
Bounded Buffer Problem
Empty Pool
Producer Consumer
Full Pool
Bounded-Buffer
Problem
Shared data
semaphore full, empty, mutex;
Initialization
full = 0, empty = n, mutex = 1;
Producer Process
do {
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
} while (1);
Consumer Process
do {
wait(full);
wait(mutex);
…
remove an item from buffer to nextc
…
signal(mutex);
signal(empty);
…
consume the item in nextc
…
} while (1);
Readers and Writers
Problem
Readers Writers
Readers and Writers
Problem
Reader
Reader
Reader
Reader Writer
Reader Writer
Writer
Reader
Reader Writer
Reader Writer
Writer
Reader Writer
Writer
Shared Resource
Readers and Writers
Problem
Writer
Writer
Writer
Writer
Writer
Writer
Writer
Writer
Reader
Reader
Reader
Reader
Reader
Reader
Reader
Reader
Reader
Shared Resource
Readers and Writers
Problem
Reader
Reader
Reader
Reader Writer
Reader Writer
Writer
Reader
Reader Writer
Reader Writer
Writer
Reader Writer
Writer
Shared Resource
First Readers and
Writers Problem
If a writer is ready, it
waits for the minimum
amount of time.
First Readers and
Writers Problem
Shared data
semaphore mutex, wrt;
Initialization
mutex = 1, wrt = 1;
readcount = 0;
Writer Process
wait(wrt);
…
writing is performed
…
signal(wrt);
Reader Process
wait(mutex);
readcount++;
if (readcount == 1)
wait(wrt);
signal(mutex);
…
reading is performed
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex);
Dining Philosophers
Problem
Five philosophers who spend
their lives just thinking and
eating.
Only five chopsticks are
available to the philosophers
Dining Philosophers
Problem
Each philosopher thinks. When
he becomes hungry, he sits
down and picks up the two
chopsticks that are closest to
him and eats.
After a philosopher finishes
eating, he puts down the
chopsticks and starts to think.
Dining-Philosophers
Problem
Dining-Philosophers
Problem
Shared data : semaphore chopstick[5];
// Initialized to 1
Dining-Philosophers
Problem
Philosopher i
do {
wait(chopstick[i])
wait(chopstick[(i+1)% 5])
eat
signal(chopstick[i]);
signal(chopstick[(i+1)% 5])
think
} while (1);
Possibility of Deadlock
If all philosophers become
hungry at the same time and
pick up their left chopstick, a
deadlock occurs.
Dining Philosophers
Problem
Five philosophers who spend
their lives just thinking and
eating.
Only five chopsticks are
available to the philosophers
Dining Philosophers
Problem
Each philosopher thinks. When
he becomes hungry, he sits
down and picks up the two
chopsticks that are closest to
him and eats.
After a philosopher finishes
eating, he puts down the
chopsticks and starts to think.
Dining-Philosophers
Problem
Dining-Philosophers
Problem
Shared data : semaphore chopstick[5];
// Initialized to 1
Dining-Philosophers
Problem
Philosopher i
do {
wait(chopstick[i])
wait(chopstick[(i+1)% 5])
eat
signal(chopstick[i]);
signal(chopstick[(i+1)% 5])
think
} while (1);
Possibility of Deadlock
If all philosophers become
hungry at the same time and
pick up their left chopstick, a
deadlock occurs.
Possible Solutions
Allow at most four philosophers
to be sitting simultaneously at
the table.
Allow a philosopher to pick up
his/her chopsticks only if both
chopsticks are available (to do
this she must pick them up in a
critical section)
Possible Solutions
Use an asymmetric solution; that
is:
• an odd philosopher picks up first
her left chopstick, whereas
• an even philosopher picks up her
right chopstick and then her left
chopstick.
Possibility of
Starvation
Two philosophers who are fast
eaters and fast thinkers, and
lock the chopsticks before
others every time.
High-level
Synchronization
Constructs
Shift the responsibility of
enforcing mutual exclusion
from the programmer (where it
resides when semaphores are
used) to the compiler.
Critical Regions
A critical region is a
section of code that is
always executed under
mutual exclusion.
Critical Regions
They consist of two parts:
1.Variables that must be
accessed under mutual
exclusion.
2.A new language statement
that identifies a critical region
in which the variables are
accessed.
The region Statement
A shared variable v of type T, is
declared as:
v: shared T;
region v when B do S;
Variable v is accessed only inside
S when B is true and when S is
being executed, no other process
can access variable v.
The region Statement
When a process tries to execute
the region statement, the Boolean
expression B is evaluated. If B is
true, statement S is executed. If it
is false, the process is delayed until
B becomes true and no other
process is in the region associated
with v.
Bounded Buffer
Example
Shared data
struct buffer {
int pool[n];
int count, in, out;
}
Producer Process
Producer process inserts
nextp into the shared
buffer
region buffer when (count < n) {
pool[in] = nextp;
in:= (in+1) % n;
count++;
}
Consumer Process
Consumer process removes
an item from the buffer and
puts it in nextc
region buffer when (count > 0) {
nextc = pool[out];
out = (out+1) % n;
count--;
}
The region Statement
What happens when two
region statements are
executed simultaneously?
region v when B do S1;
region v when B do S2;
Monitors
High-level synchronization
construct that allows the safe
sharing of an abstract data
type among concurrent
cooperating processes.
Only one process at a time is
active within a monitor.
Schematic View of a
Monitor
Monitors
monitor monitor-name
{
shared variable declarations
procedure P1 (…) { . . . }
procedure P2 (…) { . . . }
…
{
initialization code
}
}
Monitors with
Condition Variables
Additional synchronization
constructs are needed to model
some synchronization
problems. They can be modeled
with condition variables.
condition x, y;
Monitors with
Condition Variables
Only two operations can be
performed on condition
variables, wait and signal.
x.wait: Process invoking this
operation is suspended until
another process invokes
x.signal.
Monitors with
Condition Variables
x.signal: Resumes exactly
one suspended process. If no
process is suspended, this is a
null operation, i.e., the state of x
is unaffected.