Professional Documents
Culture Documents
SJF Cpu Scheduling Algorithm
SJF Cpu Scheduling Algorithm
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,bt[10],st[10],et[10],wt[10],temp,tot;
/* n=number of jobs bt=burst time st=starting time wt=waiting time*/
/*et=end time*/
float avg;
clrscr();
printf("ENTER THE NO.OF JOBS");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n \n ENTER %d PROCESS BURST TIME",i);
scanf("%d",&bt[i]);
}
/* SWAPPING BURST TIMES*/
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
}
}
/* IN CASE OF FIRST JOB*/
if(i==1)
{
st[1]=0;
et[1]=bt[1];
wt[1]=0;
}
/* FOR REMAINING CASES*/
else
{
st[i]=et[i-1];
et[i]=st[i]+bt[i];
wt[i]=st[i];
}
}
AIM:
To write a LINUX/UNIX C Program for the Implementation of Shortest Job First
Scheduling Algorithm in CS1254 - Operating Systems Laboratory.
SOURCE CODE:
#include<stdio.h>
main()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;
float att=0;awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
printf(Enter the number of process:);
scanf(%d,&n);
printf(\n Enter the burst time);
for(i=0;i<n;i++)
{
scanf(%d,&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
{
for(j=1;j<n;j++)
{
if(b[i]>b[j]
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
w[0]=0;
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
awt=aw+w[i]:
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf(\n\t Process \tWaitimgtime \t Turnaroundtime\n);
for(i=0;i<n;i++)
printf(\t p[%d] \t %d\t\t %d\n,p[i].w[i],t[i]);
printf(\n The average waiting time is %t \n,awt);
printf(The average turnaround time is %f\n,att);
return 1;
}
OUTPUT:
[examuser35@localhost Jebastin]$ cc sjf.c
[examuser35@localhost Jebastin]$ ./a.out
Enter the number of process:5
Enter the burst time:5
4
3
2
1
Process
P[4]
P[0]
P[1]
Waiting time
0
1
6
Turnaround time
1
6
10
P[2]
P[3]
10
13
13
15
ae/=n;
printf("\n Average Waiting time is : %f",aw);
printf("\n Average Turnaround time is :%f",ae);
return(0);
}</n;i++)
</b1[k])
</n;k++)
</n;i++)
</n;i++)