You are on page 1of 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 FCFS algorithm. The program should accept arrival time and burst time as
input.

$vim 2.c

#include<stdio.h>
typedef struct proc
{ int at,bt,pr;
}P; /* Structure to Hold Information of Process */
/*at : Arrival time,bt : Burst Time, pr : Process NO */

/* Function Definition To Swap Two structure Variables*/


void swap(P *p,P *q)
{ P temp;
temp.at=p->at;
temp.bt=p->bt;
temp.pr=p->pr;

p->at=q->at;
p->bt=q->bt;
p->pr=q->pr;

q->at=temp.at;
q->bt=temp.bt;
q->pr=temp.pr;
}

/* Function Definition To Sort Process with Arrival time*/


void sort_process_by_at(int n,P p[])
{ int i,j;
for(i=0;i<n-1;i++)
{ for(j=i+1;j<n;j++)
{ if(p[i].at>p[j].at)
{
swap(&p[i],&p[j]);
}
}
}
}
/* Main Function */
int main()
{ /* Declaration Section*/
int wt[15],tat[15],i,n;
float awt=0,atat=0;
P proc[15];
/* wt : Array to hold Waiting tome, tat : array to hold turn arround time*/
/*n : No of process, awt: average Waiting time , atat: avrage turn eround time*/
/*proc : Array of Structure to hold Process Information */
printf("\n\t\tEnter the No of Process(max 15) : ");
scanf("%d",&n);
printf("\n\t\tEnter the Arrival time of Process \n");
for(i=0;i<n;i++)
{ printf("\tProcess P%d : ",i);
scanf("%d",&proc[i].at); // read arrival time
proc[i].pr=i; // assign process no
}
printf("\n\t\tEnter the Burst time of Process \n");
for(i=0;i<n;i++)
{ printf("\tProcess P%d : ",i);
scanf("%d",&proc[i].bt);
}
sort_process_by_at(n,proc); //Call Sort Function To Sort Process by Arrival time
wt[0]=0;
for(i=1;i<n;i++)
wt[i]=wt[i-1]+proc[i-1].bt; // Calculate Waiting time

for(i=0;i<n;i++)
{ wt[i]=wt[i]-proc[i].at; //Substract Arrival time
awt+=wt[i];
}
awt=awt*1.0/n; // Average : Divide sum by no of process

for(i=0;i<n;i++)
{ tat[i]=wt[i]+proc[i].bt; // Turn Around Time , i.e TAT=WT+BT
atat+=tat[i];
}
atat=atat*1.0/n; // Average : Divide sum by no of process

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",proc[i].pr,proc[i].at,proc[i].bt,wt[i],tat[i]); // Print
Information
printf("\n\t_______________________________________\n");
printf("\tAverage Waiting Time = %f\n",awt);
printf("\tAverage Turn Around Time =%f\n",atat);
return 0;
}

/*===========================Out_put=================================*/
$cc 2.c
$ ./a.out

Enter the No of Process(max 15) : 4

Enter the Arrival time of Process


Process P0 : 3
Process P1 : 6
Process P2 : 0
Process P3 : 4

Enter the Burst time of Process


Process P0 : 5
Process P1 : 4
Process P2 : 3
Process P3 : 7

Proc A_T B_T W_T TA_T


______________________________________

2 0 3 0 3
0 3 5 0 5
3 4 7 4 11
1 6 4 9 13

_______________________________________

Average Waiting Time = 3.250000

Average Turn Around Time =8.000000

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

You might also like