(2) Write the program to simulate Non-preemptive Shortest Job First (SJF) -scheduling. The arrival time and first CPU-burst for different n number of processes should be input to the algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly. The output should give Gantt chart, turnaround time and waiting time for each process. Also find the average waiting time and turnaround time..
Solution:
#include<stdio.h>
int findWaitingTime(int bt[], int wt[], int n)
{
int wtavg=0,i;
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1]+2;
wtavg=wtavg+wt[i];
}
return(wtavg);
}
int findTurnAroundTime(int bt[],int wt[], int tat[], int n)
{
int tatavg=0,i;
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
tatavg=tatavg+tat[i];
}
return(tatavg);
}
void sort(int n, int bt[], int p[])
{
int i, j, pos, temp;
for(i=0; i<n; i++)
{
pos=i;
for(j=i+1;j<n; j++ )
{
if(bt[j]<bt[pos])
{
pos=j;
}
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
}
void main()
{
int at[10], bt[10], wt[10], tat[10], p[10],i,n;
int avgwt, avgtat;
printf("\nEnter The Number of Process\n");
scanf("%d",&n);
printf("\nEnter The Burst Time\n");
for(i=0;i<n;i++)
{
p[i]=i;
printf("\nProcess %d:\t",p[i]);
scanf("%d",&bt[i]);
printf("\n");
}
printf("\nBefore Sorting\n");
printf("P|\tBT|\n");
for(i=0;i<n;i++)
{
printf("%d",p[i]);
printf("\t%d\n",bt[i]);
}
sort(n, bt, p);
printf("\nAfter The Non Preemptive SJF\n");
avgwt=findWaitingTime(bt, wt, n)/n;
avgtat=findTurnAroundTime(bt, wt, tat, n)/n;
printf("P|\tBT|\tWT|\tTAT\n");
for(i=0; i<n; i++)
{
printf("%d",p[i]);
printf("\t%d", bt[i]);
printf("\t%d", wt[i]);
printf("\t%d\n", tat[i]);
}
printf("\nGantt Chart\n");
for(i=0; i<n; i++)
{ printf("| P%d ", i);
}
printf("|\n");
for(i=0; i<n; i++)
{ printf("%d ", wt[i]);
}
printf("%d\n",tat[i-1]);
printf("Average Waiting Time= %d", avgwt);
printf("\n");
printf("Avegare Turn Around Time= %d", avgtat);
}
0 Comments:
Post a Comment