home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / BD3.ZIP / PRINT.ME < prev    next >
Encoding:
Text File  |  1989-04-30  |  72.4 KB  |  1,696 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.                                               BANK DEMO 3BANK DEMO 3
  17.  
  18.                                    A Demonstration of Tasking in AdaA Demonstration of Tasking in Ada
  19.  
  20.  
  21.  
  22.  
  23.                                       Version 1.0, 11 March 1989
  24.  
  25.                                                   by
  26.                                              Richard Conn
  27.  
  28.  
  29.  
  30.  
  31.  
  32.                  Bank  Demo 3, or BD3 as the files and mainline procedure are named, is  a
  33.             demonstration program which illustrates how  Ada and its tasking mechanism can
  34.             be used to create a model of an activity in the real world.  BD3 is a model of
  35.             a bank, where the bank contains several tellers, implemented as Ada tasks, and
  36.             is used by  customers, also implemented as Ada tasks.  The user at the console
  37.             can monitor the status of the bank and the accounts of its customers and cause
  38.             more customer tasks to execute, thereby increasing the level  of  activity  at
  39.             the bank. The tasks all operate in parallel,  so  a  variety  of events can be
  40.             thought of as happening at the same time, controlled by the Ada runtime system
  41.             which is built into the Ada program by the Ada compilation system.
  42.  
  43.                  This  document  and  the   associated   software   are  presented  as  an
  44.             introduction  to the Ada language and its tasking  capabilities.    While  the
  45.             author attempted to make this  document  as  easy  to  follow  as  possible, a
  46.             limited knowledge of Ada is  assumed.  Variable assignments, type definitions,
  47.             and procedure and package specifications are presented in  correct  Ada syntax
  48.             without much explanation.  If  this  proves  to  be  a  problem,  the ADA-TUTR
  49.             shareware program will help a  lot  in  bringing  the  user up to date with an
  50.             understanding of Ada syntax.  Regardless, the program in the file  BD3.EXE can
  51.             be  run without any knowledge of Ada at all, and the tasking demonstration can
  52.             be observed.
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.                                               BANK DEMO 3
  61.                                    A Demonstration of Tasking in Ada
  62.  
  63.                                             by Richard Conn
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.             1  Introduction
  71.             1  Introduction
  72.  
  73.  
  74.                  BD3 is a model of a bank.   The  Ada  package  called BANK represents the
  75.             bank  itself, and the bank contains four tellers,  represented  by  an  array,
  76.             named  TELLER, of four tasks.  BD3 is written in an object-oriented fashion in
  77.             the Ada programming language.
  78.  
  79.                 ======================================================================
  80.  
  81.               ADD_NEW_CUSTOMER
  82.                 -- to create a new account for a customer and return
  83.                 -- an ID for that customer
  84.               GET_BALANCE
  85.                 -- to return the balance of a customer's account
  86.               MAKE_DEPOSIT
  87.                 -- to deposit money into a customer's account
  88.               MAKE_WITHDRAWAL
  89.                 -- to withdraw money from a customer's account
  90.  
  91.                   Figure 1: The bank tellers can process four kinds of transactions.
  92.                   Figure 1
  93.  
  94.                 ======================================================================
  95.  
  96.                  Each  task,  TELLER(1)  to TELLER(4), supports one  entry  point,  called
  97.             REQUEST, which is used by tasks outside the BANK package to communicate with a
  98.             TELLER(n)  task  and  request  a  transaction.  Four transaction requests  are
  99.             recognized by a TELLER(n) task, as shown in Figure 1.
  100.  
  101.  
  102.  
  103.                                 BD3 - A Demonstration of Tasking in Ada
  104.  
  105.  
  106.  
  107.                 ======================================================================
  108.  
  109.               MY_ID : BANK.CUSTOMER_ID;
  110.                 -- MY_ID is a variable which holds the customer ID of
  111.                 -- this task (type CUSTOMER_ID is defined in the package BANK)
  112.               AMOUNT : BANK.DOLLAR;
  113.                 -- type DOLLAR represents the unit of currency, as defined
  114.                 -- by the package BANK, and the variable AMOUNT is used to
  115.                 -- hold the money passed in to or out of the TELLER(n) task
  116.                  ...
  117.               BANK.TELLER(1).REQUEST (MY_ID, BANK.ADD_NEW_CUSTOMER, AMOUNT);
  118.                 -- this calls the REQUEST entry point for the task TELLER(1)
  119.                 -- in the package BANK; the ID of the customer is passed
  120.                 -- out of the TELLER(1) task in the variable MY_ID and the
  121.                 -- requested transaction is ADD_NEW_CUSTOMER, which is
  122.                 -- defined in the package BANK; the variable AMOUNT is
  123.                 -- passed as a parameter but not used
  124.               AMOUNT := 100.00;
  125.               BANK.TELLER(2).REQUEST (MY_ID, BANK.MAKE_DEPOSIT, AMOUNT);
  126.                 -- the variable AMOUNT is set to $100, and the REQUEST
  127.                 -- entry point of TELLER(2) in the package BANK is
  128.                 -- called; the variable MY_ID, which was set by the call
  129.                 -- to TELLER(1) above, is used to identify the customer,
  130.                 -- and the requested action is MAKE_DEPOSIT, as defined by
  131.                 -- the package BANK; AMOUNT is passed to the TELLER(2) task
  132.                 -- as the amount to deposit
  133.  
  134.             Figure 2: This code segment shows that the TELLER(n) tasks are accessed like procedures.
  135.             Figure 2
  136.  
  137.                 ======================================================================
  138.  
  139.                  Each TELLER(n) task performs one of these four transactions  at  a  time.
  140.             An  external  task  calls  (makes  a  rendezvous  with,  in Ada terminology) a
  141.             TELLER(n)  task  through  its  REQUEST  entry  point,  passing  one  of  these
  142.             transaction  names  as  a parameter, and  the  TELLER(n)  task  processes  the
  143.             request.  For example, a code segment in an external  task  may look something
  144.             like Figure 2.   This code segment was extracted from the body of the CUSTOMER
  145.             task definition in the package CUSTOMER_WORLD (see later).
  146.                  This demonstration program  also  contains  several  other  tasks, called
  147.             customer  tasks.    These  tasks,  hidden inside the  package  CUSTOMER_WORLD,
  148.             operate  independently  and  interact with the  various  teller  tasks.    The
  149.             customer  tasks  make requests to the teller tasks, asking to be  added  as  a
  150.             customer of the bank, making deposits, and making withdrawals.  Using  the Ada
  151.             inter-task rendezvous mechanism to communicate,  if the teller task is already
  152.             servicing another customer task,  the  customer  task  "waits in line" (having
  153.             been placed in the  implicit  queue associated with the teller's entry point).
  154.             If queued, the customer task is serviced after the tasks in the teller's queue
  155.             in front of him have  been serviced.  Indeed, the model is similar to the real
  156.             world.
  157.                  From  the  mainline procedure, the user can  monitor  the  bank  and  its
  158.             activity.   He  can  obtain  a status report from the bank (which includes the
  159.             balanace of each  customer,  the  number  of  transactions  requested  by each
  160.             customer, and the number of transactions processed by each teller),  cause new
  161.             customer tasks  to  start  up,  and shut down the system and exit the program.
  162.  
  163.  
  164.  
  165.                                                 - 2 -
  166.  
  167.  
  168.  
  169.                                 BD3 - A Demonstration of Tasking in Ada
  170.  
  171.  
  172.  
  173.             For those Ada compilers which  do  not  allow  tasking  to  proceed  while the
  174.             current task is waiting for input (such as many of the Ada compilers available
  175.             for the IBM PC), a continuous  display  function  is also provided to the user
  176.             which  displays  the  bank's status, delays five seconds (allowing the various
  177.             tasks to run),  and  displays  the  bank's  status again.  This command option
  178.             displays ten status reports before returning the user to his prompt.
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.             2  Execution of the Program
  186.             2  Execution of the Program
  187.  
  188.  
  189.                  Figures 3 to 9 show various displays during the execution of the program.
  190.             These  displays were generated on a SUN workstation,  where  the  program  was
  191.             compiled by the Verdix Ada compiler.  These  displays  may  be  different from
  192.             what you see if you compile the program  on  an  IBM  PC, particularly in that
  193.             Verdix Ada does not suspend all  tasks  while  the current task is waiting for
  194.             input from the console.   While  we  are  at  the prompt in these figures, all
  195.             tasks are running in parallel with our input activity.  On some compilers this
  196.             will not be the case, and the parallel operation of the tasks will not be seen
  197.             until the user issues the 'c' (continuous bank status) command.
  198.  
  199.                 ======================================================================
  200.  
  201.             Enter
  202.               b for bank status
  203.               c for continuous bank status
  204.               s for start next customer
  205.               x for exit: b
  206.             The bank doesn't have any customers
  207.  
  208.                           Figure 3: When BD3 starts, there are no customers.
  209.                           Figure 3
  210.  
  211.                 ======================================================================
  212.  
  213.                  Figure 3 shows the menu that  is  presented  to the user when the program
  214.             starts and that no customer tasks are running yet.
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.                                                 - 3 -
  233.  
  234.  
  235.  
  236.                                 BD3 - A Demonstration of Tasking in Ada
  237.  
  238.  
  239.  
  240.                 ======================================================================
  241.  
  242.             Enter
  243.               b for bank status
  244.               c for continuous bank status
  245.               s for start next customer
  246.               x for exit: s
  247.             Enter
  248.               b for bank status
  249.               c for continuous bank status
  250.               s for start next customer
  251.               x for exit: b
  252.             **** BANK STATUS REPORT ****
  253.             Customer      Balance  Transactions  Attempted_Overdraws
  254.                 1          100.00             2                    0
  255.  
  256.             Teller           :        1       2       3       4
  257.             Transaction_Count:        0       1       1       0
  258.  
  259.                         Figure 4: The 's' command starts a new customer task.
  260.                         Figure 4
  261.  
  262.                 ======================================================================
  263.  
  264.                  Figure 4 shows the start of the first customer task  via  the 's' command
  265.             followed immediately by a status report of the  bank  (requested  via  the 'b'
  266.             command).   The task we just started has had enough time between the start  of
  267.             the  task and the request of the status display to perform 2 transactions, and
  268.             we can see that tellers 2 and 3 were selected to perform these transactions.
  269.  
  270.                 ======================================================================
  271.  
  272.             Enter
  273.               b for bank status
  274.               c for continuous bank status
  275.               s for start next customer
  276.               x for exit: b
  277.             **** BANK STATUS REPORT ****
  278.             Customer      Balance  Transactions  Attempted_Overdraws
  279.                 1           95.26             3                    0
  280.  
  281.             Teller           :        1       2       3       4
  282.             Transaction_Count:        0       2       1       0
  283.  
  284.             Figure 5: Another status display shows just a little activity by the customer.
  285.             Figure 5
  286.  
  287.                 ======================================================================
  288.  
  289.                  Figure 5 shows another bank status  command,  which was issued as soon as
  290.             the display above completed.  During  this  brief amount of time, the customer
  291.             has had time to perform another transaction.
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.                                                 - 4 -
  300.  
  301.  
  302.  
  303.                                 BD3 - A Demonstration of Tasking in Ada
  304.  
  305.  
  306.  
  307.                 ======================================================================
  308.  
  309.             Enter
  310.               b for bank status
  311.               c for continuous bank status
  312.               s for start next customer
  313.               x for exit: s
  314.             Enter
  315.               b for bank status
  316.               c for continuous bank status
  317.               s for start next customer
  318.               x for exit: s
  319.             Enter
  320.               b for bank status
  321.               c for continuous bank status
  322.               s for start next customer
  323.               x for exit: s
  324.             Enter
  325.               b for bank status
  326.               c for continuous bank status
  327.               s for start next customer
  328.               x for exit: s
  329.             Enter
  330.               b for bank status
  331.               c for continuous bank status
  332.               s for start next customer
  333.               x for exit: s
  334.             Enter
  335.               b for bank status
  336.               c for continuous bank status
  337.               s for start next customer
  338.               x for exit: b
  339.             **** BANK STATUS REPORT ****
  340.             Customer      Balance  Transactions  Attempted_Overdraws
  341.                 1          100.59             5                    0
  342.                 2          123.13             4                    0
  343.                 3          117.75             3                    0
  344.                 4          114.97             3                    0
  345.                 5          100.00             2                    0
  346.                 6           38.86             4                    0
  347.  
  348.             Teller           :        1       2       3       4
  349.             Transaction_Count:        5       7       7       2
  350.  
  351.                               Figure 6: Five more tasks are started up.
  352.                               Figure 6
  353.  
  354.                 ======================================================================
  355.  
  356.                  Five more customer  tasks  are  started  up  in Figure 6 and another bank
  357.             status  display  is requested.  The fact that we have many  tasks  running  in
  358.             parallel now should become evident as we see more and  more  activity going on
  359.             between each of our status displays.
  360.  
  361.  
  362.  
  363.  
  364.  
  365.                                                 - 5 -
  366.  
  367.  
  368.  
  369.                                 BD3 - A Demonstration of Tasking in Ada
  370.  
  371.  
  372.  
  373.                 ======================================================================
  374.  
  375.             Enter
  376.               b for bank status
  377.               c for continuous bank status
  378.               s for start next customer
  379.               x for exit: c
  380.             **** BANK STATUS REPORT ****
  381.             Customer      Balance  Transactions  Attempted_Overdraws
  382.                 1          134.37             7                    0
  383.                 2          131.77             5                    0
  384.                 3          202.01             5                    0
  385.                 4          129.76             4                    0
  386.                 5          100.00             2                    0
  387.                 6           57.83             5                    0
  388.  
  389.             Teller           :        1       2       3       4
  390.             Transaction_Count:        9       9       7       3
  391.  
  392.                  ...
  393.  
  394.             **** BANK STATUS REPORT ****
  395.             Customer      Balance  Transactions  Attempted_Overdraws
  396.                 1           73.44            29                    3
  397.                 2          206.04            24                    0
  398.                 3          165.82            27                    0
  399.                 4           58.48            24                    3
  400.                 5           71.03            22                    0
  401.                 6           41.02            27                    2
  402.  
  403.             Teller           :        1       2       3       4
  404.             Transaction_Count:       60      36      38      19
  405.  
  406.             Figure 7: A continous bank status display shows a lot of activity over a short time.
  407.             Figure 7
  408.  
  409.                 ======================================================================
  410.  
  411.                  Figure 7 shows  a  'c'  (continue  bank status) command and its resulting
  412.             display.  To save space, I have shown only the  first  and  the  last  of  the
  413.             eleven bank status report displays generated by this command.
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.                                                 - 6 -
  432.  
  433.  
  434.  
  435.                                 BD3 - A Demonstration of Tasking in Ada
  436.  
  437.  
  438.  
  439.                 ======================================================================
  440.  
  441.             Enter
  442.               b for bank status
  443.               c for continuous bank status
  444.               s for start next customer
  445.               x for exit: s
  446.             Enter
  447.               b for bank status
  448.               c for continuous bank status
  449.               s for start next customer
  450.               x for exit: s
  451.             Enter
  452.               b for bank status
  453.               c for continuous bank status
  454.               s for start next customer
  455.               x for exit: b
  456.             **** BANK STATUS REPORT ****
  457.             Customer      Balance  Transactions  Attempted_Overdraws
  458.                 1          108.96            31                    3
  459.                 2          226.90            26                    0
  460.                 3          106.79            30                    0
  461.                 4           87.86            26                    3
  462.                 5           57.83            24                    0
  463.                 6           23.36            30                    3
  464.                 7           50.89             3                    0
  465.                 8          100.00             2                    0
  466.  
  467.             Teller           :        1       2       3       4
  468.             Transaction_Count:       68      42      42      21
  469.  
  470.                             Figure 8: Two more customer tasks are started.
  471.                             Figure 8
  472.  
  473.                 ======================================================================
  474.  
  475.                  In Figure 8, two more customer tasks are started and the result is shown.
  476.             There are now 8 customer tasks, 4 teller tasks, and the mainline procedure (as
  477.             another task) running in this Ada system.
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.                                                 - 7 -
  498.  
  499.  
  500.  
  501.                                 BD3 - A Demonstration of Tasking in Ada
  502.  
  503.  
  504.  
  505.                 ======================================================================
  506.  
  507.             Enter
  508.               b for bank status
  509.               c for continuous bank status
  510.               s for start next customer
  511.               x for exit: b
  512.             **** BANK STATUS REPORT ****
  513.             Customer      Balance  Transactions  Attempted_Overdraws
  514.                 1          127.88            34                    3
  515.                 2          232.11            28                    0
  516.                 3           56.17            33                    0
  517.                 4          110.24            28                    3
  518.                 5           81.60            28                    0
  519.                 6            4.68            32                    3
  520.                 7           31.60             6                    1
  521.                 8           54.42             4                    0
  522.  
  523.             Teller           :        1       2       3       4
  524.             Transaction_Count:       79      48      44      22
  525.             Enter
  526.               b for bank status
  527.               c for continuous bank status
  528.               s for start next customer
  529.               x for exit: b
  530.             **** BANK STATUS REPORT ****
  531.             Customer      Balance  Transactions  Attempted_Overdraws
  532.                 1           63.89            37                    3
  533.                 2          147.16            32                    0
  534.                 3           48.62            38                    0
  535.                 4          272.81            36                    3
  536.                 5           62.22            34                    0
  537.                 6           74.99            36                    4
  538.                 7           59.31            10                    1
  539.                 8           72.42             9                    0
  540.  
  541.             Teller           :        1       2       3       4
  542.             Transaction_Count:       94      59      56      24
  543.  
  544.                  Figure 9: Two more status reports are shown, each after some delay.
  545.                  Figure 9
  546.  
  547.                 ======================================================================
  548.  
  549.                  Figure 9 shows two more bank  status  displays.   The user of the program
  550.             has delayed for some time before issuing these commands so that  a significant
  551.             level of activity can be shown.
  552.                  More activity could  be  displayed,  and  more  customer  tasks  could be
  553.             started,  but the displays shown so far should be enough to give the reader  a
  554.             good feel  for  the  program's  operation.  When the user is finished, the 'x'
  555.             (for  exit)  command  will  terminate  all  tasks  and return the user to  the
  556.             operating system.
  557.                  The  displays  presented  so  far  were  generated  by the  BD3  mainline
  558.             procedure running on a UNIX system, having been  compiled  by  the  Verdix Ada
  559.             compiler.    BD3.EXE  provided in the  distribution  was  compiled  under  the
  560.  
  561.  
  562.  
  563.                                                 - 8 -
  564.  
  565.  
  566.  
  567.                                 BD3 - A Demonstration of Tasking in Ada
  568.  
  569.  
  570.  
  571.             IntegrAda environment.  The  reader  will see similar displays when BD3.EXE is
  572.             executed with the exception that no  other  tasks  will run when the prompt is
  573.             displayed.  In order for multitasking to be observed when running BD3.EXE, the
  574.                         __________________________________________________________________
  575.             user  must issue the 'c' (for continuous bank status) command.  Once the delay
  576.             ______________________________________________________________
  577.             statement in the mainline is encountered, the Ada runtime system is allowed to
  578.             come into play and other tasks are then permitted to run.
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.             3  Discussion of the Design
  586.             3  Discussion of the Design
  587.  
  588.  
  589.                  BD3  is an object-oriented design.  The objects  contained  in  this  Ada
  590.             system  are  a  console  device  (defined  by  the  Ada package CONSOLE) which
  591.             supports  input  and  output from and to the user's console terminal, a random
  592.             number  generator  (defined  by the Ada package RANDOM) which generates random
  593.             numbers of type  FLOAT  in the range from 0.0 to 1.0, the bank (defined by the
  594.             Ada package BANK) which contains the four TELLER(n) objects  (defined  by  the
  595.             Ada  task  type  TELLER_PERSON)  and  the  basic  type  definitions  (such  as
  596.             TRANSACTION, CUSTOMER_ID, and DOLLAR)  which  are  used  to communicate with a
  597.             TELLER(n)  object,  and the base of customers  (defined  by  the  Ada  package
  598.             CUSTOMER_WORLD) which interacts with the tellers in the bank.
  599.                  The specification to the package CONSOLE is shown in Figures 10,  11, and
  600.             12.  Each line is numbered for the convenience of the reader (of course, these
  601.             numbers do not appear in the files which are compiled by an Ada compiler).
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.                                                 - 9 -
  632.  
  633.  
  634.  
  635.                                 BD3 - A Demonstration of Tasking in Ada
  636.  
  637.  
  638.  
  639.                 ======================================================================
  640.  
  641.               1 package CONSOLE is
  642.               2 --------------------------------------------------------------------------
  643.               3 --| BEGIN PROLOGUE
  644.               4 --| DESCRIPTION            : CONSOLE is a package which implements an
  645.               5 --|                        : abstract state machine, a console terminal,
  646.               6 --|                        : that maps to the user's terminal.  CONSOLE
  647.               7 --|                        : provides routines to output characters,
  648.               8 --|                        : strings, integers, and floats (real numbers)
  649.               9 --|                        : to the user's terminal.  CONSOLE provides
  650.              10 --|                        : a routine to input a string of text from the
  651.              11 --|                        : user's terminal.  Finally, CONSOLE provides
  652.              12 --|                        : a function which can trim leading spaces
  653.              13 --|                        : from a string of text (useful in outputting
  654.              14 --|                        : text which was input by the read routine).
  655.              15 --|                        :
  656.              16 --| REQUIREMENTS SUPPORTED : A simple I/O package for Ada programs
  657.              17 --|                        :
  658.              18 --| LIMITATIONS            : Text input by CONSOLE.READ can be no more
  659.              19 --|                        : than 80 characters long.  Only objects of
  660.              20 --|                        : type CHARACTER, STRING, INTEGER, and FLOAT
  661.              21 --|                        : can be output.
  662.              22 --|                        :
  663.              23 --| AUTHOR(S)              : Richard Conn
  664.              24 --| CHANGE LOG             : 08/30/88  RLC  Initial Design and Code
  665.              25 --|                        :
  666.              26 --| REMARKS                : None
  667.              27 --|                        :
  668.              28 --| PORTABILITY ISSUES     : Uses TEXT_IO, so is very portable; no known
  669.              29 --|                        : portability problems.
  670.              30 --| END PROLOGUE
  671.              31 --------------------------------------------------------------------------
  672.              32
  673.  
  674.                        Figure 10: Specification of Package CONSOLE, Part 1 of 4
  675.                        Figure 10
  676.  
  677.                 ======================================================================
  678.  
  679.                  In Figure 10, the prologue in the specification of the package CONSOLE is
  680.             given.  Line 1 is the only piece of processed code; the rest of the  lines are
  681.             Ada comments which describe  the  package and give other information about it.
  682.             I developed this package some time ago for use in my Ada classes, feeling that
  683.             the package TEXT_IO (which is supplied with all Ada compilers) is  too complex
  684.             for  most  beginners  to work with.  Package CONSOLE starts  to  introduce  an
  685.             object-oriented design to Ada code design and is simple to use.
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.                                                 - 10 -
  698.  
  699.  
  700.  
  701.                                 BD3 - A Demonstration of Tasking in Ada
  702.  
  703.  
  704.  
  705.                 ======================================================================
  706.  
  707.              33     -- Special items to print
  708.              34     type    SPECIAL_ITEM is (NEW_LINE, TAB, BACKSPACE);
  709.              35
  710.              36     -- Type of string used by READ procedure
  711.              37     subtype OUTSTRING    is STRING(1 .. 80);
  712.              38
  713.              39     procedure WRITE(ITEM : in STRING);
  714.              40     procedure WRITE(ITEM : in CHARACTER);
  715.              41     -- Print strings and characters
  716.              42     -- Examples:
  717.              43     --  Ada procedure call                      Prints (without quotes)
  718.              44     --  ============================            =======================
  719.              45     --  CONSOLE.WRITE ("This is a test");       "This is a test"
  720.              46     --  CONSOLE.WRITE ('?');                    "?"
  721.              47
  722.              48     procedure WRITE(ITEM : in SPECIAL_ITEM);
  723.              49     -- Print special items
  724.              50     -- Example:
  725.              51     --  Ada procedure call                      Prints (without quotes)
  726.              52     --  ============================            =======================
  727.              53     --  CONSOLE.WRITE (CONSOLE.NEW_LINE);       <advances to next line>
  728.              54
  729.              55 --
  730.              56 -- Package CONSOLE
  731.              57
  732.              58     procedure WRITE(ITEM : in INTEGER; WIDTH : in NATURAL := 0);
  733.              59     -- Print integers
  734.              60     -- Examples:
  735.              61     --  Ada procedure call                      Prints (without quotes)
  736.              62     --  ============================            =======================
  737.              63     --  CONSOLE.WRITE (25);                     "25"
  738.              64     --  CONSOLE.WRITE (-3);                     "-3"
  739.              65     --  CONSOLE.WRITE (25, 5);                  "   25"
  740.              66
  741.  
  742.                        Figure 11: Specification of Package CONSOLE, Part 2 of 4
  743.                        Figure 11
  744.  
  745.                 ======================================================================
  746.  
  747.                  Figure 11 contains more of  the specification of package CONSOLE, showing
  748.             the  two  type definitions used by procedures  within  the  package  and  four
  749.             procedure  specifications,  all  for procedures named WRITE (see lines 39, 40,
  750.             48, and 58).  These procedures,  all  similarly  named, differ in the types of
  751.             arguments they accept, and Ada determines which procedure is reference  by the
  752.             types of  the  arguments  used  when  the  call to the procedure is made.  The
  753.             comment blocks in lines 41-46, 49-53, and 59-65 contain examples of  how these
  754.             procedures would be called from outside of package CONSOLE.
  755.                  Note that  the  WRITE  procedure  on  line  58 accepts two parameters, an
  756.             INTEGER and a NATURAL number with a default value.  The default value of 0 for
  757.             the  WIDTH parameter tells the procedure to use as many spaces as necessary to
  758.             output  the number.  As indicated by lines 63-65, the WIDTH parameter does not
  759.             have to be referenced when the procedure is called.
  760.  
  761.  
  762.  
  763.                                                 - 11 -
  764.  
  765.  
  766.  
  767.                                 BD3 - A Demonstration of Tasking in Ada
  768.  
  769.  
  770.  
  771.                 ======================================================================
  772.  
  773.              67     procedure WRITE(ITEM           : in FLOAT;
  774.              68                     BEFORE_DECIMAL : in NATURAL := 5;
  775.              69                     AFTER_DECIMAL  : in NATURAL := 5);
  776.              70     procedure WRITE_SCIENTIFIC(ITEM          : in FLOAT;
  777.              71                                AFTER_DECIMAL : in NATURAL := 8);
  778.              72     -- Print floats
  779.              73     -- Examples:
  780.              74     --  Ada procedure call                      Prints (without quotes)
  781.              75     --  ============================            =======================
  782.              76     --  CONSOLE.WRITE (25.21);                  "   25.21000"
  783.              77     --  CONSOLE.WRITE (-36.2);                  "  -36.20000"
  784.              78     --  CONSOLE.WRITE (-36.2, 1, 1);            "-36.2"
  785.              79     --  CONSOLE.WRITE (25.21, 3);               " 25.21000"
  786.              80     --  CONSOLE.WRITE (25.21, 3, 2);            " 25.21"
  787.              81     --  CONSOLE.WRITE_SCIENTIFIC (23.0);        " 2.30000000e+01"
  788.              82     --  CONSOLE.WRITE_SCIENTIFIC (5.7, 2);      " 5.70E+00"
  789.              83     --  CONSOLE.WRITE_SCIENTIFIC (-4.5e-24, 4); "-4.5000E-24"
  790.              84
  791.  
  792.                        Figure 12: Specification of Package CONSOLE, Part 3 of 4
  793.                        Figure 12
  794.  
  795.                 ======================================================================
  796.  
  797.                  Figure  12  contains  the next part  of  the  specification  for  package
  798.             CONSOLE.  Lines  67-69  define  the  specification for a WRITE procedure which
  799.             works on  objects  of  type  FLOAT (floating point) to output them in the form
  800.             "nn.nn"  while lines 70-71 define the  specification  for  a  WRITE_SCIENTIFIC
  801.             procedure which also works on  objects  of  type  FLOAT  but  outputs  them in
  802.             scientific notation.
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.                                                 - 12 -
  830.  
  831.  
  832.  
  833.                                 BD3 - A Demonstration of Tasking in Ada
  834.  
  835.  
  836.  
  837.                 ======================================================================
  838.  
  839.              85     procedure READ(ITEM : out OUTSTRING);
  840.              86     -- Read strings
  841.              87     -- Example (note: <CR> refers to the RETURN key):
  842.              88     --  MY_STRING : CONSOLE.OUTSTRING; -- 80-char string
  843.              89     --  Ada procedure call              User Types      In MY_STRING
  844.              90     --  ====================            ==========      ============
  845.              91     --  CONSOLE.READ (MY_STRING);       "Hi<CR>"        "Hi"<and 78 spaces>
  846.              92
  847.              93     function TRIM(ITEM : in STRING) return STRING;
  848.              94     -- Generate a string which has no trailing spaces
  849.              95     -- Example of use:
  850.              96     --  MY_STRING : CONSOLE.OUTSTRING;
  851.              97     --  CONSOLE.READ(MY_STRING);
  852.              98     --  CONSOLE.WRITE("Hello, ");
  853.              99     --  CONSOLE.WRITE(CONSOLE.TRIM(MY_STRING));
  854.             100     --  CONSOLE.WRITE(", how are you?");
  855.             101     -- If the CONSOLE.READ statement returns "Joe" followed by 77 spaces,
  856.             102     -- the output will look like "Hello, Joe, how are you?"
  857.             103
  858.             104 end CONSOLE;
  859.  
  860.                        Figure 13: Specification of Package CONSOLE, Part 4 of 4
  861.                        Figure 13
  862.  
  863.                 ======================================================================
  864.  
  865.                  Figure 13 shows the last part of package CONSOLE.  Procedure READ on line
  866.             85  always  returns  an object of type OUTSTRING, which is 80 characters long.
  867.             When this procedure is called, the  next  line  typed by the user is returned.
  868.             If it is less than 80 characters long, trailing spaces are appended to it.  If
  869.             it is more than 80 characters long, the first 80  characters  are returned and
  870.             the next call  to  READ returns the next 80.  For the convenience of the user,
  871.             the  function TRIM (line 93) is provided to convert a string of any length  to
  872.             one of another length that contains all characters of the input  string except
  873.             the trailing spaces.
  874.  
  875.                 ======================================================================
  876.  
  877.               MY_NUMBER : FLOAT; -- variable definition
  878.               ...
  879.               MY_NUMBER := RANDOM.NUMBER; -- compute the next random number
  880.  
  881.                             Figure 14: Calling the Random Number Generator
  882.                             Figure 14
  883.  
  884.                 ======================================================================
  885.  
  886.                  The  random  number  generator object used by the Bank Demo Ada system is
  887.             defined by the Ada package named RANDOM.  Figure 15 contains the specification
  888.             of this  package.    RANDOM  contains  only  one function, named NUMBER, which
  889.             requires no input parameters and returns  the  next random number as an object
  890.             of type FLOAT.  A code segment like the one  in  Figure 14 is used to call the
  891.             NUMBER function in package RANDOM:
  892.  
  893.  
  894.  
  895.  
  896.                                                 - 13 -
  897.  
  898.  
  899.  
  900.                                 BD3 - A Demonstration of Tasking in Ada
  901.  
  902.  
  903.  
  904.                 ======================================================================
  905.  
  906.               1 package RANDOM is
  907.               2 --------------------------------------------------------------------------
  908.               3 --| BEGIN PROLOGUE
  909.               4 --| DESCRIPTION            : Package RANDOM contains the function NUMBER
  910.               5 --|                        : which returns a pseudo-random number
  911.               6 --|                        : of type FLOAT in the range 0.0 .. 1.0.
  912.               7 --|                        :
  913.               8 --| REQUIREMENTS SUPPORTED : Random Number Generator
  914.               9 --|                        :
  915.              10 --| LIMITATIONS            : None
  916.              11 --|                        :
  917.              12 --| AUTHOR(S)              : Richard Conn (RLC) from Bill Whitaker's work
  918.              13 --| CHANGE LOG             : 09/30/88  RLC  Design, code, test from
  919.              14 --|                        :                 Bill Whitaker's original work
  920.              15 --|                        : 10/11/88  RLC  Modified based on ideas from
  921.              16 --|                        :                 Ron Bell and his RAN2 Package
  922.              17 --|                        :
  923.              18 --| REMARKS                : None
  924.              19 --|                        :
  925.              20 --| PORTABILITY ISSUES     : Uses 16-bit integers, so should be quite
  926.              21 --|                        : portable
  927.              22 --| END PROLOGUE
  928.              23 --------------------------------------------------------------------------
  929.              24
  930.              25     function NUMBER return FLOAT;
  931.              26     -- Return a floating point pseudo-random number
  932.              27
  933.              28 end RANDOM;
  934.  
  935.                               Figure 15: Specification of Package RANDOM
  936.                               Figure 15
  937.  
  938.                 ======================================================================
  939.  
  940.                  If you look at the body of  package  RANDOM,  which  is  included  in the
  941.             source code distributed with Bank Demo,  you  will find that RANDOM contains a
  942.             hidden procedure called SEED that sets the first random number in the sequence
  943.             based  on the time-of-day clock value returned  by  the  Ada-standard  package
  944.             called CALENDAR.  Procedure SEED, however, is not needed by the outside world,
  945.             so package RANDOM does not make it available.
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.                                                 - 14 -
  963.  
  964.  
  965.  
  966.                                 BD3 - A Demonstration of Tasking in Ada
  967.  
  968.  
  969.  
  970.                 ======================================================================
  971.  
  972.               1 package BANK is
  973.               2 --------------------------------------------------------------------------
  974.               3 --| BEGIN PROLOGUE
  975.               4 --| DESCRIPTION            : BANK defines a bank, the TELLER objects
  976.               5 --|                        : within it, and the procedure PRINT_REPORT
  977.               6 --|                        : (which reports on the status of the BANK).
  978.               7 --|                        :
  979.               8 --|                        : BANK is an abstract state machine, defining
  980.               9 --|                        : a BANK object which contains TELLER objects.
  981.              10 --|                        :
  982.              11 --| REQUIREMENTS SUPPORTED : Bank Demonstration Program to show
  983.              12 --|                        : object-oriented design and tasking
  984.              13 --|                        : with Ada
  985.              14 --|                        :
  986.              15 --| LIMITATIONS            : None
  987.              16 --| AUTHOR(S)              : Richard Conn (RLC)
  988.              17 --| CHANGE LOG             : 1/16/89  RLC  Initial Design and Code
  989.              18 --| CHANGE LOG             : 2/25/89  RLC  Final Review Prior to Release
  990.              19 --| REMARKS                : None
  991.              20 --| PORTABILITY ISSUES     : None
  992.              21 --| END PROLOGUE
  993.              22 --------------------------------------------------------------------------
  994.              23
  995.  
  996.                         Figure 16: Specification of Package BANK, Part 1 of 4
  997.                         Figure 16
  998.  
  999.                 ======================================================================
  1000.  
  1001.                  Figure 16 contains the  prologue  of  the  specification of package BANK,
  1002.             which defines the bank object.  Note that the tellers are implemented as tasks
  1003.             (I refer to the tellers in a general sense as  TELLER(n), where n is 1 to 4 to
  1004.             indicate which teller).
  1005.  
  1006.                 ======================================================================
  1007.  
  1008.              24     -- TRANSACTION requested of a TELLER
  1009.              25     type TRANSACTION  is (ADD_NEW_CUSTOMER,
  1010.              26                           GET_BALANCE,
  1011.              27                           MAKE_DEPOSIT,
  1012.              28                           MAKE_WITHDRAWAL);
  1013.              29
  1014.              30     -- Unit of currency
  1015.              31     type DOLLAR       is new FLOAT;
  1016.              32
  1017.              33     -- Identification of a CUSTOMER
  1018.              34     type CUSTOMER_ID  is new NATURAL range 1 .. NATURAL'LAST;
  1019.              35
  1020.  
  1021.                         Figure 17: Specification of Package BANK, Part 2 of 4
  1022.                         Figure 17
  1023.  
  1024.                 ======================================================================
  1025.  
  1026.  
  1027.  
  1028.  
  1029.                                                 - 15 -
  1030.  
  1031.  
  1032.  
  1033.                                 BD3 - A Demonstration of Tasking in Ada
  1034.  
  1035.  
  1036.  
  1037.                  Figure 17 shows  more  of  the  specification  of package BANK.  The type
  1038.             definitions are presented first.  Lines 25-28 define  type  TRANSACTION, which
  1039.             are the transactions which may  be  requested  of  a  TELLER(n)  object.   The
  1040.             transactions  which  may  be  performed  by  a  teller  are  ADD_NEW_CUSTOMER,
  1041.             GET_BALANCE, MAKE_DEPOSIT,  and MAKE_WITHDRAWAL.  Line 31 defines type DOLLAR,
  1042.             which  is  the  unit  of  currency for the bank.  DOLLAR is implemented  as  a
  1043.             floating point number, derived from type FLOAT, but DOLLAR objects  are unique
  1044.             from FLOAT objects by this type definition (DOLLAR is a  derived  type in Ada)
  1045.             and we cannot interchange the use of a DOLLAR object and a FLOAT object.  Line
  1046.             34 defines  type  CUSTOMER_ID,  which  is  the  ID number associated with each
  1047.             customer.  When a transaction is made with a teller, the customer presents his
  1048.             ID  number  to identify himself along with  his  transaction  request.    Like
  1049.             DOLLAR, CUSTOMER_ID is a derived type, but CUSTOMER_ID is  derived  from  type
  1050.             NATURAL  and  can  take on values from 1 to NATURAL'LAST  (the  largest  value
  1051.             allowed for objects of type NATURAL).
  1052.  
  1053.                 ======================================================================
  1054.  
  1055.              36     -- Index of and Number of TELLER objects within the bank
  1056.              37     type TELLER_INDEX is new NATURAL range 1 .. 4;
  1057.              38
  1058.              39     -- A TELLER_PERSON is an object to which a CUSTOMER may make a REQUEST
  1059.              40     -- The BANK tells the TELLER_PERSON to START_WORK, giving the
  1060.              41     -- TELLER_PERSON its TELLER_NUMBER
  1061.              42     task type TELLER_PERSON is
  1062.              43         entry REQUEST(ID     : in out CUSTOMER_ID;
  1063.              44                       KIND   : in TRANSACTION;
  1064.              45                       AMOUNT : in out DOLLAR);
  1065.              46     end TELLER_PERSON;
  1066.              47     -- These are the TELLER objects available at the bank
  1067.              48     TELLER : array(TELLER_INDEX) of TELLER_PERSON;
  1068.              49
  1069.  
  1070.                         Figure 18: Specification of Package BANK, Part 3 of 4
  1071.                         Figure 18
  1072.  
  1073.                 ======================================================================
  1074.  
  1075.                  In  Figure  18,  line  37  defines  type  TELLER_INDEX,  which is used to
  1076.             identify the desired teller.    Type TELLER_INDEX is derived from type NATURAL
  1077.             and restricted in range from 1 to 4.    If  the  user  wishes  to  modify this
  1078.             program and add  more  tellers to the model, he need only change the 4 on line
  1079.             37 to the desired number.  The parts of the code which need to  know  how many
  1080.             tellers  are  available  at the bank reference TELLER_INDEX'LAST, which is the
  1081.             largest value that objects of type TELLER_INDEX can take on.
  1082.                  Task type TELLER_PERSON on lines 42-46 defines the interface to all tasks
  1083.             of type TELLER_PERSON.  A TELLER_PERSON task contains one entry  point, called
  1084.             REQUEST,  which  can  be  called  by  tasks  external to package BANK.   Three
  1085.             parameters must be specified when the REQUEST entry point is called: the ID of
  1086.             the customer (type  CUSTOMER_ID),  the KIND of transaction (type TRANSACTION),
  1087.             and the AMOUNT associated with the transaction (type DOLLAR).
  1088.                  The task body of  TELLER_PERSON  is not contained in the specification of
  1089.             package BANK, but it is hidden in the body of package BANK.  You can  see this
  1090.             code  by looking at the source code provided in the distribution.  In summary,
  1091.             tasks  of type TELLER_PERSON are quite simple in their operation.  When a task
  1092.  
  1093.  
  1094.  
  1095.                                                 - 16 -
  1096.  
  1097.  
  1098.  
  1099.                                 BD3 - A Demonstration of Tasking in Ada
  1100.  
  1101.  
  1102.  
  1103.             of   type  TELLER_PERSON  starts  running,  it   calls   the   internal   task
  1104.             TELLER_ASSIGNER (which is hidden inside of  package  BANK) in order to get its
  1105.             ID number.  As each customer has an ID, so  each  teller  also has an ID.  The
  1106.             teller's ID  is  used  to  keep a running record of the number of transactions
  1107.             each teller makes.  After it gets it's ID number, a task of type TELLER_PERSON
  1108.             enters an infinite loop in which it waits  for  an  external  task to call its
  1109.             REQUEST entry point, processes the request from this external  task  based  on
  1110.             the  KIND of transaction requested, and then resumes the loop.  It is left  as
  1111.             an exercise to the reader to read the code and figure out exactly what happens
  1112.             for each KIND of transaction.
  1113.                  Line 48 defines the array, named  TELLER,  of  tellers in the bank.  Each
  1114.             element  of  this  array  is  of  type  TELLER_PERSON,  making each element an
  1115.             independent task  as  opposed  to  what the reader may normally think of as an
  1116.             array (which is an array of values stored  in  memory).    The array TELLER is
  1117.             indexed  by  TELLER_INDEX, taking on index values from 1 to 4.    Hence,  four
  1118.             tasks are created by  the  array declaration on line 48: TELLER(1), TELLER(2),
  1119.             TELLER(3), and TELLER(4).  Each task starts running  during  initialization of
  1120.             the Ada system  before the first line  of  code  of  the mainline procedure is
  1121.             executed.
  1122.  
  1123.                 ======================================================================
  1124.  
  1125.              50     -- PRINT_REPORT gives the transaction log of all the bank
  1126.              51     -- customers
  1127.              52     procedure PRINT_REPORT;
  1128.              53
  1129.              54     -- STOP_WORK terminates all TELLER tasks
  1130.              55     procedure STOP_WORK;
  1131.              56 end BANK;
  1132.  
  1133.                         Figure 19: Specification of Package BANK, Part 4 of 4
  1134.                         Figure 19
  1135.  
  1136.                 ======================================================================
  1137.  
  1138.                  Package  BANK  also  exports  two  procedures,  as  shown in  Figure  19:
  1139.             PRINT_REPORT and STOP_WORK.  PRINT_REPORT  (whose specification is on line 52)
  1140.             prints  a report of the status of the bank.  This report indicates the  number
  1141.             of transactions requested by  each  customer,  the  balance of each customer's
  1142.             account, and the number of transactions processed by each teller.  These items
  1143.             of information are updated constantly by the TELLER(n)  tasks  by  means  of a
  1144.             data  base kept internal to package BANK.  The outside world does not need  to
  1145.             know  that this data base exists or what form it exists in; the outside  world
  1146.             only  needs to know that it can print this report based on information in  the
  1147.             data base.  PRINT_REPORT is called by the BD3 mainline procedure  whenever the
  1148.             user at the console asks for it or during the running of the continuous status
  1149.             display command issued by the user.  STOP_WORK (whose specification is on line
  1150.             55) terminates  all  of  the  TELLER(n) tasks.  STOP_WORK is called by the BD3
  1151.             mainline procedure when the user asks for the program to  shut  down (the exit
  1152.             command).   All tasks of the Ada system must be terminated before the mainline
  1153.             procedure,  which  is also a task, may  terminate.    STOP_WORK  represents  a
  1154.             function that would be performed by a bank when it closes and it is reasonable
  1155.             to provide it as part of the  specification  of  package  BANK  as  opposed to
  1156.             relying  upon the mainline procedure to abort each of the TELLER(n) tasks on a
  1157.             task-by-task basis.
  1158.  
  1159.  
  1160.  
  1161.                                                 - 17 -
  1162.  
  1163.  
  1164.  
  1165.                                 BD3 - A Demonstration of Tasking in Ada
  1166.  
  1167.  
  1168.  
  1169.                 ======================================================================
  1170.  
  1171.             282 package CUSTOMER_WORLD is
  1172.             283 --------------------------------------------------------------------------
  1173.             284 --| BEGIN PROLOGUE
  1174.             285 --| DESCRIPTION            : CUSTOMER_WORLD is an abstract state machine
  1175.             286 --|                        : which defines the collection of all CUSTOMERs
  1176.             287 --|                        : of the BANK.  It allows the mainline procedure
  1177.             288 --|                        : to ADD a new CUSTOMER and TERMINATE_ALL
  1178.             289 --|                        : current CUSTOMERs.  The CUSTOMER itself acts
  1179.             290 --|                        : as an independent task and requires no
  1180.             291 --|                        : direct interaction with the mainline once
  1181.             292 --|                        : it starts.
  1182.             293 --|                        :
  1183.             294 --| REQUIREMENTS SUPPORTED : Bank Demonstration Program to show
  1184.             295 --|                        : object-oriented design and tasking
  1185.             296 --|                        : with Ada
  1186.             297 --|                        :
  1187.             298 --| LIMITATIONS            : None
  1188.             299 --| AUTHOR(S)              : Richard Conn (RLC)
  1189.             300 --| CHANGE LOG             : 1/16/89  RLC  Initial Design and Code
  1190.             301 --| CHANGE LOG             : 2/25/89  RLC  Final Review Prior to Release
  1191.             302 --| REMARKS                : None
  1192.             303 --| PORTABILITY ISSUES     : None
  1193.             304 --| END PROLOGUE
  1194.             305 --------------------------------------------------------------------------
  1195.             306
  1196.             307     -- Add another CUSTOMER task to the system
  1197.             308     procedure ADD;
  1198.             309
  1199.             310     -- Terminate all CUSTOMERs in the system
  1200.             311     procedure TERMINATE_ALL;
  1201.             312
  1202.             313 end CUSTOMER_WORLD;
  1203.  
  1204.                           Figure 20: Specification of Package CUSTOMER_WORLD
  1205.                           Figure 20
  1206.  
  1207.                 ======================================================================
  1208.  
  1209.                  Figure 20 contains the specification of the package CUSTOMER_WORLD.  This
  1210.             package  contains,  hidden within it, all of  the  customer  tasks  which  are
  1211.             running  in the system.  Internally, package  CUSTOMER_WORLD  keeps  track  of
  1212.             these tasks  by  creating  a  linked  list  which points to each of them.  The
  1213.             procedure  ADD  (whose  specification  is  on  line  308) causes  the  package
  1214.             CUSTOMER_WORLD to spawn another customer task, updating the  linked  list when
  1215.             it  does  so.    When  each  customer  task  is  spawned,  it  begins  running
  1216.             immediately, performing the functions of an independent customer.    There are
  1217.             no entry points  to a customer; the outside world does not request access to a
  1218.             customer,  and  the  customer tasks each  act  independently  of  each  other,
  1219.             requesting access to the  TELLER(n)  tasks in the bank to perform transactions
  1220.             as they need them.  Each time the user issues the "start new customer" command
  1221.             from  the  console,  procedure CUSTOMER_WORLD.ADD is called  to  perform  this
  1222.             function, and a new customer task starts running.
  1223.  
  1224.  
  1225.  
  1226.  
  1227.                                                 - 18 -
  1228.  
  1229.  
  1230.  
  1231.                                 BD3 - A Demonstration of Tasking in Ada
  1232.  
  1233.  
  1234.  
  1235.                  As mentioned in the  discussion  of package BANK, all children tasks have
  1236.             to  be  aborted  before the mainline  procedure  can  terminate,  and  package
  1237.             CUSTOMER_WORLD exports  the procedure TERMINATE_ALL to do this.  TERMINATE_ALL
  1238.             moves  through  the  linked  list  of  tasks,  aborting  each  one  as  it  is
  1239.             encountered.  The BD3  mainline  procedure  calls CUSTOMER_WORLD.TERMINATE_ALL
  1240.             just like it calls BANK.STOP_WORK when the exit command is processed.
  1241.                  The task specification and body of a customer  (the  task  type  is named
  1242.             CUSTOMER) is hidden in the body of package CUSTOMER_WORLD.  Each customer task
  1243.             performs the following sequence of operations:
  1244.  
  1245.               1. The customer selects a TELLER(n)  task  at random using the random number
  1246.             generator  (function  NUMBER  in package RANDOM).    The  customer  issues  an
  1247.             ADD_NEW_CUSTOMER transaction through  the  REQUEST entry point of the selected
  1248.             TELLER(n) task.  The selected TELLER(n) task returns the  customer  task's  ID
  1249.             number, which the customer task stores for use on all future transactions with
  1250.             all TELLER(n) tasks.
  1251.               2.  The  customer,  using his ID number, then selects a  TELLER(n)  task  at
  1252.             random and issues a MAKE_DEPOSIT  transaction  through  that  teller's REQUEST
  1253.             entry  point.  The customer deposits $100.00 (see lines 325 and 379-380 in the
  1254.             full listing of BD3 distributed with this document).
  1255.               3. The customer  now enters an infinite loop, which he stays in for the rest
  1256.             of his life  (until  he is aborted by the user at the console issuing the exit
  1257.             command).  This loop (see lines 389-399 of the source listing) consists of the
  1258.             following steps:
  1259.               3.1.  Delay a random amount of time from 0 to 5 seconds.
  1260.               3.2.  Generate a random  amount  of  money  from  -$50.00 to $50.00.  If the
  1261.             amount is negative, select the current transaction to  be  MAKE_WITHDRAWAL and
  1262.             make  the  amount  positive.  If the amount is positive,  select  the  current
  1263.             transaction to be MAKE_DEPOSIT.
  1264.               3.3.    Select  a TELLER(n) task at random, call that teller's REQUEST entry
  1265.             point,  passing  to  it the his ID number, the desired  transaction,  and  the
  1266.             amount.   The TELLER(n) task will then process  the  transaction,  allowing  a
  1267.             withdrawal only if the customer has sufficient funds to cover the request.
  1268.  
  1269.                  Figures  21  to  24 contain the source code of the mainline procedure for
  1270.             this Ada  system,  procedure  BD3  (short for Bank Demo 3).  This procedure is
  1271.             where the execution of the mainline begins.   All  tasks  which  were declared
  1272.             statically as objects, such as the TELLER(n) tasks of package BANK,  will have
  1273.             already been initialized  and  begun  execution  before the first line of this
  1274.             procedure is executed.  The customer tasks will be dynamically  created during
  1275.             the execution of this procedure, where a new  customer  task  will  be created
  1276.             each time the user issues the "start next customer" command from the console.
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292.                                                 - 19 -
  1293.  
  1294.  
  1295.  
  1296.                                 BD3 - A Demonstration of Tasking in Ada
  1297.  
  1298.  
  1299.  
  1300.                 ======================================================================
  1301.  
  1302.             442 with BANK;
  1303.             443 with CUSTOMER_WORLD;
  1304.             444 with CONSOLE;
  1305.             445 procedure BD3 is                           -- BANK_DEMO_3
  1306.             446 ------------------------------------------------------------------------
  1307.             447 --| BEGIN PROLOGUE
  1308.             448 --| DESCRIPTION            : Procedure BD3 (BANK DEMO 3) is a mainline
  1309.             449 --|                        : which demonstrates the operation of the
  1310.             450 --|                        : BANK.  Upon invocation, the console becomes
  1311.             451 --|                        : a command processor for the bank manager.
  1312.             452 --|                        : The bank manager can obtain the status of
  1313.             453 --|                        : the bank (balances, number of transactions,
  1314.             454 --|                        : and attempted overdraws of all customers
  1315.             455 --|                        : and number of transactions processed by all
  1316.             456 --|                        : tellers) in a single or continuous (group
  1317.             457 --|                        : of 11 over about one minute) display.
  1318.             458 --|                        : The user at the console can also cause new
  1319.             459 --|                        : Customer tasks to be created and shut down
  1320.             460 --|                        : the system.
  1321.             461 --|                        :
  1322.             462 --| REQUIREMENTS SUPPORTED : Bank Demonstration Program to show
  1323.             463 --|                        : object-oriented design and tasking
  1324.             464 --|                        : with Ada
  1325.             465 --|                        :
  1326.             466 --| LIMITATIONS            : None
  1327.             467 --| AUTHOR(S)              : Richard Conn (RLC)
  1328.             468 --| CHANGE LOG             : 1/16/89  RLC  Initial Design and Code
  1329.             469 --| CHANGE LOG             : 2/25/89  RLC  Final Review Prior to Release
  1330.             470 --| REMARKS                : None
  1331.             471 --| PORTABILITY ISSUES     : Uses CONSOLE (TEXT_IO), so is very portable;
  1332.             472 --|                        : no known portability problems.
  1333.             473 --| END PROLOGUE
  1334.             474 ------------------------------------------------------------------------
  1335.             475
  1336.  
  1337.                                 Figure 21: Procedure BD3, Part 1 of 4
  1338.                                 Figure 21
  1339.  
  1340.                 ======================================================================
  1341.  
  1342.                  In Figure 21, lines 442-444 establish the context in which  the procedure
  1343.             functions.  In particular, the procedure BD3 needs to know about package BANK,
  1344.             package  CUSTOMER_WORLD,  and  package  CONSOLE.   These Ada packages must  be
  1345.             previously compiled into the current program unit library before procedure BD3
  1346.             can  be  compiled.   The Ada compiler  will  check  all  interfaces  (such  as
  1347.             procedure and object names, parameters passed to the procedures, and the types
  1348.             of all objects referenced) between these packages and procedure BD3  each time
  1349.             BD3 references one of these packages.
  1350.                  Line  445  introduces  the procedure (is the beginning of procedure BD3's
  1351.             body), and the rest of the lines in Figure 21 are comments in the prologue.
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.                                                 - 20 -
  1359.  
  1360.  
  1361.  
  1362.                                 BD3 - A Demonstration of Tasking in Ada
  1363.  
  1364.  
  1365.  
  1366.                 ======================================================================
  1367.  
  1368.             476     -- Line input from the console
  1369.             477     INPUT                               : CONSOLE.OUTSTRING;
  1370.             478
  1371.             479     -- Number of continuous status reports displayed by the 'c'
  1372.             480     -- command before control is returned to the console
  1373.             481     NUMBER_OF_CONTINUOUS_STATUS_REPORTS : constant := 10;
  1374.             482
  1375.  
  1376.                                 Figure 22: Procedure BD3, Part 2 of 4
  1377.                                 Figure 22
  1378.  
  1379.                 ======================================================================
  1380.  
  1381.                  Figure 22 shows a local variable  and  a local constant in procedure BD3.
  1382.             The variable INPUT is used  to receive the line entered by the user when he is
  1383.             prompted for input.  The READ  procedure  in  package CONSOLE is used to input
  1384.             this line.  The definition of the constant number of continuous status reports
  1385.             is self-explanatory.
  1386.  
  1387.                 ======================================================================
  1388.  
  1389.             483 --
  1390.             484 -- procedure BD3
  1391.             485
  1392.             486 begin                                      -- mainline
  1393.             487
  1394.             488     -- This is the beginning of the main loop.  In this loop,
  1395.             489     -- a list of commands is printed on the console, the user
  1396.             490     -- at the console (as CUSTOMER 0) enters one of these commands
  1397.             491     -- followed by striking the RETURN key, and the command is
  1398.             492     -- processed.
  1399.             493     loop
  1400.             494
  1401.             495         -- Command listing and prompt
  1402.             496     CONSOLE.WRITE("Enter");
  1403.             497     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  1404.             498     CONSOLE.WRITE("  b for bank status");
  1405.             499     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  1406.             500     CONSOLE.WRITE("  c for continuous bank status");
  1407.             501     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  1408.             502     CONSOLE.WRITE("  s for start next customer");
  1409.             503     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  1410.             504     CONSOLE.WRITE("  x for exit: ");
  1411.             505     CONSOLE.READ(INPUT);
  1412.             506
  1413.  
  1414.                                 Figure 23: Procedure BD3, Part 3 of 4
  1415.                                 Figure 23
  1416.  
  1417.                 ======================================================================
  1418.  
  1419.                  Figure 23 shows the beginning of the executable code  in  procedure  BD3.
  1420.             This  code starts after the "begin" statement on line 486, and the entirety of
  1421.             this code is contained within the loop which starts on line 493.
  1422.  
  1423.  
  1424.  
  1425.                                                 - 21 -
  1426.  
  1427.  
  1428.  
  1429.                                 BD3 - A Demonstration of Tasking in Ada
  1430.  
  1431.  
  1432.  
  1433.                  The first thing done in this  loop  is  the presentation of the prompt to
  1434.             the  user  (lines 496-504) and the input of the command from  the  user  (line
  1435.             505).
  1436.  
  1437.                 ======================================================================
  1438.  
  1439.             507         -- Interpretation and execution of input command
  1440.             508     case INPUT(1) is
  1441.             509         when 'b' | 'c' =>              -- Short or continuous bank status
  1442.             510                                        -- report
  1443.             511         BANK.PRINT_REPORT;
  1444.             512         if INPUT(1) = 'c' then
  1445.             513             for I in 1 .. NUMBER_OF_CONTINUOUS_STATUS_REPORTS loop
  1446.             514             delay 5.0;
  1447.             515             BANK.PRINT_REPORT;
  1448.             516                     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  1449.             517             end loop;
  1450.             518         end if;
  1451.             519
  1452.             520         when 's' =>                    -- Start up a new CUSTOMER
  1453.             521         CUSTOMER_WORLD.ADD;
  1454.             522
  1455.             523         when 'x' =>                      -- Exit program
  1456.             524         CUSTOMER_WORLD.TERMINATE_ALL;    -- Kill CUSTOMER tasks
  1457.             525         BANK.STOP_WORK;                  -- Kill TELLER tasks
  1458.             526         exit;                            -- Exit loop
  1459.             527
  1460.             528         when ' ' =>                    -- Non-error on a null input line
  1461.             529         null;
  1462.             530
  1463.             531         when others =>                 -- Other commands are invalid
  1464.             532         CONSOLE.WRITE("Invalid Command: ");
  1465.             533         CONSOLE.WRITE(CONSOLE.TRIM(INPUT));
  1466.             534         CONSOLE.WRITE(CONSOLE.NEW_LINE);
  1467.             535
  1468.             536     end case;
  1469.             537
  1470.             538     end loop;
  1471.             539
  1472.             540 end BD3;
  1473.  
  1474.                                 Figure 24: Procedure BD3, Part 4 of 4
  1475.                                 Figure 24
  1476.  
  1477.                 ======================================================================
  1478.  
  1479.                  Figure 24 shows the rest of procedure  BD3.    The  first  letter  of the
  1480.             command  just  input  (line 505 of Figure 23)  is  the  target  for  the  case
  1481.             expression  in line 508.  If this letter is a lower-case 'b' (bank status)  or
  1482.             'c'  (continuous bank status), the code in lines 509-519 will be executed.  If
  1483.             this  letter  is  a lower-case 's' (start next customer), the  code  in  lines
  1484.             520-522 will be executed.  If this letter is a lower-case 'x' (exit), the code
  1485.             in lines 523-527 will be executed.  If this letter is a space, nothing happens
  1486.             (lines 528-530).  If this letter is anything else, an error message is printed
  1487.  
  1488.  
  1489.  
  1490.  
  1491.                                                 - 22 -
  1492.  
  1493.  
  1494.  
  1495.                                 BD3 - A Demonstration of Tasking in Ada
  1496.  
  1497.  
  1498.  
  1499.             (lines 531-535).    The  loop  itself ends on lines 538, and the procedure BD3
  1500.             ends on line 540.
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.             4  File Distribution
  1508.             4  File Distribution
  1509.  
  1510.  
  1511.                  Figure  25  shows  the files provided in the distribution of Bank Demo 3.
  1512.             This document is in one-column format in the file PRINT.ME.
  1513.  
  1514.                 ======================================================================
  1515.  
  1516.                  READ.ME          An introduction to the distribution
  1517.                  PRINT.ME         This document, ready to be printed as ASCII
  1518.                                     text on 8 1/2" x 11" paper in elite type
  1519.                  BD3.SPR          Source (in Borland's SPRINT) to the file PRINT.ME
  1520.  
  1521.                  CONSOLE.ADA      Compilable Ada source code to package CONSOLE
  1522.                  RANDOM.ADA       Compilable Ada source code to package RANDOM
  1523.                  BD3.ADA          Compilable Ada source code to packages BANK
  1524.                                     and CUSTOMER_WORLD and procedure BD3
  1525.                                   Note: the compilation order is CONSOLE.ADA,
  1526.                                     RANDOM.ADA, and BD3.ADA
  1527.  
  1528.                  CONSOLE.LST      Listing of CONSOLE.ADA with each line numbered
  1529.                  RANDOM.LST       Listing of RANDOM.ADA with each line numbered
  1530.                  BD3.LST          Listing of BD3.ADA with each line numbered
  1531.  
  1532.                  BD3.EXE          Executable binary of the BD3 system (run by
  1533.                                     giving the command "BD3" at the MSDOS prompt)
  1534.  
  1535.                          Figure 25: Files Distributed as Part of Bank Demo 3
  1536.                          Figure 25
  1537.  
  1538.                 ======================================================================
  1539.  
  1540.  
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558.                                                 - 23 -
  1559.  
  1560.  
  1561.  
  1562.  
  1563.  
  1564.  
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570.  
  1571.  
  1572.                                             ContentsContents
  1573.  
  1574.  
  1575.                      1  Introduction . . . . . . . . . . . . . . . . . . . . . . . 1
  1576.                      2  Execution of the Program . . . . . . . . . . . . . . . . . 3
  1577.                      3  Discussion of the Design . . . . . . . . . . . . . . . . . 9
  1578.                      4  File Distribution  . . . . . . . . . . . . . . . . . . .  23
  1579.  
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.  
  1589.  
  1590.  
  1591.  
  1592.  
  1593.  
  1594.  
  1595.  
  1596.  
  1597.  
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.                                                   i
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.  
  1664.  
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.                                                   ii
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.                                             FiguresFigures
  1703.  
  1704.  
  1705.                   Figure 1: The bank tellers can process four kinds of
  1706.                             transactions.  . . . . . . . . . . . . . . . . . . . . 1
  1707.                   Figure 2: This code segment shows that the TELLER(n) tasks are
  1708.                             accessed like procedures.  . . . . . . . . . . . . . . 2
  1709.                   Figure 3: When BD3 starts, there are no customers. . . . . . . . 3
  1710.                   Figure 4: The 's' command starts a new customer task.  . . . . . 4
  1711.                   Figure 5: Another status display shows just a little activity by
  1712.                             the customer.  . . . . . . . . . . . . . . . . . . . . 4
  1713.                   Figure 6: Five more tasks are started up.  . . . . . . . . . . . 5
  1714.                   Figure 7: A continous bank status display shows a lot of activity
  1715.                             over a short time. . . . . . . . . . . . . . . . . . . 6
  1716.                   Figure 8: Two more customer tasks are started. . . . . . . . . . 7
  1717.                   Figure 9: Two more status reports are shown, each after some
  1718.                             delay. . . . . . . . . . . . . . . . . . . . . . . . . 8
  1719.                   Figure 10: Specification of Package CONSOLE, Part 1 of 4 . . . .10
  1720.                   Figure 11: Specification of Package CONSOLE, Part 2 of 4 . . . .11
  1721.                   Figure 12: Specification of Package CONSOLE, Part 3 of 4 . . . .12
  1722.                   Figure 13: Specification of Package CONSOLE, Part 4 of 4 . . . .13
  1723.                   Figure 14: Calling the Random Number Generator . . . . . . . . .13
  1724.                   Figure 15: Specification of Package RANDOM . . . . . . . . . . .14
  1725.                   Figure 16: Specification of Package BANK, Part 1 of 4  . . . . .15
  1726.                   Figure 17: Specification of Package BANK, Part 2 of 4  . . . . .15
  1727.                   Figure 18: Specification of Package BANK, Part 3 of 4  . . . . .16
  1728.                   Figure 19: Specification of Package BANK, Part 4 of 4  . . . . .17
  1729.                   Figure 20: Specification of Package CUSTOMER_WORLD . . . . . . .18
  1730.                   Figure 21: Procedure BD3, Part 1 of 4  . . . . . . . . . . . . .20
  1731.                   Figure 22: Procedure BD3, Part 2 of 4  . . . . . . . . . . . . .21
  1732.                   Figure 23: Procedure BD3, Part 3 of 4  . . . . . . . . . . . . .21
  1733.                   Figure 24: Procedure BD3, Part 4 of 4  . . . . . . . . . . . . .22
  1734.                   Figure 25: Files Distributed as Part of Bank Demo 3  . . . . . .23
  1735.  
  1736.  
  1737.  
  1738.  
  1739.  
  1740.  
  1741.  
  1742.  
  1743.  
  1744.  
  1745.  
  1746.  
  1747.  
  1748.  
  1749.  
  1750.  
  1751.  
  1752.  
  1753.                                                  iii
  1754.