Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. /*
  2. * Starter skeleton for Cafe using channels
  3. */
  4.  
  5. #define NCUSTS 3 /* number of customers */
  6. #define NCASHIERS 1 /* number of cashiers */
  7. #define NSERVERS 2 /* number of servers */
  8.  
  9. #define NOBODY 255 /* default assignment for nobody */
  10.  
  11. mtype = {CHILI, SANDWICH, PIZZA} ; /* the types of foods */
  12. mtype favorite_foods[3] ; /* default assignment of customers' favorite foods */
  13.  
  14.  
  15. /* Channel definitions (suggested) */
  16.  
  17.  
  18. /*
  19. * Channel for customers to give an order to the cashier
  20. */
  21. chan customer_order[NCUSTS] = [0] of { mtype } ;
  22. mtype { WAITING, ORDER } ;
  23.  
  24. /*
  25. * Channel for cashier to tell a customer they can order
  26. */
  27. chan cashier_order = [0] of { mtype, byte, mtype } ;
  28. mtype { AVAILABLE, ORDER_TAKEN } ;
  29.  
  30. /*
  31. * Channel for cashier to give a server a new order
  32. */
  33. chan cashier_to_server[NSERVERS] = [0] of { mtype, byte, mtype } ;
  34. mtype { COOK } ;
  35.  
  36. /*
  37. * Channel for a server to give a customer their
  38. * finished order
  39. */
  40. chan server_to_customer[NCUSTS] = [0] of { mtype, mtype } ;
  41. mtype { SERVED }
  42.  
  43.  
  44. /*
  45. * Process representing a customer.
  46. * Takes in their favorite food and an integer id
  47. * to represent them
  48. */
  49. proctype Customer(mtype favorite; byte id)
  50. {
  51. mtype order;
  52.  
  53. do
  54. ::
  55.  
  56. printf("C%d enters.\n", id) ;
  57.  
  58.  
  59. /* customer tells the cashier they are waiting to order */
  60. cashier_order ! WAITING, id ;
  61.  
  62. printf("C%d is waiting to order...\n", id) ;
  63.  
  64. /* block until the cashier says they may order */
  65. customer_order[id] ? AVAILABLE ;
  66.  
  67. /* tell the cashier their order */
  68. cashier_order ! ORDER, id, favorite ;
  69.  
  70. printf("C%d orders %e.\n", id, favorite);
  71.  
  72. /* block until the customer receives their food */
  73. server_to_customer[id] ? SERVED, order ;
  74.  
  75. printf("C%d leaves with %e.\n", id, order);
  76.  
  77. od ;
  78.  
  79. }
  80.  
  81.  
  82. /*
  83. * Process representing a cashier
  84. */
  85. proctype Cashier()
  86. {
  87.  
  88. /* the current customer ordering*/
  89. byte current ;
  90.  
  91. /* the current customer's order */
  92. mtype order ;
  93.  
  94. do
  95. ::
  96.  
  97. printf("Cashier is waiting for a new customer.\n");
  98.  
  99. /* block until a customer wants to order */
  100. cashier_order ? WAITING, current, order;
  101.  
  102. /* tell a customer they can order */
  103. customer_order[current] ! AVAILABLE ;
  104.  
  105. printf("Cashier tells C%d that they can order\n", current);
  106.  
  107. /* block until the cashier receives the customer's order */
  108. cashier_order ? ORDER, current, order;
  109.  
  110. printf("Cashier puts in C%d's order\n", current);
  111.  
  112. /* give the order to the server */
  113. cashier_order ! ORDER_TAKEN, current, order;
  114.  
  115. od ;
  116.  
  117.  
  118. }
  119.  
  120. /*
  121. * Process representing a server
  122. */
  123. proctype Server(byte id)
  124. {
  125. /* the customer the server is making food for */
  126. byte my_customer ;
  127.  
  128. /* the order the server is making */
  129. mtype order ;
  130.  
  131. do
  132. ::
  133. printf("Server%d is free.\n", id) ;
  134.  
  135. /* block until an order is given */
  136. cashier_order ? ORDER_TAKEN, my_customer, order;
  137.  
  138. printf("Server%d makes %e for C%d.\n", id, order, my_customer) ;
  139.  
  140. /* make and give the order to the customer */
  141. server_to_customer[my_customer] ! SERVED, order;
  142. od ;
  143.  
  144.  
  145. }
  146.  
  147.  
  148. /*
  149. * Sets up processes. This model creates three
  150. * customers with a specific favorite food, and
  151. * two servers.
  152. */
  153. init{
  154. favorite_foods[0] = PIZZA ;
  155. favorite_foods[1] = SANDWICH ;
  156. favorite_foods[2] = CHILI ;
  157. atomic{
  158. run Customer(PIZZA, 0) ;
  159. run Customer(SANDWICH, 1);
  160. run Customer(CHILI, 2);
  161. run Cashier();
  162. run Server(0);
  163. run Server(1);
  164.  
  165. }
  166.  
  167. }
  168.  
  169.  
  170. /*
  171. * Safety: The cashier always sends the correct order
  172. * for a customer to the servers.
  173. */
  174. ltl S_CashierCorrectOrder {
  175. true;
  176. }
  177.  
  178. /*
  179. * Safety: It is never the case that the two servers
  180. * are busy working on the same order.
  181. */
  182. ltl S_NeverSameOrder {
  183. true;
  184. }
  185.  
  186. /*
  187. * Liveness: If a customer wants to place
  188. * an order then a customer eventually does.
  189. */
  190. ltl L_EventuallyOrder {
  191. true;
  192. }
  193.  
  194. /*
  195. * Liveness: it is always the case that eventually
  196. * a server is busy making an order
  197. */
  198. ltl L_EventuallyServerBusy {
  199. true;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement