Time-Shared Computer Model

Simlib to simulate a model of a time-shared computer facility considered by Adiri and Avi-Itzhak. A company has a computer system consisting of a single central processing unit (CPU) and n terminals. The operator of each terminal ‘thinks’ for an amount of time that is an exponential random variable with mean 25 seconds, and then sends to the CPU a job having service time distributed exponentially with mean 0.8 second. ………………………..


/* External definitions for time-shared computer model. */

#include “simlib.h” /* Required for use of simlib.c. */

#define EVENT_ARRIVAL 1 /* Event type for arrival of job to CPU. */
#define EVENT_END_CPU_RUN 2 /* Event type for end of a CPU run. */
#define EVENT_END_SIMULATION 3 /* Event type for end of the simulation. */
#define LIST_QUEUE 1 /* List number for CPU queue. */
#define LIST_CPU 2 /* List number for CPU. */
#define SAMPST_RESPONSE_TIMES 1 /* sampst variable for response times. */
#define STREAM_THINK 1 /* Random-number stream for think times. */
#define STREAM_SERVICE 2 /* Random-number stream for service times. */

/* Declare non-simlib global variables. */

int min_terms, max_terms, incr_terms, num_terms, num_responses,
num_responses_required, term;
float mean_think, mean_service, quantum, swap;
FILE *infile, *outfile;

/* Declare non-simlib functions. */

void arrive(void);
void start_CPU_run(void);
void end_CPU_run(void);
void report(void);

int main() /* Main function. */
/* Open input and output files. */

infile = fopen(“tscomp.in”, “r”);
outfile = fopen(“tscomp.out”, “w”);

/* Read input parameters. */

fscanf(infile, “%d %d %d %d %f %f %f %f”,
&min_terms, &max_terms, &incr_terms, &num_responses_required,
&mean_think, &mean_service, &quantum, &swap);

/* Write report heading and input parameters. */

fprintf(outfile, “Time-shared computer model\n\n”);
fprintf(outfile, “Number of terminals%9d to%4d by %4d\n\n”,
min_terms, max_terms, incr_terms);
fprintf(outfile, “Mean think time %11.3f seconds\n\n”, mean_think);
fprintf(outfile, “Mean service time%11.3f seconds\n\n”, mean_service);
fprintf(outfile, “Quantum %11.3f seconds\n\n”, quantum);
fprintf(outfile, “Swap time %11.3f seconds\n\n”, swap);
fprintf(outfile, “Number of jobs processed%12d\n\n\n”,
fprintf(outfile, “Number of Average Average”);
fprintf(outfile, ” Utilization\n”);
fprintf(outfile, “terminals response time number in queue of CPU”);

/* Run the simulation varying the number of terminals. */

for (num_terms = min_terms; num_terms <= max_terms;
num_terms += incr_terms) {

/* Initialize simlib */


/* Set maxatr = max(maximum number of attributes per record, 4) */

maxatr = 4; /* NEVER SET maxatr TO BE SMALLER THAN 4. */

/* Initialize the non-simlib statistical counter. */

num_responses = 0;

/* Schedule the first arrival to the CPU from each terminal. */

for (term = 1; term <= num_terms; ++term)
event_schedule(expon(mean_think, STREAM_THINK), EVENT_ARRIVAL);

/* Run the simulation until it terminates after an end-simulation event
(type EVENT_END_SIMULATION) occurs. */

do {

/* Determine the next event. */


/* Invoke the appropriate event function. */

switch (next_event_type) {

/* If the event just executed was not the end-simulation event (type
EVENT_END_SIMULATION), continue simulating. Otherwise, end the
simulation. */

} while (next_event_type != EVENT_END_SIMULATION);


return 0;

void arrive(void) /* Event function for arrival of job at CPU after think
time. */

/* Place the arriving job at the end of the CPU queue.
Note that the following attributes are stored for each job record:
1. Time of arrival to the computer.
2. The (remaining) CPU service time required (here equal to the
total service time since the job is just arriving). */

transfer[1] = sim_time;
transfer[2] = expon(mean_service, STREAM_SERVICE);
list_file(LAST, LIST_QUEUE);

/* If the CPU is idle, start a CPU run. */

if (list_size[LIST_CPU] == 0)

void start_CPU_run(void) /* Non-event function to start a CPU run of a job. */
float run_time;

/* Remove the first job from the queue. */

list_remove(FIRST, LIST_QUEUE);

/* Determine the CPU time for this pass, including the swap time. */

if (quantum < transfer[2])
run_time = quantum + swap;
run_time = transfer[2] + swap;

/* Decrement remaining CPU time by a full quantum. (If less than a full
quantum is needed, this attribute becomes negative. This indicates that
the job, after exiting the CPU for the current pass, will be done and is
to be sent back to its terminal.) */

transfer[2] -= quantum;

/* Place the job into the CPU. */

list_file(FIRST, LIST_CPU);

/* Schedule the end of the CPU run. */

event_schedule(sim_time + run_time, EVENT_END_CPU_RUN);

void end_CPU_run(void) /* Event function to end a CPU run of a job. */
/* Remove the job from the CPU. */

list_remove(FIRST, LIST_CPU);

/* Check to see whether this job requires more CPU time. */

if (transfer[2] > 0.0) {

/* This job requires more CPU time, so place it at the end of the queue
and start the first job in the queue. */

list_file(LAST, LIST_QUEUE);

else {

/* This job is finished, so collect response-time statistics and send it
back to its terminal, i.e., schedule another arrival from the same
terminal. */

sampst(sim_time – transfer[1], SAMPST_RESPONSE_TIMES);

event_schedule(sim_time + expon(mean_think, STREAM_THINK),

/* Increment the number of completed jobs. */


/* Check to see whether enough jobs are done. */

if (num_responses >= num_responses_required)

/* Enough jobs are done, so schedule the end of the simulation
immediately (forcing it to the head of the event list). */

event_schedule(sim_time, EVENT_END_SIMULATION);


/* Not enough jobs are done; if the queue is not empty, start
another job. */

if (list_size[LIST_QUEUE] > 0)

void report(void) /* Report generator function. */
/* Get and write out estimates of desired measures of performance. */

fprintf(outfile, “\n\n%5d%16.3f%16.3f%16.3f”, num_terms,
sampst(0.0, -SAMPST_RESPONSE_TIMES), filest(LIST_QUEUE),

simlib.c, simlib.h, simlibdefs.h can be found here

tscomp.in – simulation input

Inputs parameter : the minimum number of terminals, the maximum number of terminals, the increment in the number of terminals, The number of responses required, mean think, mean service time, the quantum, the swap time.

tscomp.out – simulation output

Reference :
Textbook : Simulation Modeling and Analysis

Strive to be independence

April 15, 2008
Taipei City
High Speed Network Lab

Udin Harun

Comments are closed.

%d bloggers like this: