You are on page 1of 5

/* 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];
}
}

printf("\n\n BURST TIME \t STARTING TIME \t END TIME \t WAIT TIME\n");


printf("\n ********************************************************\n");
for(i=1;i<=n;i++)
{
printf("\n %5d %15d %15d %15d",bt[i],st[i],et[i],wt[i]);
}
printf("\n ********************************************************\n");
for(i=1,tot=0;i<=n;i++)
tot+=wt[i];
avg=(float)tot/n;
printf("\n\n\n AVERAGE WAITING TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=et[i];
avg=(float)tot/n;
printf("\n\n AVERAGE TURNAROUND TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=st[i];
avg=(float)tot/n;
printf("\n\n AVERAGE RESPONSE TIME=%f",avg);
getch();
}

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

The average waiting time is 6.000000


The average turnaround time is 9.000000
#include
int main()
{
int n,b[12],b1[12],j[12],w[12],e[12];
int i,k,t,t1;
float aw=0,ae=0;
printf("\n Enter the number of Jobs:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the bursttime of Job %d:",i+1);
scanf("%d",&b[i]);
b1[i]=b[i];
j[i]=i;
}
for(i=0;i<n;i++)
for(k=0;k<n;k++)
if(b1[i]<b1[k])
{
t=b1[i];
b1[i]=b1[k];
b1[k]=t;
}
printf("\n Job \t bursttime \t Waitingtime \t Turnaroundtime");
for(i=0;i<n;i++)
{
if(i==0)
{
w[0]=0;
e[0]=b1[0];
}
else
{ w[i]=e[i-1];
e[i]=e[i-1]+b1[i];
}
printf("\n %d \t\t %d \t\t %d \t\t %d",i+1,b1[i],w[i],e[i]);
aw+=w[i];
ae+=e[i];
}
aw/=n;

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++)

You might also like