dining philosophers problem using montiors

21
OPERATING SYSTEMS Presented to: Miss Najaf Khan 1

Upload: warda-chaudhry

Post on 12-Aug-2015

16 views

Category:

Software


1 download

TRANSCRIPT

Page 1: dining philosophers problem using montiors

1

OPERATING SYSTEMS

Presented to: Miss Najaf Khan

Page 2: dining philosophers problem using montiors

2

PRESENTED BY:Fatima Tariq 13041519-004

Ghufran Qamar 13041519-011

Umair Arif 13041519-013

Warda Iftikhar 13041519-014

DEPARTMENT OF COMPUTER SCIENCE

UNIVERSITY OF GUJRAT-LAHORE CAMPUS

Page 3: dining philosophers problem using montiors

3

THE DINING-PHILOSOPHERS PROBLEM

Page 4: dining philosophers problem using montiors

4

THE DINING PHILOSOPHERS

Five Philosophers live in a house, where a table is laid for them.

The life of each philosopher consists principally of thinking and eating.

Through years of thought, all of the philosophers had agreed that the only food that contributed to their thinking is Spaghetti.

Eating Spaghetti ( in most proper manner) requires that a philosopher use both adjacent forks (simultaneously).

Page 5: dining philosophers problem using montiors

5

THE DINING PHILOSOPHERS

Page 6: dining philosophers problem using montiors

6

DINING PHILOSOPHER BEHAVIORvoid typicalPhilosopher()

{

while ( true )

{

think();

Eat();

} // end while

} // end typicalPhilosopher

Page 7: dining philosophers problem using montiors

7

IMPLEMENTATION OF METHOD EATvoid Eat()

{

pickUpLeftFork();

pickUpRightFork();

eatForSomeTime();

putDownRightFork();

putDownLeftFork();

} // Eat

Page 8: dining philosophers problem using montiors

8

STARVATION AND DEADLOCKStarvation

“A Condition in Which Process is Indefinitely Delayed Because Other

Processes are always given preference.”

Deadlock

“Situation in Which a Process or Thread is Waiting for an Event that

will Never Occur.”

In this Problem, Philosophers operate asynchronously and concurrently, it is possible for each philosopher might pick up Left Fork before any philosopher pick up Right Fork. In this case, each Philosopher will hold exactly one fork, and no forks will remain available on the table. The philosophers will all deadlock and starve.

Page 9: dining philosophers problem using montiors

9

BREAKING DEADLOCK

When each philosopher holds a fork a Left Fork is to force one or more philosophers to put their left fork down so that another philosopher may grab it as Right Fork.

To implement this rule, pickUpRightFork() can specify that a philosopher puts down the left fork if that philosopher cannot obtain the right fork.

However, in this case, it is possible that each philosopher will pick up and put down its left fork for repeatedly in tandem without ever obtaining the two forks it needs to eat spaghetti.

In this case, the philosophers are not deadlocked, suffering from indefinite postponement, and they will still starve.

Page 10: dining philosophers problem using montiors

10

The Solution must prevent deadlock

(by forcing philosophers to put down Forks) but also prevent

indefinite postponement by guaranteeing that each philosopher will obtain both

forks time to time.

Page 11: dining philosophers problem using montiors

11 MONITORS

Page 12: dining philosophers problem using montiors

12

WHAT IS A MONITOR? A monitor is a software module consisting of one or more procedures, an

initialization sequence, and local data.

The chief characteristics of monitor are:

Local data variables are accessible only by the monitor’s procedures

and not by any external procedureOnly one process may be executing

in the monitor at a time

Page 13: dining philosophers problem using montiors

13

MONITORS PROVIDES MUTUAL EXCLUSION By enforcing the discipline of one process at a time, the monitor is able to

provide a mutual exclusion facility. The data variables in a monitor can be accessed by only one process at a time.

A shared data structure can be protected by placing it in a monitor. If the data in a monitor is representing some resource, then the monitor provides a mutual exclusion facility for accessing the resource.

Mutual Exclusion:

“A Condition in Which there is a set of Processes, only one of Which is able to access a given Resource or Perform a given function at any time.”

Page 14: dining philosophers problem using montiors

14

SYNCHRONIZATION Achieved by the use of condition variables that are contained within the monitor and accessible only within the monitor

Condition variables are operated on by two functions: cwait(c): suspend execution of the calling process on

condition c csignal(c): resume execution of some process blocked

after a cwait on the same condition

Page 15: dining philosophers problem using montiors

15

STRUCTURE OF A MONITOR

Page 16: dining philosophers problem using montiors

16

DINING- PHILOSOPHERS SOLUTION USING MONITORS

Page 17: dining philosophers problem using montiors

17

Monitor dining_controller;Cond forkReady[5]; /*condition variable for synchronization */Boolean fork[5] = { true }; /*availability of each fork */

Void get_forks(int pid){ int left= pid;

int right = (++pid) % 5;/* grant the left fork */

if( !fork(left) cwait(ForkReady[left]); /* queue on condition variable

*/fork(left) = false;/* grants the right fork */

if( !fork(right)cwait (ForkReady[right]); /* queue on condition

variable */fork(right) = false;

}

Page 18: dining philosophers problem using montiors

18

void release_forks(int pid){

int left = pid ;int right = (++pid) % 5 ;/* release the empty fork */

if( empty(ForkReady[left] ) /* no one is waiting for this fork */fork[left0 = true ;

else /* awaken a process waiting for this fork */

csignal{ForkReady{left]);/* release the right fork */

if( empty{ForkReady[right]) /* no one is waiting for this fork */fork(right) = true;

else /* awaken a process waiting for this fork */csignal(ForkReady[right] );

}

Page 19: dining philosophers problem using montiors

19

void philosopher[k=0 to 4 ] /* the philosopher clients */{

while( true ){

<think>;get_forks(k); /* client requests two forks via monitor

*/<eat spaghetti>;release_forks(k); /* client releases forks via the

monitor */}

}

Page 20: dining philosophers problem using montiors

20

REFERENCES

Stallings, W. (2009). OPERATING SYSTEMS: INTERNALS AND DESIGN PRINCIPLES, SIXTH EDITION. Upper Saddle River, NJ, USA : Prentice-Hall Inc.

Deital, H. M., & Deital, P. J., & Choffnes D. R. (2003). OPERATING SYSTEMS, TIRTH EIDITION. Upper Saddle River, NJ, USA : Prentice-Hall Inc.

Silberschatz, A., & Galvin, P. B., & Gagne, G. (2013). OPERATING SYSTEMS CONCEPTS, NINTH EIDITION.  Honoken, NJ, USA :John Wiley & Sons, Inc.

Page 21: dining philosophers problem using montiors

21

That’s All… QUESTIONS??