From:	SCOVCB::GALVIN_FX    31-JAN-1989 09:05
To:	ARISIA::EVERHART
Subj:	

with text_io ; use text_io ;
with UTILITY, SIMULATION, RANDOM, RESOURCES, STATISTICS;

procedure EXAMPLE3 is
   use UTILITY.INT_IO; use UTILITY.REAL_IO;

   NUMBER_OF_TERMINALS : constant positive := 30;
   NUMBER_OF_JOBS      : constant positive := 34;
   QUANTUM             : constant float    := 0.1;
   OVERHEAD            : constant float    := 0.015;
   MEAN_THINK_TIME     : constant float    := 25.0;
   MEAN_SERVICE_TIME   : constant float    := 0.8;

   CPU                 : RESOURCES.FACILITIES.FACILITY;
   THINK, SERVICE      : RANDOM.EXPONENTIAL_RANDOM;
   RESPONSE_TIME       : STATISTICS.TALLY_STATISTIC;
   TERM_ID             : string (1..3);
   TERM_ARRAY          : array (positive range 1 .. NUMBER_OF_TERMINALS)
                        of SIMULATION.PROCESS;

   procedure PRINT_MODEL_INPUTS is
   begin
      put_line ("SIMULATION TIME UNIT = 1 SECOND"); new_line;
      put ("NUMBER_OF_TERMINALS = "); put (NUMBER_OF_TERMINALS); new_line;
      put ("NUMBER_OF_JOBS =      "); put (NUMBER_OF_JOBS);      new_line;
      put ("QUANTUM =             "); put (QUANTUM);             new_line;
      put ("OVERHEAD =            "); put (OVERHEAD);            new_line;
      put ("MEAN_THINK_TIME =     "); put (MEAN_THINK_TIME);     new_line;
      put ("MEAN_SERVICE_TIME =   "); put (MEAN_SERVICE_TIME);   new_line;
      new_line;
   end PRINT_MODEL_INPUTS;

   task type TERMINAL;
   task type JOB;

   package P1 is new SIMULATION.PROCESS_CREATOR 
                     ("TERMINAL", TERMINAL, INTEGER);
   package P2 is new SIMULATION.PROCESS_CREATOR 
                     ("JOBS", JOB, SIMULATION.PROCESS);

   task body TERMINAL is
      TERM_JOB : SIMULATION.PROCESS;
      TERM_NUM : integer;
      TERM_ID, JOB_ID : string (1..3);
   begin
      for I in 1 .. NUMBER_OF_JOBS loop
         SIMULATION.DELAY_PROCESS (RANDOM.EXPONENTIAL (THINK));
         put (JOB_ID, I);               -- convert I to ascii string
         P1.GET_ATTRIBUTES (TERM_NUM);
         put (TERM_ID, TERM_NUM);       -- convert to ascii string
         P2.CREATE_PROCESS (TERM_JOB, "T" & TERM_ID & "J" & JOB_ID, 
            SIMULATION.CURRENT_SIMULATION_TIME, 0.0, 
            SIMULATION.CURRENT_PROCESS);
         SIMULATION.SUSPEND_PROCESS;
      end loop;
      P1.DESTROY_PROCESS;
   end TERMINAL;

   task body JOB is
      START_TIME, SERVICE_TIME_LEFT, CPU_TIME : float;
      TERM_JOB : SIMULATION.PROCESS;
   begin
      START_TIME := SIMULATION.CURRENT_SIMULATION_TIME;
      SERVICE_TIME_LEFT := RANDOM.EXPONENTIAL (SERVICE);
      loop
         RESOURCES.FACILITIES.REQUEST_FACILITY (CPU);
         if SERVICE_TIME_LEFT > QUANTUM then
            CPU_TIME := QUANTUM + OVERHEAD;
            SERVICE_TIME_LEFT := SERVICE_TIME_LEFT - QUANTUM;
         else
            CPU_TIME := SERVICE_TIME_LEFT + OVERHEAD;
            SERVICE_TIME_LEFT := 0.0;
         end if;
         SIMULATION.DELAY_PROCESS (CPU_TIME);
         RESOURCES.FACILITIES.RELEASE_FACILITY (CPU);
         exit when SERVICE_TIME_LEFT = 0.0;
      end loop;
      STATISTICS.COLLECT_TALLY (RESPONSE_TIME,
         SIMULATION.CURRENT_SIMULATION_TIME - START_TIME);
      P2.GET_ATTRIBUTES (TERM_JOB);
      SIMULATION.RESUME_PROCESS (TERM_JOB, 0.0, 0.0);
      P2.DESTROY_PROCESS;
   end JOB;

begin
   put_line ("EXAMPLE3 ADASIM MODEL - TIME-SHARED COMPUTER MODEL");
   new_line;
   SIMULATION.SET_PROCESS_REPORT_OPTION (UTILITY.OFF);
   RANDOM.CREATE_EXPONENTIAL (THINK, "THINK", MEAN_THINK_TIME, 0.0, 60.0);
   RANDOM.CREATE_EXPONENTIAL (SERVICE, "SERVICE", MEAN_SERVICE_TIME, 
                             0.0, 10.0);
   RESOURCES.FACILITIES.CREATE_FACILITY (CPU, "CPU");
   STATISTICS.CREATE_TALLY (RESPONSE_TIME, "RESPONSE TIME");

   for I in 1 .. NUMBER_OF_TERMINALS loop
      put (TERM_ID, I);
      P1.CREATE_PROCESS (TERM_ARRAY(I), "TERM_" & TERM_ID, 0.0, 0.0, I);
   end loop;

   SIMULATION.START_SIMULATION;

   PRINT_MODEL_INPUTS;
   STATISTICS.REPORT_STATISTICS;
   RESOURCES.OUTPUT_RESOURCE_REPORTS;
end EXAMPLE3;
