IMPLEMENTATION OF SJF SCHEDULING
AIM:
To write a C program to perform SJF scheduling in Linux.
ALGORITHM:
1. Stop the program.
2. Read the number of request
3. Read the servicing time for each process
4. Sort the process by servicing time(i.e. the job having least burst time is executed first)
5. Calculate the waiting time for all the process.
6. Calculate the average waiting time.
7. Draw the gantt chart and print the waiting time, average waiting time, turnaround time and average turnaround time
8. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int p[5],b[5],w[5],f[5],n,tb,t[5],i,j,tw,tt,temp,wt,c[10];
float aw,at;
printf("\n Enter the no. of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time:");
scanf("%d",&b[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
tw=w[0]=tt=t[0]=tb=0;
for(i=0;i<n;i++)
{
w[i]=tb;
tb=tb+b[i];
t[i]=b[i]+w[i];
tw=tw+w[i];
tt=tt+t[i];
}
aw=(float)tw/n;
at=(float)tt/n;
for(i=0;i<n;i++)
c[i]=w[i];
c[n]=t[n-1];
printf("\n Process Burst time Waiting time Turnaround time\n");
for(i=0;i<n;i++)
printf("\np%d\t\t%d\t\t%d\t\t%d\n",p[i],b[i],w[i],t[i]);
printf("\nGantt chart\n");
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
for(i=0;i<n;i++)
printf("\t p%d",p[i]);
printf("\n");
for(i=0;i<=n;i++)
printf("\t%d",c[i]);
printf("\n The total waiting time=%d",tw);
printf("\n Total turnaround time is=%d",tt);
printf("\n Average waiting time=%f",aw);
printf("\n Average turnaround time=%f",at);
printf("\n");
}
}
OUTPUT:
[root@localhost ~]# ./p5
Enter the no. of process:4
Enter the burst time:3
Enter the burst time:5
Enter the burst time:6
Enter the burst time:1
Process Burst time Waiting time Turnaround time
P4 1 0 1
P1 3 1 4
P2 5 4 9
P3 6 9 15
Gantt chart
P4 P1 P2 P3
0 1 4 9 15
The total waiting time=14
Total turnaround time is=29
Average waiting time=3.500000
Average turnaround time=7.250000
0 comments:
Post a Comment