Skip to main content
SJF and SRTF combine with gantt chart Code in C
Code : #include<stdio.h>
#include<conio.h>
#define
max 100
int
i,j,n,t,i2[max], p[max],bt[max],wt[max],tat[max],rt[max],at[max];
int
endTime,smallest,processGantt[100];
int
remain=0,time,sum_wait=0,sum_turnaround=0;
float awt = 0 , atat = 0 ;
void
main ()
{
system("cls");
printf("\n\n SJF STARTING ");
printf("Enter the process = ");
scanf("%d",&n);
printf("Enter the process numbers =
\n");
for (i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
printf("Enter the Burst time =
\n");
for (i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
// Appling
sorting algo
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (bt[j]>bt[i]) //Ville
{
t=bt[j];
bt[j]=bt[i];
bt[i]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}
printf("Process \t Burst time \t
Waiting time \t Turn around time\n");
for (i=0;i<n;i++)
{
wt[i]=0;
tat[i]=0;
for (j=0;j<i;j++)
{
wt[i] = wt[i] + bt[j];
}
tat[i] = wt [i] + bt [i] ;
awt = awt + wt [i];
atat = atat + tat [i];
printf(" %d\t\t %d\t\t
%d\t\t
%d\n",p[i],bt[i],wt[i],tat[i]);
}
awt= awt/n;
atat=atat/n;
printf("Avrage waiting time = %f
\n",awt);
printf("Avrage Turning time = %f
\n",atat);
printf("\n\n gantta chart\n");
chart();
getch();
printf("\n\n");
printf("SRTF START Input burst and
arrival time\n\n");
STRF();
}
STRF
()
{
{
for(i=0;i<n;i++)
{
printf("Enter
arrival time for Process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter
burst time for Process P%d : ",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("\n\nProcess\t|Turnaround
Time| Waiting Time\n\n");
rt[9]=9999;
for(time=0;remain!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time
&& rt[i]<rt[smallest] && rt[i]>0)
{
processGantt[time]=i;
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain++;
endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-bt[smallest]-at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
}
printf("\n\nAverage
waiting time = %f\n",sum_wait*1.0/n);
printf("Average
Turnaround time = %f\n\n",sum_turnaround*1.0/5);
printf("Gennta
chart \n\n");
for(i=0;i<=time;i++)
{
printf("%d->P%d
",i,processGantt[i]+1);
}
}
getch();
}
chart()
{
//-------------------------------------------------------------------------
// printing top bar
printf(" ");
for(i=0; i<n; i++) {
for(j=0; j<p[i],bt[j]; j++)
printf("--");
printf(" ");
}
printf("\n|");
// middle position
for(i=0; i<n; i++)
{
for(j=0; j<p[i],bt[j]; j++)
printf(" ");
printf("p%d",p[i]);
for(j=0; j<p[i],bt[j]; j++)
printf(" ");
printf("|");
}
printf("\n ");
//
bottom bar
for(i=0; i<n; i++) {
for(j=0; j<p[i],bt[j]; j++)
printf("--");
printf(" ");
}
printf("\n");
//
printing waiting time
printf("\n0\t");
int minus = 0;
int last =
p[i] + bt[i]+( n== 1 ? 0 : p[n-1],wt[i]);
int waiting_time = wt[i];
int burst_time = bt[i];
for(i=0; i<n; i++) {
if(p[i],waiting_time) printf("
");
printf("%d\t",wt[i]+bt[i]);
}
}
Comments
Post a Comment