Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2011
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. monitor Smokers {
  2.     enum { PAPER, TOBACCO, MATCHES } materials[3];
  3.     enum { SMOKING, WAITING } state[3];
  4.     condition self[3];
  5.    
  6.     void smoke(int i) {
  7.         state[i] = WAITING;
  8.         test(i);
  9.         if(state[i] != SMOKING) self[i].wait();
  10.     }
  11.    
  12.     void stopSmoking(int i) {
  13.         state[i] = WAITING;
  14.         test((i + 2) % 3);
  15.         test((i + 1) % 3);
  16.     }
  17.    
  18.     void test(int i) {
  19.         //not smoking & has different materials from those on the table
  20.         if(state[i] == WAITING &&
  21.         materials[i] != materials[(i + 2) % 3] &&
  22.         materials[i] != materials[(i + 1) % 3])
  23.         {
  24.             state[i] = SMOKING;
  25.             self[i].signal();
  26.         }
  27.     }
  28.  
  29.     initialization_code() {
  30.         for(int i = 0; i < 3; i++) {
  31.             state[i] = WAITING;
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement