Presented to: Miss Najaf Khan 1
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
2
3
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).
4
THE DINING
PHILOSOPHE
RS
5
DINING PHILOSOPHER
BEHAVIOR
void typicalPhilosopher()
{
while ( true )
{
think();
Eat();
} // end while
} // end typicalPhilosopher
6
IMPLEMENTATION OF
METHOD EAT
void Eat()
{
pickUpLeftFork();
pickUpRightFork();
eatForSomeTime();
putDownRightFork();
putDownLeftFork();
} // Eat
7
STARVATION AND
DEADLOCK
Starvation
“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.”
8
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.
 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.
9
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.
11
 A monitor is a software module consisting of one or more procedures, an
initialization sequence, and local data.
 The chief characteristics of monitor are:
12
Local data variables are
accessible only by the
monitor’s procedures and not
by any external procedure
Process enters monitor by
invoking one of its
procedures
Only one process may be
executing in the monitor at a
time
 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.”
13
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
14
15
16
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;
}
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] );
}
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 */
}
}
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.
20
21

dining philosophers problem using montiors

  • 1.
    Presented to: MissNajaf Khan 1
  • 2.
    PRESENTED BY: Fatima Tariq13041519-004 Ghufran Qamar 13041519-011 Umair Arif 13041519-013 Warda Iftikhar 13041519-014 DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF GUJRAT-LAHORE CAMPUS 2
  • 3.
  • 4.
    THE DINING PHILOSOPHERS  FivePhilosophers 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). 4
  • 5.
  • 6.
    DINING PHILOSOPHER BEHAVIOR void typicalPhilosopher() { while( true ) { think(); Eat(); } // end while } // end typicalPhilosopher 6
  • 7.
    IMPLEMENTATION OF METHOD EAT voidEat() { pickUpLeftFork(); pickUpRightFork(); eatForSomeTime(); putDownRightFork(); putDownLeftFork(); } // Eat 7
  • 8.
    STARVATION AND DEADLOCK Starvation “A Conditionin 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.” 8 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.
  • 9.
     When eachphilosopher 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. 9
  • 10.
    10 The Solution must preventdeadlock (by forcing philosophers to put down Forks) but also prevent indefinite postponement by guaranteeing that each philosopher will obtain both forks time to time.
  • 11.
  • 12.
     A monitoris a software module consisting of one or more procedures, an initialization sequence, and local data.  The chief characteristics of monitor are: 12 Local data variables are accessible only by the monitor’s procedures and not by any external procedure Process enters monitor by invoking one of its procedures Only one process may be executing in the monitor at a time
  • 13.
     By enforcingthe 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.” 13
  • 14.
    Achieved by theuse 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 14
  • 15.
  • 16.
  • 17.
    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; }
  • 18.
    18 void release_forks(int pid) { intleft = 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] ); }
  • 19.
    19 void philosopher[k=0 to4 ] /* 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 */ } }
  • 20.
    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. 20
  • 21.

Editor's Notes

  • #15 A monitor supports synchronization by the use of condition variables that are contained within the monitor and accessible only within the monitor. Condition variables are a special data type in monitors, which are operated on by two functions: • cwait(c) : Suspend execution of the calling process on condition c . The monitor is now available for use by another process. • csignal(c) : Resume execution of some process blocked after a cwait on the same condition. If there are several such processes, choose one of them; if there is no such process, do nothing. Note that monitor wait and signal operations are different from those for the semaphore. If a process in a monitor signals and no task is waiting on the condition variable, the signal is lost.
  • #16 illustrates the structure of a monitor. Although a process can enter the monitor by invoking any of its procedures, we can think of the monitor as having a single entry point that is guarded so that only one process may be in the monitor at a time. Other processes that attempt to enter the monitor join a queue of processes blocked waiting for monitor availability. Once a process is in the monitor, it may temporarily block itself on condition x by issuing cwait(x) ; it is then placed in a queue of processes waiting to reenter the monitor when the condition changes, and resume execution at the point in its program following the cwait(x) call. If a process that is executing in the monitor detects a change in the condition variable x , it issues csignal(x) , which alerts the corresponding condition queue that the condition has changed.