Professional Documents
Culture Documents
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);
}
/* 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);
}
/* 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");
}
}
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");
}
}