'' @(#) law103.sim 2/25/86
''Time-shared computer model
''An Introduction to Simulation Using SIMSCRIPT II.5, Chapter 3
PREAMBLE
  
   PROCESSES INCLUDE TERMINAL

      EVERY JOB HAS A JB.TERMINAL 
      DEFINE JB.TERMINAL AS AN INTEGER VARIABLE

   RESOURCES INCLUDE CPU

   DEFINE MEAN.SERVICE.TIME, MEAN.THINK.TIME, OVERHEAD,
      QUANTUM, AND RESPONSE.TIME AS REAL VARIABLES
      
   DEFINE INC.NUM.TERMINALS, MAX.TERMINALS, MIN.TERMINALS,
      NUM.JOBS.COMPLETED, NUM.JOBS.DESIRED, AND NUM.TERMINALS
      AS INTEGER VARIABLES

   DEFINE .SECONDS TO MEAN UNITS  

   TALLY MAX.RESPONSE.TIME AS THE MAXIMUM AND 
      MEAN.RESPONSE.TIME AS THE MEAN OF RESPONSE.TIME           

   ACCUMULATE AVG.NUMBER.IN.QUEUE AS THE AVERAGE OF N.Q.CPU

   ACCUMULATE UTIL.CPU AS THE AVERAGE OF N.X.CPU

END ''PREAMBLE 
MAIN     use 1 for input
      
   write as  /, "Test LAW103 - Time-shared Computer model", /
   CALL READ.DATA

   FOR NUM.TERMINALS = MIN.TERMINALS TO MAX.TERMINALS 
      BY INC.NUM.TERMINALS
   DO                                                                    
      CALL INITIALIZE
      START SIMULATION
   LOOP
   write as  /, /, "End of Test LAW103", /
 
END ''MAIN
ROUTINE READ.DATA

   CREATE EVERY CPU(1)
   LET U.CPU(1) = 1

   READ QUANTUM AND OVERHEAD
 
   READ MIN.TERMINALS, MAX.TERMINALS, INC.NUM.TERMINALS, AND
      MEAN.THINK.TIME
     
   READ MEAN.SERVICE.TIME AND NUM.JOBS.DESIRED

   PRINT 6 LINES THUS
 


TIME-SHARED COMPUTER MODEL -- INPUT PARAMETERS 
----------------------------------------------

   SKIP 3 LINES

   PRINT 11 LINES WITH MEAN.THINK.TIME, MEAN.SERVICE.TIME, 
      QUANTUM, OVERHEAD, AND NUM.JOBS.DESIRED THUS
   
THINK TIMES ARE EXPONENTIAL WITH MEAN ***.* SECONDS
 
SERVICE TIMES ARE EXPONENTIAL WITH MEAN ***.* SECONDS

QUANTUM    ***.*** SECONDS
  
OVERHEAD   ***.*** SECONDS
  
NUMBER OF JOBS PROCESSED   *****

   START NEW PAGE

   PRINT 6 LINES THUS



SIMULATION RESULTS (ALL TIMES ARE IN SECONDS)
---------------------------------------------

   SKIP 3 LINES

   PRINT 5 LINES THUS

 NUMBER OF        MEAN            MAXIMUM           AVERAGE            CPU 
 TERMINALS    RESPONSE TIME    RESPONSE TIME    NUMBER IN QUEUE    UTILIZATION 
 _________    _____________    _____________    _______________    ___________
 
END ''READ.DATA
ROUTINE INITIALIZE
 
   DEFINE I AS AN INTEGER VARIABLE
 
   LET TIME.V = 0
   LET NUM.JOBS.COMPLETED = 0

   RESET TOTALS OF RESPONSE.TIME, N.Q.CPU(1), AND N.X.CPU(1)
 
   FOR I = 1 TO NUM.TERMINALS
   DO
      ACTIVATE A TERMINAL NOW
   LOOP
 
END '' INITIALIZE
PROCESS TERMINAL
   
   UNTIL NUM.JOBS.COMPLETED >= NUM.JOBS.DESIRED
   DO
      WAIT EXPONENTIAL.F(MEAN.THINK.TIME,1) .SECONDS
      CREATE A JOB
      LET JB.TERMINAL(JOB) = TERMINAL
      ACTIVATE THIS JOB NOW
      SUSPEND ''THE TERMINAL
         
      ADD 1 TO NUM.JOBS.COMPLETED 
      IF NUM.JOBS.COMPLETED = NUM.JOBS.DESIRED
         CALL REPORT
      ALWAYS
   LOOP
  
END ''TERMINAL
PROCESS JOB
   
   DEFINE SERVICE.TIME AND START.TIME AS REAL VARIABLES

   LET START.TIME = TIME.V
   LET SERVICE.TIME = EXPONENTIAL.F(MEAN.SERVICE.TIME,2)
    
   WHILE SERVICE.TIME > QUANTUM
   DO
      REQUEST 1 CPU(1)
      WORK (QUANTUM + OVERHEAD) .SECONDS 
      LET SERVICE.TIME = SERVICE.TIME - QUANTUM
      RELINQUISH 1 CPU(1)
   LOOP
 
   REQUEST 1 CPU(1)
   WORK (SERVICE.TIME + OVERHEAD) .SECONDS
   RELINQUISH 1 CPU(1)
   LET RESPONSE.TIME = TIME.V - START.TIME
   REACTIVATE THE TERMINAL CALLED JB.TERMINAL(JOB) NOW
    
END ''JOB
ROUTINE REPORT
   
   PRINT 3 LINES WITH NUM.TERMINALS, MEAN.RESPONSE.TIME,
      MAX.RESPONSE.TIME, AVG.NUMBER.IN.QUEUE(1), AND UTIL.CPU(1) THUS 

       ***        *****.***        *****.***            ***.***          *.***

END ''REPORT
