0% found this document useful (0 votes)
40 views34 pages

Lec25 CS604 Pps

Uploaded by

Ibrahim Choudary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views34 pages

Lec25 CS604 Pps

Uploaded by

Ibrahim Choudary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 34

Operating

Systems
Lecture 25
Syed Mansoor Sarwar
Agenda for Today
 Review of previous lecture
 Dining philosophers problem

 High-level synchronization
constructs
 Critical region

 Monitor

 Recap of lecture
14 September 2019 © Copyright Virtual University of
Pakistan
Review of Lecture 24
 Counting semaphores
 Classical synchronization
problems
 Bounded buffer problem

 Readers and writers problem

 Dining philosophers problem


14 September 2019 © Copyright Virtual University of
Pakistan
Dining Philosophers
Problem

 Five philosophers who spend


their lives just thinking and
eating.
 Only five chopsticks are
available to the philosophers
14 September 2019 © Copyright Virtual University of
Pakistan
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.
14 September 2019 © Copyright Virtual University of
Pakistan
Dining-Philosophers
Problem

14 September 2019 © Copyright Virtual University of


Pakistan
Dining-Philosophers
Problem
Shared data : semaphore chopstick[5];
// Initialized to 1

14 September 2019 © Copyright Virtual University of


Pakistan
Dining-Philosophers
Problem
Philosopher i
do {
wait(chopstick[i])
wait(chopstick[(i+1)% 5])
eat
signal(chopstick[i]);
signal(chopstick[(i+1)% 5])
think
}14while (1);
September 2019 © Copyright Virtual University of
Pakistan
Possibility of Deadlock
If all philosophers become
hungry at the same time and
pick up their left chopstick,
a deadlock occurs.

14 September 2019 © Copyright Virtual University of


Pakistan
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)
14 September 2019 © Copyright Virtual University of
Pakistan
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.
14 September 2019 © Copyright Virtual University of
Pakistan
Possibility of
Starvation
 Two philosophers who are fast
eaters and fast thinkers, and
lock the chopsticks before
others every time.

14 September 2019 © Copyright Virtual University of


Pakistan
High-level
Synchronization
Constructs
Shift the responsibility of
enforcing mutual exclusion
from the programmer (where it
resides when semaphores are
used) to the compiler.
14 September 2019 © Copyright Virtual University of
Pakistan
Critical Regions
A critical region is a
section of code that is
always executed under
mutual exclusion.

14 September 2019 © Copyright Virtual University of


Pakistan
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.
14 September 2019 © Copyright Virtual University of
Pakistan
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.
14 September 2019 © Copyright Virtual University of
Pakistan
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.
14 September 2019 © Copyright Virtual University of
Pakistan
Bounded Buffer
Example

 Shared data
struct buffer {
int pool[n];
int count, in, out;
}
14 September 2019 © Copyright Virtual University of
Pakistan
Producer Process
 Producer process inserts
nextp into the shared
buffer
region buffer when (count < n) {
pool[in] = nextp;
in:= (in+1) % n;
count++;
}
14 September 2019 © Copyright Virtual University of
Pakistan
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--;
}
14 September 2019 © Copyright Virtual University of
Pakistan
The region Statement
 What happens when two
region statements are
executed simultaneously?
region v when B do S1;
region v when B do S2;

14 September 2019 © Copyright Virtual University of


Pakistan
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.
14 September 2019 © Copyright Virtual University of
Pakistan
Schematic View of a
Monitor

14 September 2019 © Copyright Virtual University of


Pakistan
Monitors
monitor monitor-name
{
shared variable declarations
procedure P1 (…) { . . . }
procedure P2 (…) { . . . }

{
initialization code
}
}
14 September 2019 © Copyright Virtual University of
Pakistan
Monitors with
Condition Variables
 Additional synchronization
constructs are needed to model
some synchronization
problems. They can be modeled
with condition variables.
condition x, y;
14 September 2019 © Copyright Virtual University of
Pakistan
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.
14 September 2019 © Copyright Virtual University of
Pakistan
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.

14 September 2019 © Copyright Virtual University of


Pakistan
Monitor with Condition
Variables

14 September 2019 © Copyright Virtual University of


Pakistan
Dining Philosophers
Example
monitor dp
{
enum {thinking, hungry, eating} state[5];
condition self[5];
void pickup(int i) // Following slides
void putdown(int i) // Following slides
void test(int i) // Following slides
void init() {
for (int i = 0; i < 5; i++)
state[i] = thinking;
}
} 14 September 2019 © Copyright Virtual University of
Pakistan
Dining Philosophers
void pickup(int i) {
state[i] = hungry;
test(i);
if (state[i] != eating)
self[i].wait();
}
14 September 2019 © Copyright Virtual University of
Pakistan
Dining Philosophers
void putdown(int i) {
state[i] = thinking;
// test left and right
// neighbors
test((i+4) % 5);
test((i+1) % 5);
}
14 September 2019 © Copyright Virtual University of
Pakistan
Dining Philosophers
void test(int i) {
if ((state[(i+4)%5]!= eating)
&& (state[i] == hungry) &&
(state[(i+1)%5]!= eating))
{
state[i] = eating;
self[i].signal();
}
}14 September 2019 © Copyright Virtual University of
Pakistan
Recap of Lecture
 Dining philosophers problem
 High-level language
constructs
 Critical region

 Monitor

14 September 2019 © Copyright Virtual University of


Pakistan
Operating
Systems
Lecture 25
Syed Mansoor Sarwar

You might also like