You are on page 1of 9

#include <stdio.

h>
#include <stdlib.h>
#include <time.h>
#define N 32000
#define N2 100
#define N3 40
/* Process Data Structure */
struct process {
int pid; /* Process ID */
int burst; /* CPU Burst Time */
int priority; /* Priority */
int working; /* Working time, for round-robin scheduling */
int waiting; /* Waiting time, for round-robin scheduling */
struct process *next;
};
/* Function Prototype Declarations */
int random();
int randomBurst();
int randomPriority();
struct process *init_process (int pid, int burst, int priority);
void listprocs (struct process *proc);
void fcfs (struct process *proc);
void priority (struct process *proc);
void rr (struct process *proc, int quantum);
void sjf (struct process *proc);

/* Main Program Segment */


int main () {
/* Initialize process list */
int rand,rand2,rand3;
int i; /* counter to create MAX number of processes */
struct process *plist, *ptmp;
plist = init_process(1, 10, 3);
plist->next = init_process(2, 1, 1);
ptmp = plist->next;
/* Initialize variable rand with a random number up to 1000000 */
rand = 5;
for(i=3;i<(rand+1);i++ )
{
rand2 = randomBurst();
rand3 = randomPriority();
ptmp->next = init_process(i, rand2, rand3);
ptmp = ptmp->next;
rand2 = randomBurst();
rand3 = randomPriority();
}
/*ptmp->next = init_process(4, 1, 4);
ptmp = ptmp->next;
ptmp->next = init_process(5, 5, 2);*/
/* Perform simulations */
listprocs(plist);
fcfs(plist);
sjf(plist);
priority(plist);
rr(plist, 1);
/* Terminate cleanly */
while (plist != NULL) {
ptmp = plist;
plist = plist->next;
free(ptmp);
}
system("pause");
return(0);
}
/*Creates a random burst time between 0 and 100 */
int randomPriority()
{
int j;
/* Set evil seed (initial seed) */
srand( (unsigned)time( NULL ) );
j = (int) N3 * rand() / (RAND_MAX + 1.0);
return j ;
}
/*Creates a random burst time between 0 and 100 */
int randomBurst()
{
int j;
/* Set evil seed (initial seed) */
srand( (unsigned)time( NULL ) );
j = (int) N2 * rand() / (RAND_MAX + 1.0);
return j ;
}

/* Random generator to create a random number of processes*/


int random()
{
int j;
/* Set evil seed (initial seed) */
srand( (unsigned)time( NULL ) );
j = (int) N * rand() / (RAND_MAX + 1.0);
/* This function tries to generate a some what random number between 10000
and 1000000
if initial j is not greater then 10000 it recalls itself until it is */

if((j > 10000)&&(j < 11599))


{
j = (j % 33)*j;
}
else if (( j > 12465)&&(j < 21,345))
{
j = (j % 27)* j;
}
else if ((j > 22540)&&(j < 24567))
{
j = j * 13;
}
else if (( j > 25789)&&(j < 29486))
{
j = (j % 13) * (j + 8);
}
return j ;

}
/* Process list entry initialization routine */
struct process *init_process (int pid, int burst, int priority) {
struct process *proc;
proc = malloc(sizeof(struct process));
if (proc == NULL) {
printf("Fatal error: memory allocation failure.\nTerminating.\n");
exit(1);
}
proc->pid = pid;
proc->burst = burst;
proc->priority = priority;
proc->working = 0;
proc->waiting = 0;
proc->next = NULL;
return(proc);
}

/* First-Come-First-Served scheduling simulation */


void fcfs (struct process *proc) {
FILE *fcfs_stream;
int close_errorFCFS;
int time = 0, start, end;
struct process *tmp = proc;
printf("BEGIN:\tFirst-Come-First-Served scheduling simulation\n");
/* excel book to write the data to */
fcfs_stream = fopen("FirstComeFirstServed.xls","a");
fprintf(fcfs_stream,"Process ID\tEnd Time\tWaiting Time\tTurnaround time\n
\n");
while (tmp != NULL) {
start = time;
time += tmp->burst;
end = time;
printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", tmp->
pid, time, start, end);
fprintf(fcfs_stream, " %d \t %d \t %d \t %d \n",tmp->pid,time,start,
end);
tmp = tmp->next;
}
printf("END:\tFirst-Come-First-served scheduling simulation\n\n");
close_errorFCFS = fclose (fcfs_stream);
if (close_errorFCFS != 0)
{
printf ("File could not be closed.\n");
}
else
{
printf ("File closed.\n");
}
}

/* Process listing */
void listprocs (struct process *proc) {
struct process *tmp = proc;
FILE *lp_stream;
int close_lerror;
printf("BEGIN:\tProcess Listing\n");
lp_stream = fopen("Process Listing.xls","a");
fprintf(lp_stream,"Process ID\tPriority\tBurst\n\n");
while (tmp != NULL) {
printf("PID: %d\t\tPriority: %d\tBurst: %d\n", tmp->pid, tmp->priority,
tmp->burst);
fprintf(lp_stream, "%d \t %d \t %d \n",tmp->pid,tmp->priority,tmp->b
urst);
tmp = tmp->next;
}
printf("END:\tProcess Listing\n\n");
close_lerror = fclose (lp_stream);
if (close_lerror != 0)
{
printf ("File could not be closed.\n");
}
else
{
printf ("File closed.\n");
}
}

/* Priority scheduling simulation


* Note: lower priority value gets a higher priority
*/
void priority (struct process *proc) {
FILE *ps_stream;
int close_errorPS;
int time, start, end, highest;
struct process *copy, *tmpsrc, *tmp, *beforehighest;
printf("BEGIN:\tPriority scheduling simulation\n");
ps_stream = fopen("PriorityScheduling.xls","a");
fprintf(ps_stream,"Process ID\tEnd Time\tWaiting Time\tTurnaround time\n\n
");
/* Duplicate process list */
tmpsrc = proc;
copy = tmp = NULL;
while (tmpsrc != NULL) {
if (copy == NULL) {
copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
tmp = copy;
} else {
tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priorit
y);
tmp = tmp->next;
}
tmpsrc = tmpsrc->next;
}
/* Main routine */
time = 0;
while (copy != NULL) {
/* Find the next job */
beforehighest = NULL;
highest = copy->priority;
tmp = copy->next;
tmpsrc = copy;
while (tmp != NULL) {
if (tmp->priority < highest) {
highest = tmp->priority;
beforehighest = tmpsrc;
}
tmpsrc = tmp;
tmp = tmp->next;
}
/* Process job and remove from copy of process list */
if (beforehighest == NULL) {
/* Handle first job is highest priority case */
start = time;
time += copy->burst;
end = time;
printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", c
opy->pid, time, start, end);
/* Write the data to a file */
fprintf(ps_stream, "%d \t %d \t %d \t %d \n",copy->pid,time,st
art,end);
tmpsrc = copy->next;
free(copy);
copy = tmpsrc;
}
else {
/* Handle first job is not highest priority case */
tmp = beforehighest->next;
start = time;
time += tmp->burst;
end = time;
printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", t
mp->pid, time, start, end);
/* Write the data to a file */
fprintf(ps_stream, "%d\t%d\t%d\t%d\n",copy->pid,time,start,end
);
beforehighest->next = tmp->next;
free(tmp);
}
}
close_errorPS = fclose (ps_stream);
if (close_errorPS != 0)
{
printf ("File could not be closed.\n");
}
else
{
printf ("File closed.\n");
}
printf("END:\tPriority scheduling simulation\n\n");
}

/* Round-Robin scheduling simulation */


void rr (struct process *proc, int quantum) {
FILE *rr_stream;
//int timeToComplete;
int close_errorRR;
int jobsremain, passes;
struct process *copy, *tmpsrc, *tmp, *slot ;
printf("BEGIN:\tRound-Robin scheduling simulation (Quantum: %d)\n", quantum)
;
/* Open excel file to hold the results of the rr algorithm */
rr_stream = fopen("RoundRobin.xls","a");
fprintf(rr_stream,"Process ID\tWorking\tWaiting\tTurnaround time\n\n");
/* Duplicate process list */
tmpsrc = proc;
copy = tmp = NULL;
while (tmpsrc != NULL) {
if (copy == NULL) {
copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
tmp = copy;
} else {
tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priorit
y);
tmp = tmp->next;
}
tmpsrc = tmpsrc->next;
}
/* Main routine */
jobsremain = 1;
slot = NULL;
while (jobsremain) {
jobsremain = 0;
/* Pick next working slot */
if (slot == NULL) {
slot = copy;
jobsremain = 1;
} else {
passes = 0;
do {
if (slot->next == NULL) {
passes++;
slot = copy;
} else {
slot = slot->next;
}
} while (passes <= 2 && slot->burst == slot->working);
if (passes <= 2) {
jobsremain = 1;
}
}
/* Perform a cycle */
tmp = copy;
while (tmp != NULL) {
if (tmp->burst > tmp->working) {
if (tmp == slot) {
tmp->working += quantum;
} else {
tmp->waiting += quantum;
}
}
tmp = tmp->next;
}
}
/* Display statistics and clean up copy */
tmp = copy;
while (tmp != NULL) {
printf("Process: %d\tWorking: %d\tWaiting: %d\tTurnaround: %d\n", tmp->p
id, tmp->working, tmp->waiting, tmp->working + tmp->waiting);
/* Write results to text file */
fprintf(rr_stream, "%d\t%d\t%d\t%d\n",tmp->pid,tmp->working,tmp->wai
ting,tmp->working+tmp->waiting);
tmpsrc = tmp;
tmp = tmp->next;
free(tmpsrc);
}
printf("END:\tRR scheduling simulation\n\n");
close_errorRR = fclose (rr_stream);
if (close_errorRR != 0)
{
printf ("File could not be closed.\n");
}
else
{
printf ("File closed.\n");
}
}

/* Shortest Job First scheduling simulation */


void sjf (struct process *proc) {
FILE *sjf_stream;
int timeToComplete,timeComplete;
int close_errorSJB;
int time, start, end, shortest;
struct process *copy, *tmpsrc, *tmp, *beforeshortest;
printf("BEGIN:\tShortest Job First scheduling simulation\n");
/* Open excel file to hold the results of the Sjb algorithm */
sjf_stream = fopen("shortestJobFirst.xls","a");
fprintf(sjf_stream,"Process ID\tEnd Time\tWaiting Time\tTurnaround time\tC
omplete\n\n");
/* Duplicate process list */
tmpsrc = proc;
copy = tmp = NULL;
while (tmpsrc != NULL) {

if (copy == NULL) {
copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
tmp = copy;
} else {
tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priorit
y);
tmp = tmp->next;
}
tmpsrc = tmpsrc->next;
}
/* Main routine */
time = 0;
while (copy != NULL) {
/* Find the next job */
beforeshortest = NULL;
shortest = copy->burst;
tmp = copy->next;
tmpsrc = copy;
while (tmp != NULL) {
if (tmp->burst < shortest) {
shortest = tmp->burst;
beforeshortest = tmpsrc;
}
tmpsrc = tmp;
tmp = tmp->next;
}
/* Process job and remove from copy of process list */
if (beforeshortest == NULL) {
/* Handle first job is shortest case */
start = time;
time += copy->burst;
end = time;
timeToComplete=time - start;
printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", c
opy->pid, time, start, end);
/* Write data to a file */
fprintf(sjf_stream, "%d \t %d \t %d \t %d \t %d \n",copy->pid,st
art,time,end,timeToComplete);

tmpsrc = copy;
copy = copy->next;
free(tmpsrc);
} else {
/* Handle first job is not shortest case */
tmp = beforeshortest->next;
start = time;
time += tmp->burst;
end = time;
timeComplete = time - start;
printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", t
mp->pid, time, start, end);
/* Write data to the sjb file */
fprintf(sjf_stream, "%d \t %d \t %d \t %d \t %d \n",tmp->pid,s
tart,time,end,timeComplete);

beforeshortest->next = tmp->next;
free(tmp);
}
}
printf("END:\tShortest Job First scheduling simulation\n\n");
/*Close the file */
close_errorSJB = fclose (sjf_stream);
if (close_errorSJB != 0)
{
printf ("File could not be closed.\n");
}
else
{
printf ("File closed.\n");
}
}

You might also like