home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog3 / chap26.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  14.4 KB  |  350 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 26
  5.                                        THE CONDITIONAL RENDEZVOUS
  6.  
  7.  
  8. IMPATIENT PROGRAMMING
  9. _________________________________________________________________
  10.  
  11. Many times in your life you wish to do something but you decide
  12. after trying for a short period of time, that it cannot be done,
  13. so you change your mind and do something else.  This capability
  14. must be available in a computer program also, since many times a
  15. computer program is used to simulate something in real life.  This
  16. chapter will give examples of impatient programs, those that refuse
  17. to simply wait forever for a certain job to be done.
  18.  
  19.  
  20. THE BASIC PROGRAM OUTLINE
  21. _________________________________________________________________
  22.  
  23. Examine the file named MEALS1.ADA for the        ================
  24. program that will serve as the outline for the      MEALS1.ADA
  25. selective rendezvous even though it does not     ================
  26. contain one.  The program consists of four
  27. tasks, one each to describe Bill's day and
  28. John's day, and one each to describe eating a meal at a restaurant
  29. and at a fast food Burger Boy (hopefully not an actual restaurant).
  30. In this program, all of the delay times have been defined in terms
  31. of a constant named HOURS which actually causes a delay of one
  32. second per hour to speed up program execution to a reasonable
  33. level.
  34.  
  35. Eating a meal at the restaurant requires half an hour to be served
  36. and another half hour to consume the meal, whereas the Burger Boy
  37. requires only one tenth of an hour to be served and another tenth
  38. to consume the meal.  The task named Bills_Day has him eating three
  39. meals only one hour apart, and the task named Johns_Day has him
  40. eating two meals in rapid succession then waiting four hours until
  41. he eats supper.  Another interesting point is the fact that both
  42. eat every meal at the restaurant with no concern for how long they
  43. have to wait to be seated and eat.  It is of no concern either to
  44. the Ada program at hand or the analysis of the problem, that one
  45. of the tasks is never called, because this is perfectly legal.
  46. Finally, both the restaurant and the Burger Boy can only serve one
  47. customer at a time, since this is the way the tasks were
  48. programmed.  They can however, each serve a different customer at
  49. the same time.
  50.  
  51.  
  52. EATING MEALS IS VERY SLOW
  53. _________________________________________________________________
  54.  
  55. You will notice, by inspecting the delays, that John waits 0.4
  56. hours and gets to the restaurant first, then takes a total of one
  57.  
  58.                                                         Page 26-1
  59.  
  60.                           Chapter 26 - The Conditional Rendezvous
  61.  
  62. hour to eat.  Bill waits one hour, and goes to the restaurant, but
  63. must wait on the entry queue for 0.4 hours for John to finish
  64. eating, then takes another hour to eat.  During this hour, John has
  65. returned and is waiting to be served again.  They continue getting
  66. in each others way and finally each consumes three meals.
  67.  
  68.  
  69. DEADLOCK OCCURS
  70. _________________________________________________________________
  71.  
  72. The observant student will notice that the tasks named Bills_Day
  73. and Johns_Day each run through sequentially to completion and make
  74. no additional calls.  The two named Restaurant and Burger_Boy
  75. however, each consist of an infinite loop and never actually
  76. complete.  When the first two tasks execute to completion, the
  77. system will recognize that there are no tasks running that are
  78. capable of making calls to the waiting accept statements.  The
  79. system will recognize this condition, and it will terminate the
  80. program due to deadlock after displaying a message that deadlock
  81. has occurred.
  82.  
  83. We still have not given an acceptable method of terminating our
  84. program gracefully, but we will later in this chapter.  You should
  85. spend the time necessary to thoroughly understand this program
  86. since it will be the basis for the remaining example programs in
  87. this chapter.  When you do understand it, compile and execute it
  88. to see if your compiler will recognize the deadlock condition.
  89.  
  90.  
  91.  
  92. THE CONDITIONAL RENDEZVOUS
  93. _________________________________________________________________
  94.  
  95. Examine the program named MEALS2.ADA for our     ================
  96. first example of a conditional rendezvous.  This    MEALS2.ADA
  97. program is identical to the last one named       ================
  98. MEALS1.ADA except for the addition of a few
  99. statements in the task named Bills_Day.  In this
  100. program Bill is in a bit of a hurry to eat his first two meals and
  101. puts in a few conditions to indicate this.
  102.  
  103.  
  104. THE SELECTED RENDEZVOUS
  105. _________________________________________________________________
  106.  
  107. When Bill starts his day, he is very impatient and will not wait
  108. at all for his first meal.  If the restaurant is not ready to serve
  109. a meal immediately, he will go to the Burger Boy and have his
  110. breakfast.  The statements in lines 29 through 33 replace the
  111. single statement in line 29 of the last program to indicate this
  112. impatience.  In Ada terms, if the task named Restaurant is not
  113. waiting at the Eat_A_Meal entry point, the call will not be made
  114. there, but a call will be made to the entry point named Eat_A_Meal
  115. in the Burger_Boy task.  If the Burger_Boy task is not waiting at
  116.  
  117.                                                         Page 26-2
  118.  
  119.                           Chapter 26 - The Conditional Rendezvous
  120.  
  121. the entry point, then Bill will be required to wait until it is
  122. ready, when he will be served.
  123.  
  124. The reserved word else is used to indicate the selected rendezvous
  125. and it can follow as many or clauses as desired.  The general form
  126. is;
  127.  
  128.      select
  129.         <entry call>;
  130.      or
  131.         <entry call>;
  132.      else
  133.         <entry call>;
  134.      end select;
  135.  
  136. The else clause will be taken if none of the other entry points are
  137. waiting at their respective rendezvous points.
  138.  
  139.  
  140. THE DELAYED RENDEZVOUS
  141. _________________________________________________________________
  142.  
  143. Bill is not quite as hungry for his second meal so he is willing
  144. to wait for a short period at the restaurant, but if he is not
  145. served within one tenth of an hour, he will go to the Burger Boy
  146. for lunch.  The single statement in lines 35 through 40 replaces
  147. the statement in line 31 of the previous program.  In Ada terms,
  148. the task named Bills_Day will wait .1 hour for the Restaurant task
  149. to reach its entry point, after which it will call the entry point
  150. of Burger_Boy and wait no matter how long it takes to be served
  151. there.
  152.  
  153. As many selections as desired can be used, with delay statements
  154. in as many of the selections as needed.  The general form is given
  155. by;
  156.  
  157.      select
  158.         delay <time>;
  159.         <entry call>;
  160.      or
  161.         delay <time>;
  162.         <entry call>;
  163.      or
  164.         delay <time>;
  165.         <entry call>;
  166.      end select;
  167.  
  168. An else clause cannot be used if any delay statements are used
  169. because the else clause will be executed immediately and any
  170. statement with a delay would never have a chance to time out.  It
  171. would therefore never be executed and must be regarded as
  172. unexecutable code.
  173.  
  174.  
  175.  
  176.                                                         Page 26-3
  177.  
  178.                           Chapter 26 - The Conditional Rendezvous
  179.  
  180. BILL'S STUBBORN RESOLUTION FOR SUPPER
  181. _________________________________________________________________
  182.  
  183. Bill has decided that under no conditions will he eat supper at the
  184. Burger Boy.  He will wait for as long as he has to in order to be
  185. served at the restaurant, and this is reflected in the single call
  186. in line 42 in exactly the same manner that was indicated in the
  187. last program.
  188.  
  189. Take careful notice that both of these select statements are in the
  190. calling task, not the called task, but we will see shortly that the
  191. same sort of things are available in the called task.
  192.  
  193. Examination of the result of execution will reveal that Bill did
  194. eat both of his early meals at the Burger Boy.  In addition, you
  195. will see that his second meal was ordered and eaten at the Burger
  196. Boy during the time that John was eating his meal at the
  197. Restaurant.  This indicates that resources are being well used.
  198.  
  199. Be sure to compile and execute this program.  Observe the result
  200. of execution to see that it does send Bill to the Burger Boy on
  201. occasion.  Because we still have no graceful termination, deadlock
  202. occurs once again.
  203.  
  204.  
  205. THE DELAYED ENTRY POINT
  206. _________________________________________________________________
  207.  
  208. Examine the program named MEALS3.ADA for yet     ================
  209. another addition to our example of Bill and John    MEALS3.ADA
  210. satisfying their desire for nourishment.  In     ================
  211. this case, the restaurant will not remain open
  212. forever, nor will the Burger Boy, both choosing
  213. to close for the day if there are no customers for a period of
  214. time.
  215.  
  216. The select statement in lines 59 through 72 contains two
  217. alternatives, one to handle a customer as usual, and another with
  218. a delay of 1.5 hours followed by a restaurant closing statement.
  219. If there are no entry calls to the entry named Eat_A_Meal for 1.5
  220. seconds (since HOURS is actually one second), then the other branch
  221. of the select statement will be executed, resulting in the display
  222. of a message and execution of an exit statement.  You will recall
  223. that an exit will take you out of the most immediate loop and
  224. continue execution of statements from that point.  In the case of
  225. the Restaurant task, the task is therefore completed and waits for
  226. the other tasks to complete.
  227.  
  228. The task named Burger_Boy has the same alternative entry point,
  229. except for a delay of 2.1 seconds.  It also reaches its end
  230. statement and waits for the others to finish.  Examination of the
  231. result of execution will reveal that both eating establishments
  232. actually did reach timeout and closed for the day leaving John to
  233. go hungry since he never ate his third meal.  Since John did not
  234.  
  235.                                                         Page 26-4
  236.  
  237.                           Chapter 26 - The Conditional Rendezvous
  238.  
  239. eat his third meal of the day, some code was not executed and it
  240. was not reported as such.  This seems to be an error, but it is
  241. actually not, because it is a tasking error that is not propagated
  242. to the main program.  This is according to the definition of Ada
  243. and is explained more fully in the last paragraph of this chapter.
  244.  
  245. Be sure to compile and run this program and observe the early
  246. closing of the eating establishments.
  247.  
  248.  
  249. ORDERLY TERMINATION OF TASKS
  250. _________________________________________________________________
  251.  
  252. Examine the program named TERMINAT.ADA for an    ================
  253. example of orderly termination of tasks.  This     TERMINAT.ADA
  254. program is identical to the first program in     ================
  255. this chapter named MEALS1.ADA except for two
  256. minor changes.  The entry point for the task
  257. named Restaurant has been put inside of a select statement in a
  258. manner similar to that done in MEALS2.ADA or MEALS3.ADA but a
  259. different "or" clause is used, one that contains the reserved word
  260. terminate.  Each time through the loop, the entry point named
  261. Eat_A_Meal can be selected for execution and the task execution
  262. will wait until this entry point is called.  While the program is
  263. waiting for the entry call, the branch of the select with the
  264. terminate allows the possibility of a termination to occur.
  265. However, the termination can only occur under the right conditions.
  266. The right conditions require that all other concurrent tasks,
  267. including the parent task, are either at their final end waiting
  268. to terminate, or have a similar terminate alternative to select.
  269.  
  270. The task named Burger_Boy has a similar terminate alternative.
  271. When the two calling tasks reach their final end, the two serving
  272. tasks have terminate alternatives available, so there is an orderly
  273. termination and the program returns to the operating system.  Be
  274. sure to compile and execute this program, then study the result for
  275. conformance to this definition.
  276.  
  277.  
  278.  
  279. THE abort STATEMENT
  280. _________________________________________________________________
  281.  
  282. The abort statement will unconditionally abort any tasks with no
  283. regard to their present operating condition.  It is extremely
  284. abrupt and does not provide for an orderly termination.  It should
  285. therefore be used only in conditions of an emergency nature.
  286. Possibly the detection of loss of power would be such a
  287. circumstance.  The tasks to be aborted are listed following the
  288. reserved word abort in an executable statement anywhere that it is
  289. legal to use an executable statement.
  290.  
  291.  
  292.  
  293.  
  294.                                                         Page 26-5
  295.  
  296.                           Chapter 26 - The Conditional Rendezvous
  297.  
  298. TWO TASK ATTRIBUTES
  299. _________________________________________________________________
  300.  
  301. There are two useful task attributes that can be used to find the
  302. condition of any given task.  They are CALLABLE, and TERMINATED,
  303. and each returns a BOOLEAN type indication of the current status
  304. of the task to which the attribute is appended.  See the LRM for
  305. details on the use of these two attributes.
  306.  
  307.  
  308. TASK EXCEPTIONS
  309. _________________________________________________________________
  310.  
  311. The tasking exception named Tasking_Error is raised during
  312. essentially any communication error.  Any error during task
  313. elaboration will raise this exception (this will be illustrated in
  314. a later example program).  This exception is raised in the caller
  315. if the called task is aborted, and is raised in the originators of
  316. all calls on an entry queue of an aborted task.
  317.  
  318. If an exception is raised during a rendezvous, it is propagated
  319. into both tasks since each may need some form of recovery.  If the
  320. exception is not handled internally by the task, it is not
  321. propagated to the main program because to do so would be very
  322. disruptive.  This means that a program that appears to be working
  323. correctly could actually have a very disruptive tasking error, much
  324. like the example program named MEALS3.ADA in this chapter.  Each
  325. significant task should therefore have an exception handler to
  326. guard against the occurrence of an unknown exception.
  327.  
  328.  
  329. PROGRAMMING EXERCISE
  330. _________________________________________________________________
  331.  
  332. 1.   Modify the program named MEALS2.ADA such that it has an
  333.      orderly termination upon completion.
  334.  
  335. 2.   Modify MEALS2.ADA even further such that John eats every meal
  336.      at the Burger Boy unless he has to wait there, in which case
  337.      he eats at the restaurant.
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.                                                         Page 26-6