Today we are going
write a c- program for one of the process scheduling techniques we have learnt.
So let us write the program for the first technique First Come First Serve.
This technique computes the process depending on, which
process comes first. It doesn’t depend upon the burst time of the process. In
order to write the program first we need to know about the inputs, process to
be done and then the output. Input of this program is process name and burst
time of that process. In order to store those inputs we shall use arrays for
process name and burst time of processes. We need to compute the waiting time
of each process so, let us use an array wait. We need to calculate total
waiting time of all the process and need to find out average waiting time.
Therefore total set of variables required are:
int n,i,j,tot=0,wait[100],burst[100];
char process[100];
float avg;
The variables n is used to get the size of total number of
processes. Variables i,j for loop controlling. The procedure after getting
inputs is we need calculate the waiting time of each process and add them to
total waiting time. Waiting time of each process is given by formula: wait[i]=wait[i-1]+burst[i-1] . For this
purpose we shall use “for” loop as follows:
for(i=2;i<=n;i++)
{
wait[i]=wait[i-1]+burst[i-1];
tot=tot+wait[i];
}
Here we finding total waiting time in the same for loop. Avg
waiting time is calculated by dividing total waiting time by total number of
processes .Hence total program is as follows:
#include<stdio.h>
#include<conio.h>
void main()
{
int
n,i,j,t,tot=0,wait[100],burst[100];
char process[100];
float avg;
clrscr();
printf("\nEnter number of processes:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter name of %d
process:",i);
scanf("%s",process[i]);
printf("\nEnter burst time of %d process:",i);
scanf("%d",&burst[i]);
}
wait[1]=0;
for(i=2;i<=n;i++)
{
wait[i]=wait[i-1]+burst[i-1];
tot=tot+wait[i];
}
avg=(float)tot/(float)n;
printf("\nTotal waiting time=%d",tot);
printf("\nAverage waiting time=%f",avg);
getch();
}
No comments:
Post a Comment