You are on page 1of 9

Problems with Semaphores

 Incorrect use of semaphore operations:

 signal (mutex) …. wait (mutex)

 wait (mutex) … wait (mutex)

 Omitting of wait (mutex) or signal (mutex) (or


both)

 Deadlock and starvation are possible.

Operating System Concepts – 9th Edition 5.1 Silberschatz, Galvin and Gagne
Monitors
 A high-level abstraction that provides a convenient and
effective mechanism for process synchronization
 Abstract data type, internal variables only accessible by
code within the procedure
 Only one process may be active within the monitor at a time
 But not powerful enough to model some synchronization
schemes

monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }

procedure Pn (…) {……}

Initialization code (…) { … }


}
}

Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne
Schematic view of a Monitor

Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne
Condition Variables
 condition x, y;
 Two operations are allowed on a condition variable:
 x.wait() – a process that invokes the operation
is suspended until x.signal()
 x.signal() – resumes one of processes (if any)
that invoked x.wait()
 If no x.wait() on the variable, then it has no
effect on the variable

Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne
Monitor with Condition Variables

Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne
Condition Variables Choices
 If process P invokes x.signal(), and process Q is
suspended in x.wait(), what should happen next?
 Both Q and P cannot execute in parallel. If Q is resumed,
then P must wait
 Options include
 Signal and wait – P waits until Q either leaves the
monitor or it waits for another condition
 Signal and continue – Q waits until P either leaves the
monitor or it waits for another condition
 Both have pros and cons – language implementer can
decide
 Monitors implemented in Concurrent Pascal compromise
 P executing signal immediately leaves the monitor,
Q is resumed
 Implemented in other languages including Mesa, C#,
Java

Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne
Monitor Solution to Dining Philosophers
monitor DiningPhilosophers
{
enum { THINKING, HUNGRY, EATING } state [5] ;
condition self [5];

void pickup (int i) {


state[i] = HUNGRY;
test(i);
if (state[i] != EATING)
self[i].wait;
}

void putdown (int i) {


state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}

Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne
Solution to Dining Philosophers (Cont.)

void test (int i) {


if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ;
self[i].signal () ;
}
}

initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}

Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne
Solution to Dining Philosophers (Cont.)

 Each philosopher i invokes the operations pickup()


and putdown() in the following sequence:

DiningPhilosophers.pickup(i);

EAT

DiningPhilosophers.putdown(i);

 No deadlock, but starvation is possible

Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne

You might also like