0% found this document useful (0 votes)
72 views2 pages

Single Server FIFO C Prog

This C program simulates a single-server queue using arrival and service times from a data file. It calculates the average interarrival time, service time, delay in the queue, and wait time in the system. Arrival times are read from the file and service begins once the server is idle. Statistics like total delay and wait are accumulated and averages are calculated and printed at the end.

Uploaded by

Mubtasim Fuad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views2 pages

Single Server FIFO C Prog

This C program simulates a single-server queue using arrival and service times from a data file. It calculates the average interarrival time, service time, delay in the queue, and wait time in the system. Arrival times are read from the file and service begins once the server is idle. Statistics like total delay and wait are accumulated and averages are calculated and printed at the end.

Uploaded by

Mubtasim Fuad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* -------------------------------------------------------------------------

* This program simulates a single-server FIFO service node using arrival


* times and service times read from a text file. The server is assumed
* to be idle when the first job arrives. All jobs are processed completely
* so that the server is again idle at the end of the simulation. The
* output statistics are the average interarrival time, average service
* time, the average delay in the queue, and the average wait in the service
* node.
*
* Name : ssq1.c (Single Server Queue, version 1)
* Authors : Steve Park & Dave Geyer
* Language : ANSI C
* Latest Revision : 9-01-98
* Compile with : gcc ssq1.c
* -------------------------------------------------------------------------
*/

#include <stdio.h>

#define FILENAME "ssq1.dat" /* input data file */


#define START 0.0

/* =========================== */
double GetArrival(FILE *fp) /* read an arrival time */
/* =========================== */
{
double a;

fscanf(fp, "%lf", &a);


return (a);
}

/* =========================== */
double GetService(FILE *fp) /* read a service time */
/* =========================== */
{
double s;

fscanf(fp, "%lf\n", &s);


return (s);
}

/* ============== */
int main(void)
/* ============== */
{
FILE *fp; /* input data file */
long index = 0; /* job index */
double arrival = START; /* arrival time */
double delay; /* delay in queue */
double service; /* service time */
double wait; /* delay + service */
double departure = START; /* departure time */
struct { /* sum of ... */
double delay; /* delay times */
double wait; /* wait times */
double service; /* service times */
double interarrival; /* interarrival times */
} sum = {0.0, 0.0, 0.0};

fp = fopen(FILENAME, "r");
if (fp == NULL) {
fprintf(stderr, "Cannot open input file %s\n", FILENAME);
return (1);
}

while (!feof(fp)) {
index++;
arrival = GetArrival(fp);
if (arrival < departure)
delay = departure - arrival; /* delay in queue */
else
delay = 0.0; /* no delay */
service = GetService(fp);
wait = delay + service;
departure = arrival + wait; /* time of departure */
sum.delay += delay;
sum.wait += wait;
sum.service += service;
}
sum.interarrival = arrival - START;

printf("\nfor %ld jobs\n", index);


printf(" average interarrival time = %6.2f\n", sum.interarrival / index);
printf(" average service time .... = %6.2f\n", sum.service / index);
printf(" average delay ........... = %6.2f\n", sum.delay / index);
printf(" average wait ............ = %6.2f\n", sum.wait / index);

fclose(fp);
return (0);
}

You might also like