Вы находитесь на странице: 1из 3

/* SHARANABASU G ANGADI */

/* Information Science & Engineering */


/* BEC Bagalkot , (Karnataka) INDIA. */
Write a C/C++ program to compute average waiting time and average
turnaround time for SJF algorithm. The program should accept arrival time and
burst time as input.

$vim 3.c

#include<stdio.h>
typedef struct proc
{ int at,bt,status;
int proc,est;
}P;
P p[15];
int arr[15];
void swap(P *p,P *q)
{ P temp;
temp.at=p->at;
temp.bt=p->bt;
temp.status=p->status;
temp.proc=p->proc;
temp.est=p->est;

p->at=q->at;
p->bt=q->bt;
p->status=q->status;
p->proc=q->proc;
p->est=q->est;

q->at=temp.at;
q->bt=temp.bt;
q->status=temp.status;
q->proc=temp.proc;
q->est=temp.est;
}

void sort_process_by_bt(int n)
{ int i,j;
for(i=0;i<n-1;i++)
{ for(j=i+1;j<n;j++)
{ if(p[i].bt>p[j].bt)
{
swap(&p[i],&p[j]);
}
}
}
}

void cal_proc(int n)
{ int ct=0,i,j,k=0;
up:
for(j=0;j<n;j++)
for(i=0;i<n;i++)
{ if(p[i].status==0 && p[i].at<=ct)
{ p[i].status=1;
p[i].est=ct;
ct+=p[i].bt;
arr[k++]=p[i].proc;
goto up;
}
}
}

int main()
{ int n,i,wt[15],tat[15];
float awt,atat;
printf("\n\n\t\tEnter the no of Process (max 15) : ");
scanf("%d",&n);
printf("\n\n\t\tEnter the arrivel time of process \n");
for(i=0;i<n;i++)
{ printf("\t\tprocess P%d : ",i);
scanf("%d",&p[i].at);
}

printf("\n\n\t\tEnter the Burst time of process \n");


for(i=0;i<n;i++)
{ printf("\t\tprocess P%d : ",i);
scanf("%d",&p[i].bt);
p[i].status=0;
p[i].proc=i;
}
sort_process_by_bt(n);
cal_proc(n);
for(i=0;i<n;i++)
{ wt[i]=p[i].est-p[i].at;
awt+=wt[i];
tat[i]=(p[i].est-p[i].at)+p[i].bt;
atat+=tat[i];
}
awt/=n*1.0;
atat/=n*1.0;
printf("\n\tProc A_T B_T W_T TA_T\t");
printf("\n\t______________________________________\n\n");
for(i=0;i<n;i++)
printf("\t%d\t%d\t%d\t%d\t%d\n",p[i].proc,p[i].at,p[i].bt,wt[i],tat[i]);
printf("\n\t_______________________________________\n");
printf("\n\n\t\tAverage Waiting time = %f\n",awt);
printf("\n\n\t\tAverage Turn around time = %f\n",atat);
return 0;
}
/
*===========================Out_put===================================
===*/
$cc 3.c
$./a.out
Enter the no of Process (max 15) : 4

Enter the arrivel time of process


process P0 : 4
process P1 : 0
process P2 : 7
process P3 : 5

Enter the Burst time of process


process P0 : 5
process P1 : 4
process P2 : 3
process P3 : 6

Proc A_T B_T W_T TA_T


______________________________________
2 7 3 2 5
1 0 4 0 4
0 4 5 0 5
3 5 6 7 13
_______________________________________

Average Waiting time = 2.250000

Average Turn around time = 6.750000


/
*====================================================================
===*/