Originally Posted by
CommonTater
Post your code HERE ... don't be making us go get it... cause most of us won't.
My apologies, I didn't think clicking a single link would be a problem. I wanted to keep the post as uncluttered as possible. As requested though, here's my code.
edit, fixed formatting
Code:
//Shortest Job First Scheduling
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*Define Structure with Variables*/
typedef struct
{
char pid[10]; /* Process ID */
int arr; /* Arrival */
int brt; /* Burst */
int rmt; /* Runtime */
int wt; /* Wait Time */
int tat; /* Total TIme */
int active; /* Active Process */
int finish; /* Finish */
}prc;
/*Main Function*/
int main ()
{
/*Define Variables*/
int i, j, l, lq; /* Count and temp varialbes */
int startp, totalt, endt; /* Start Process, Total Time, End Time varialbes */
int num; /* Number of Processes varialbe */
int startt; /* Start Time */
int remaint, tmp, tmp1; /* Remaining Time and Temps */
int fakep = 9999; /* Idle CPU Time Check */
int pr[100], cpr[100], que[100];/* process, current process, and que */
prc p[100]; /* Structure */
/*Obtain User Input*/
printf("Enter the number of processes: ");
scanf("%d", &num);
/*Init Start and Total Time*/
startt = 9999; totalt = 0;
/*Obtain User Input based on the number of processes entered*/
for (i=0;i<num;i++)
{
printf("Enter the Process Name, Arrival Time, and Burst Time: ");
scanf("%s%d%d", &p[i].pid, &p[i].arr, &p[i].brt);
p[i].rmt = p[i].brt;
p[i].wt = 0; p[i].tat = 0; p[i].active = 0; p[i].finish = 0;
/*Check for the lowest arrival time to determine the start time*/
if(p[i].arr < startt)
{
startt = p[i].arr;
startp = i;
}//end if
/*Calculate the Total time by adding all of the burst times*/
totalt += p[i].brt;
}//end for
/*Calculate the end time by adding the start and total times together*/
endt = startt + totalt;
//printf("After Input \n");
/***** SJF ALGORITHM *****/
lq = 0;
for(i=0;i<startt;i++) cpr[i] = fakep;
l = i;
cpr[l] = startp;
p[cpr[l]].active = 1;
while (lq >= 0)
{
for(i=0;i<num;i++)
{
if((p[i].active==0)&&(p[i].finish==0)&&(p[i].arr==l))
{
if(lq == 0)
{
que[lq] = i;
}//end if
else
{
tmp = i;
for(j=0;j>lq;j++)
{
if (p[tmp].brt > p[que[j]].brt)
{
tmp1 = tmp;
tmp = que[j];
que[j] = tmp1;
}//end if
}//end for
que[lq] = tmp;
}//end else
lq++;
}//end if
}//end for
remaint = p[cpr[l]].rmt - 1;
l++;
if(remaint > 0)
{
cpr[l] = cpr[l-1];
p[cpr[l]].rmt = remaint;
}//end if
else
{
lq--;
if(lq >= 0)
{
cpr[l] = que[lq];
p[cpr[l-1]].finish = 1;
p[cpr[l]].active = 1;
}//end if
}//end else
}//end while
/*Print the Gantt Chart*/
printf("\nGantt Chart:\n");
// Run for i less than endtime plus 1, print the timeline
for (i=0; i<endt+1;i++)
printf("%2d ",i);
printf("\n");
// Run for i less than end time
for (i=0; i<endt;i++)
{
//print a space for idle cpu time
if(cpr[i] == fakep)
printf(" ");
else
//print the pid for the process
printf(" %s", p[cpr[i]].pid);
}//end for
printf("\n");
}//end main