home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / CLDEC87.ZIP / TURING.LTG < prev   
Encoding:
Text File  |  1987-11-23  |  3.8 KB  |  107 lines

  1.  
  2.  
  3. Here is the complete text for the DIning Philosopher's example written in 
  4. the Turing Plus programming language.  
  5.             Stephen Perelgut 
  6. ------------------------- Dining Philosophers --------------------------
  7. randomize    % re-initialize the random number generator each run
  8. const *numPhilosophers := 5    % maximum number of philosophers
  9.  
  10. monitor Table    % mutually exclusive access to forks
  11.     export  unqualified GetFork,
  12.         unqualified GiveFork
  13.  
  14.     var ForkInUse : array 1..numPhilosophers of boolean    % indicate fork in use
  15.     for i : 1..numPhilosophers
  16.     /* initially all forks are free */
  17.     ForkInUse(i) := false
  18.     end for
  19.  
  20.     var ForkQueue : array 1..numPhilosophers of condition
  21.  
  22.     /* Each philosopher brings one fork to the table and needs
  23.      * two forks to eat.  GetFork accesses the specified fork
  24.      * by seeing if it is in use (and waiting for it to be freed
  25.      * if it is in use) and then marking it as being used.  GiveFork
  26.      * marks a fork as being available again and signals someone
  27.      * waiting for that fork that it is now available
  28.      */
  29.  
  30.     procedure GetFork(Fork: 1..numPhilosophers)
  31.     post ForkInUse(Fork)
  32.  
  33.     if ForkInUse(Fork) then
  34.         wait ForkQueue(Fork)
  35.     end if
  36.     assert not ForkInUse(Fork)
  37.     ForkInUse(Fork) := true
  38.     end GetFork
  39.  
  40.     procedure GiveFork(Fork : 1..numPhilosophers)
  41.     pre ForkInUse(Fork)
  42.     ForkInUse(Fork) := false
  43.     signal ForkQueue(Fork)
  44.     end GiveFork
  45. end Table
  46.  
  47. const numBites := 25    % number of bites required to finish the meal
  48. const maxThinkTime := 5    % maximum number of seconds the philosopher will think
  49. const maxBiteSize := 10    % maximum number of seconds the philosopher will eat
  50.  
  51. /* This process will be activated for each philosopher.  It repeatedly
  52.  * consumes a random number of bites after following this sequence:
  53.  *    think, get left fork, think, get right fork, eat, return forks
  54.  * This continues until all the food is consumed (unless deadlock occurs
  55.  * with each philosopher clutching exactly one fork.รจ */
  56.  
  57. process Philosopher (phil : 1..numPhilosophers)
  58.     var bitesLeft := numBites
  59.     loop    % activities that form a single bite
  60.  
  61.     /* think before first Fork */
  62.     var thinkTime : int
  63.     randint(thinkTime, 1, maxThinkTime)
  64.     put "Philosopher ", phil, " will think for ", thinkTime, " seconds."
  65.     pause thinkTime
  66.     put "Philosopher ", phil, " has completed thinking."
  67.  
  68.     /* get left Fork - one philosopher brought to the table */
  69.     put "Philosopher ", phil, " reaching for left Fork."
  70.     GetFork(phil)
  71.     put "Philosopher ", phil, " has obtained left Fork."
  72.  
  73.     /* think a bit more before getting the next Fork */
  74.     randint(thinkTime, 1, maxThinkTime)
  75.     put "Philosopher ", phil, " will pause for ", thinkTime, " seconds."
  76.     pause thinkTime
  77.     put "Philosopher ", phil, " has completed pausing."
  78.  
  79.     /* get right Fork - one the nextdoor philospher brought */
  80.     put "Philosopher ", phil, " reaching for right Fork."
  81.     GetFork(phil mod numPhilosophers + 1)
  82.     put "Philosopher ", phil, " has obtained right Fork."
  83.  
  84.     /* time to eat */
  85.     var biteSize : int
  86.     randint(biteSize, 1, maxBiteSize)
  87.     put "Philosopher ", phil, " will eat for ", biteSize, " cookies."
  88.     biteSize := min(biteSize, bitesLeft)    % can only eat what's there
  89.     pause biteSize    % one second per bite - chewing 30 times :-)
  90.     put "Philosopher ", phil, " has completed eating."
  91.  
  92.     /* give back the Forks */
  93.     GiveFork(phil)
  94.     GiveFork(phil mod numPhilosophers + 1)
  95.     bitesLeft -= biteSize
  96.     exit when bitesLeft = 0
  97.     put "Philosopher ", phil, " has ", bitesLeft, " cookies left to eat."
  98.     end loop
  99.  
  100.     put "Philosopher ", phil, " has completed his meal."
  101. end Philosopher
  102.  
  103. for i : 1..numPhilosophers
  104.     fork Philosopher(i)    % start up each philosopher in turn
  105. end for
  106.  
  107.