In 1965, Dijkstra Posed and Solved a Synchronization Problem He Called THE
Dining philosophers problem. ....... Problem can be stated QUITE SIMPLY
AS FOLLOWS. Five Philosophers Are Seated Around a Circular Table. Each Philo
Sopher Has a Plate of Spaghetti. The spaghetti is so slippery That a Philoso
PHER NEEDS TWO forks to Eat It. Between Each Pair of Plates Is One fork.
The Life of a Philosopher Consists of Alternate Periods of Eating and Think
Ing. When a Philosopher Gets Hungry, She Tries To Acquire Her Left and Right
Fork, One At a Time, IN Either Order. if Successful In acquiring two forks,
SHE Eats for a while, the puts down the forks and continues to think..
Key Question Is: Can You Write a Program for Each Philosopher That Does What
IT is supposed to do and nets stuck?
--FROM
Written by Andrew S. Tanenbaum
Typed by foolball :-P
Program advance by ya
::: 1: Use public documents, according to strict turning
:: #include
:: #include
:: #include
:: #include
: #Define n 5
:: INT I, J, T, STATUS;
:: File * f;
:: char * state [n];
::: main ()
:: {
:: F = fopen ("/ share", "w ");
:: PUTC (J, F);
:::: (For (fork ())
:: {IF (fork ())
:: {IF (fork ())
:: {IF (fork ())
:: {IF (fork ())
:: {WaitPID (-1, * status, 0);
:: Fclose (f);
:::: ELSE
:: Philosophy (4);}
:::: ELSE
:: Philosophy (3);
:::: ELSE
:: Philosophy (2);
:::: ELSE
:: Philosophy (1);}
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::}
:: Void Philosophy (INT i)
:: {
:::::::::::::::::::::
:::: Printf ("% D% S / N", I, "IS thinking");
::: For (t = 0; t <= rand () 10000; t );
::::::: ";";
:: Printf ("% D% S / N", I, "is Hungry");
::: For (t = 0; t <= rand () 10000; t );
::: For (;; i! = J;)
:: {FSeek (f, 0L, 0);
:: J = getc (f);}
:::::::::::
::: Printf ("% D% S / N", I, "is eating");
:: J = (j 1)% n
:: Fseek (f, 0l, 0);
:: PUTC (J, F);
::}
::: 二: Implementation by file lock
:: #include
:: #include
: #Define N 4
:: File * f;
:: INT I, STATUS;
:: char * state [n];
:: Void Philosofy (INT I);
:: Void main ()
:: {
:: IF ((f = fopen ("Turn", "W ")) == NULL)
:: {
:: Printf ("Cann't Open");
::: EXIT (0);
::}
:::: (For (fork ())
:: {
:::: (For (fork ())
:: {
:::: (For (fork ())
:: {
:::: (For (fork ())
:: {
:::: (For (fork ())
:: {
:: Waitpid (-1, & status, 0);
:: Fclose (f);
::}
:::: ELSE
::: Philosofy (4);
::}
:::: ELSE
:: Philosofy (3);
::}
:::: ELSE
:: Philosofy (2);
::}
:::: ELSE
:: Philosofy (1);
::}
:::: ELSE
::: Philosofy (0);
:::} // end of main
:: Void Philosophy (INT i)
:: {INT T;
:::::::::::::::::::::
:::: Printf ("% D% S / N", I, "IS thinking");
::: For (t = 0; t <= rand () 10000; t );
::::::: ";";
:: Printf ("% D% S / N", I, "is Hungry");
::: For (t = 0; t <= rand () 10000; t );
::: While ((f = fopen ("Turn.lock", "R")))! = Null);
:: LINK ("Turn", "Turn.lock");
:::::::::::
::: Printf ("% D% S / N", I, "is eating");
::: For (t = 1; t <= 10000 rand (); t );
:: Unlink ("turn");
::}