0% found this document useful (0 votes)
60 views5 pages

MONITORS and Dining Philospher Solution

Monitors provide a mechanism for process synchronization through the use of abstract data types and mutual exclusion. Only one process can be active within a monitor at a time. Condition variables allow processes to wait on and signal specific conditions within a monitor. The monitor solution to the dining philosophers problem avoids deadlock but allows for possible starvation through the use of shared state variables and condition signaling.

Uploaded by

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

MONITORS and Dining Philospher Solution

Monitors provide a mechanism for process synchronization through the use of abstract data types and mutual exclusion. Only one process can be active within a monitor at a time. Condition variables allow processes to wait on and signal specific conditions within a monitor. The monitor solution to the dining philosophers problem avoids deadlock but allows for possible starvation through the use of shared state variables and condition signaling.

Uploaded by

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

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 (…) { … }

}
Condition Variables

n condition x, y;

n Two operations are allowed on a condition variable:

l x.wait() – a process that invokes the operation is suspended until x.signal()

l 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


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);

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;

• Each philosopher i invokes the operations pickup() and putdown() in the following
sequence:

• DiningPhilosophers.pickup(i);
o EAT
• DiningPhilosophers.putdown(i);

• No deadlock, but starvation is possible

Sleeping Barber Solution Code

Semaphore Customers = 0;
Semaphore Barber = 0;
Mutex Seats = 1;
int FreeSeats = N;

Barber {
while(true) {
/* waits for a customer (sleeps). */
down(Customers);
/* mutex to protect the number of available seats.*/
down(Seats);

/* a chair gets free.*/


FreeSeats++;

/* bring customer for haircut.*/


up(Barber);

/* release the mutex on the chair.*/


up(Seats);
/* barber is cutting hair.*/
}
}

Customer {
while(true) {
/* protects seats so only 1 customer tries to sit
in a chair if that's the case.*/
down(Seats); //This line should not be here.
if(FreeSeats > 0) {

/* sitting down.*/
FreeSeats--;

/* notify the barber. */


up(Customers);

/* release the lock */


up(Seats);

/* wait in the waiting room if barber is busy. */


down(Barber);
// customer is having hair cut
} else {
/* release the lock */
up(Seats);
// customer leaves
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy