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

  1.  
  2.  
  3.  
  4.                                                        Chapter 31
  5.                                             MORE EXAMPLE PROGRAMS
  6.  
  7.  
  8. RANDOM NUMBERS
  9. _________________________________________________________________
  10.  
  11. Occasionally when writing a computer program,    ================
  12. you will have a need to use a random number         RANDOM.ADA
  13. generator.  Very few compilers have a random     ================
  14. number generator as a part of the system, so it
  15. is necessary for you to either write one
  16. yourself or find one that has been written and debugged by someone
  17. else.  The example package named RANDOM.ADA is presented to you for
  18. several reasons.  The first reason is to provide you with a useful
  19. example of a complete generic package to serve as an illustration
  20. of how to write one.  Secondly, it is a useful package that you can
  21. use as a part of your own programs when you have a need to use a
  22. random number generator.  Finally, it is an illustration of good
  23. formatting style and it illustrates the inclusion of enough
  24. comments in the package specification to completely define the
  25. method used, and how to utilize this package as part of another
  26. program.
  27.  
  28. No other comments need to be given about the operation or use of
  29. this package, so you will be left on your own to study the listing
  30. then compile this package in preparation for use with the next
  31. example program.
  32.  
  33.  
  34.  
  35. TESTING THE RANDOM NUMBERS
  36. _________________________________________________________________
  37.  
  38. Examine the program named TESTRAN.ADA which was   ===============
  39. written solely to test the random number            TESTRAN.ADA
  40. generator in the package named Random.  It        ===============
  41. instantiates a copy of the generic package using
  42. the type FLOAT in line 7, then declares a few
  43. objects and an array type.  In the executable part of the program
  44. the random number generator is initialized with the Set_Seed
  45. procedure in line 21, and 12 random numbers are read and printed
  46. to see that they do cover the range of 0.0 to 1.0 as defined in the
  47. header of the package named Random.
  48.  
  49. The real test of the random number generator is in the loop
  50. beginning in line 35 where ten thousand random numbers are
  51. generated and converted into integer type values by multiplying by
  52. 100.  The integer values will therefore cover a range of 1 to 100,
  53. and they are counted in the array named Events.  The count in each
  54. element of the array should be about 100 since there are ten
  55. thousand cases distributed over 100 elements.  Execution of the
  56. program will reveal that the count in each array element is about
  57.  
  58.                                                         Page 31-1
  59.  
  60.                                Chapter 31 - More Example Programs
  61.  
  62. 100 as expected, so we declare the random number generator to be
  63. at least reasonably random.  A mathematician may decide that this
  64. method is too crude to be called a good random number generator,
  65. but for our purposes it is good enough.
  66.  
  67. Compile and execute this program, and you will find that each time
  68. you run it, you should get different results because it uses the
  69. system clock to set the seed, resulting in a new starting seed for
  70. each execution.
  71.  
  72.  
  73.  
  74. A NEW DYNAMIC STRING PACKAGE
  75. _________________________________________________________________
  76.  
  77. Examine the program named DYNSTRNG.ADA, which    ================
  78. is included in part 2 of this tutorial for a       DYNSTRNG.ADA
  79. better dynamic string package.  You will recall  ================
  80. that when we studied the dynamic string package
  81. in chapter 16 of part 1 of this tutorial, we
  82. found a problem when using string constants in the Copy procedure
  83. calls.  This was because the system found ambiguous procedures.
  84. It could not tell if the string constant was of type STRING or of
  85. our own declared type which we named DYNAMIC_STRING.  Since we had
  86. not studied the discriminated record at that time, we could not
  87. properly fix the problem.  The new DynStrng package, using a
  88. discriminated record, is offered as a better package for the
  89. problem of using dynamic strings.
  90.  
  91. The DYNAMIC_STRING type is declared in lines 32 through 36 and is
  92. declared as a record this time so there is no confusion as to
  93. whether it is a string or a record, and the overloading ambiguity
  94. problem is gone.  The package specification is essentially
  95. unchanged from the last dynamic string package, except for the type
  96. of course, but the body is changed considerably to reflect the new
  97. data structure.  You will be left on your own to compare the bodies
  98. of these two packages if you so desire.
  99.  
  100.  
  101.  
  102. THE STRING CONSTANT PROBLEM IS FIXED
  103. _________________________________________________________________
  104.  
  105. The program named TESTSTRN.ADA is designed to    ================
  106. test the new package with a few string constants   TESTSTRN.ADA
  107. to prove that it really does work as advertised. ================
  108. You can compile and execute this file to see
  109. that it really does work with string constants
  110. in a Copy procedure call.
  111.  
  112. You can return to the program named TRYSTRNG.ADA from chapter 16
  113. to prove that the new package still works with this old program.
  114. You will find that a couple of changes must be made to reflect the
  115. different data type.  Lines 11 and 12 must be modified to reflect
  116.  
  117.                                                         Page 31-2
  118.  
  119.                                Chapter 31 - More Example Programs
  120.  
  121. only the upper limit on the static length of the ================
  122. dynamic string variables.  They will read as       TRYSTRNG.ADA
  123. follows;                                         ================
  124.  
  125.      Name    : DYNAMIC_STRING(15);
  126.      Stuff   : DYNAMIC_STRING(35);
  127.  
  128. In addition, because the type is changed, lines 21 and 22 must also
  129. be modified as follows;
  130.  
  131.      Name.Dynamic_Length := 3;
  132.      Stuff.Dynamic_Length := 7;
  133.  
  134. After making these two changes, this program should execute exactly
  135. as it did when it used the old dynamic string package.
  136.  
  137.  
  138.  
  139. HOW OLD ARE YOU IN DAYS
  140. _________________________________________________________________
  141.  
  142. This program is a repeat of the program given in ================
  143. chapter 16, but it is improved somewhat here.        AGE2.ADA
  144. Since we now know how to use the Calendar        ================
  145. package, we can use it to get today's date for
  146. us, and we do this in the new program named
  147. AGE2.ADA.  Notice especially the way the data is read in and
  148. checked for validity before continuing on.  If the data were read
  149. into the corresponding variables, an invalid entry would cause an
  150. exception, but since the data is read into an INTEGER type
  151. variable, it can be checked for validity before being assigned to
  152. the correct variable.  The program should be very simple for you
  153. to understand, but it would be good for you to spend a little time
  154. studying it before compiling and executing it.
  155.  
  156.  
  157.  
  158.  
  159. THE DINING PHILOSOPHERS
  160. _________________________________________________________________
  161.  
  162. Most books and articles on tasking or            ================
  163. concurrency at least mention the problem of the     PHILOS.ADA
  164. dining philosophers, so it would not be good to  ================
  165. leave this tutorial without a little discussion
  166. of this problem.  In fact, the program named
  167. PHILOS.ADA is a program you can study and execute to see this
  168. problem illustrated.
  169.  
  170. The problem is stated that five philosophers sit down to eat.  They
  171. like to eat for awhile then think for awhile and repeat the pattern
  172. forever.  In order to eat, they require a fork in both their left
  173. and their right hands, and the table is set with a fork on each
  174. side of their plates.  The problem occurs when we state that there
  175.  
  176.                                                         Page 31-3
  177.  
  178.                                Chapter 31 - More Example Programs
  179.  
  180. is only one fork between each adjacent philosopher, and he is
  181. therefore required to share each fork with his adjacent colleague.
  182.  
  183. Each philosopher sits down, waits a random length of time, then
  184. picks up the fork on his left, waits another random length of time,
  185. and picks up the fork on his right.  He then proceeds to eat for
  186. a random length of time, then returns both forks to the table and
  187. thinks for a random length of time.  Once he picks up the left
  188. fork, he stubbornly hangs on to it until he gets the right fork.
  189. If we ever reach the condition where each philosopher has his left
  190. fork, then none will ever return it and none can ever therefore
  191. pick up his right fork.  The entire system is said to be deadlocked
  192. because nothing else will ever be accomplished.  All five of the
  193. uncooperative philosophers will eventually starve since none can
  194. eat.
  195.  
  196.  
  197.  
  198. THE PROGRAM ILLUSTRATES DEADLOCK
  199. _________________________________________________________________
  200.  
  201. The program uses a package to define a task type for one
  202. philosopher with the required delays and the logic to acquire each
  203. fork, eat, then return the forks to the table.  Following our study
  204. of tasking, you should have no problem understanding the logic
  205. presented here.
  206.  
  207. The main program named Philos, which starts in line 91 of the file
  208. simply declares the five philosophers, starts them through their
  209. cycles in lines 106 through 110, then loops while it is watching
  210. for deadlock.  When deadlock is detected, a message is output to
  211. the monitor, and the entire system is aborted.
  212.  
  213. Compile and execute this program, so you can observe deadlock
  214. occurring and the system aborting operation.  If you run it several
  215. times, you will see that quite often deadlock occurs immediately,
  216. but many times it will run for several seconds before deadlock is
  217. detected.
  218.  
  219. This is an interesting problem, but the more interesting point is
  220. the fact that this program, which begins in line 91 uses Text_IO
  221. and One_Man, and One_Man in turn uses Text_IO, Calendar, and
  222. Random.  This program uses quite a bit of the resources available
  223. in Ada, and uses several packages to accomplish its intended
  224. mission.  Since we are using Random, which was developed for an
  225. earlier project, we are actually illustrating the reusability of
  226. Ada software.
  227.  
  228.  
  229. THE GENERIC STACK
  230. _________________________________________________________________
  231.  
  232. We promised we would include a generic stack when we studied the
  233. character stack in chapter 16, and GENSTACK.ADA is a generic stack
  234.  
  235.                                                         Page 31-4
  236.  
  237.                                Chapter 31 - More Example Programs
  238.  
  239. to fulfill that promise.  It is really only a    ================
  240. copy of CHARSTAK.ADA from chapter 16 made into     GENSTACK.ADA
  241. a generic package according to the rules studied ================
  242. in this tutorial.
  243.  
  244. The program TRYSTAK.ADA is nearly identical to    ===============
  245. the program of the same name from chapter 16,       TRYSTAK.ADA
  246. the only difference being the instantiation of    ===============
  247. the generic package so it can be used in the
  248. main program.
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.                                                         Page 31-5