rendered paste body#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROCESSES 100
struct process
{
unsigned long timeleft;
unsigned long entered;
unsigned long started;
unsigned long finished;
int pid;
};
struct queue
{
struct process p[MAX_PROCESSES];
int push;
int pop;
};
void push(struct queue *, struct process);
struct process pop(struct queue *);
void readfile(char *);
int nextPid;
struct process pNULL;
unsigned long waitTimes[MAX_PROCESSES];
unsigned long runTimes[MAX_PROCESSES];
int processCount, i;
void push(struct queue *q, struct process p)
{
q->p[q->push] = p;
q->push++;
if (q-> push >= MAX_PROCESSES)
q ->push = 0;
}
struct process pop(struct queue *q)
{
int pos = q->pop;
if (q-> pop == q->push)
return pNULL;
q->pop++;
if (q-> pop >= MAX_PROCESSES)
q-> pop = 0;
return q->p[pos];
}
void readfile(char *filename)
{
char buffer[512];
char *token;
char *buf;
FILE *fd;
processCount = 0;
fd = fopen(filename, "r");
while ((fgets(buffer, 512, fd)) != NULL)
{
buf = buffer;
token = strtok(buf, "\t\n");
waitTimes[processCount] = atol(token);
token = strtok(NULL, "\t\n");
runTimes[processCount] = atol(token);
processCount++;
}
fclose(fd);
}
main(int argc, char **argv)
{
int quantum;
int overhead;
struct queue q, finished;
struct process p;
unsigned long clock;
unsigned long run;
int quantumc;
int overheadc;
int overheadFlag;
char filename[512];
nextPid = 1;
pNULL.pid = -1;
q.pop = 0;
q.push = 0;
finished.pop = 0;
finished.pop = 0;
clock = 0;
run = 20000;
quantum = 10;
overhead = 2;
strcpy(filename, "processes");
p = pNULL;
for (i = 1; i < argc; i++)
{
if ((strcmp(argv[i], "-r")) == 0 )
run = atol(argv[i+1]);
if ((strcmp(argv[i], "-q")) == 0 )
quantum = atol(argv[i+1]);
if ((strcmp(argv[i], "-o")) == 0 )
overhead = atol(argv[i+1]);
if ((strcmp(argv[i], "-f")) == 0 )
strcpy(filename, argv[i+1]);
}
quantumc = quantum;
overheadc = overhead;
overheadFlag = 0;
readfile(filename);
while ( run > 0 )
{
for (i = 0; i < processCount; i ++)
{
if (waitTimes[i] == clock)
{
struct process newP;
newP.pid = nextPid;
newP.timeleft = runTimes[i];
newP.started = 99;
newP.finished = 0;
newP.entered = clock;
nextPid++;
push(&q, newP);
}
/* if no process is loaded, then load one*/
if (p.pid < 0 && overheadFlag == 0)
{
p = pop(&q);
/* set started if its the first time running*/
if((p.pid > 0) && (p.started > clock))
p.started = clock;
}
/* If there is a process in the cpu */
if (p.pid > 0)
{
p.timeleft -= 1;
quantumc--;
/* if the process is done */
if (p.timeleft == 0)
{
p.finished = clock;
push(&finished, p);
p = pNULL;
quantumc = quantum;
overheadFlag = 1;
}
/* if this timeslice is done*/
else if (quantumc == 0)
{
push(&q, p);
p = pNULL;
quantumc = quantum;
overheadFlag = 1;
}
}
/* If overhead work needs to be done*/
else if (overheadFlag == 1)
{
overhead--;
if (overhead == 0)
{
overheadFlag = 0;
overheadc = overhead;
}
}
clock++;
run++;
}
/*Print report*/
unsigned long waitTime, avgWaitTime, turnAround, avgTurnAround;
processCount = 0;
avgWaitTime = 0;
p = pop(&finished);
while (p.pid > 0)
{
waitTime = p.started = p.entered;
avgWaitTime += waitTime;
turnAround = p.finished - p.started;
avgTurnAround += turnAround;
processCount++;
printf("Process pid: %d ", p.pid);
printf("Wait Time: %ld \t", waitTime);
printf("Turn Around Time %ld\n", turnAround);
p = pop(&finished);
}
avgWaitTime /= processCount;
avgTurnAround /= processCount;
printf("\n Average Wait Time: %ld", avgWaitTime);
printf("\n Average Turn Around Time: %ld\n", avgTurnAround);
}
}