Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

SIMULATION OF SHORTEST JOB

FIRST(SJF) PROCESS SCHEDULING USING


C PROGRAM

AIM:
To write a c program to implement the following scheduling discipline

Shortest Job First (SJF)


SJF:
In this case the job having the shortest burst time is encountered first.

ALGORITHM:

· Get the number of process from the user.

· Get also CPU service time for each process from the user.

· Sort the CPU time in ascending order.

· Now for each process the waiting time is equivalent to CPU time of previous process.

· The ratio of waiting time of all the process to the number of process will give the average
waiting time.

· Display the result.

PROGRAM SOURCE CODE

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k=0,ptime[25],n,s=0,I,sum=0;
char name[25][25];
int t,p,time[10],pas[10];
float avg;
printf ("enter the no. of process: \t");
scanf ("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the name for processes: \t");
printf("%d \t",i+1);
scanf("%s",name[i]);
}
printf("\n \n");

for(i=0;i<n;i++)
{
printf("enter the process time: \t");
printf("%s \t",name[i]);
scanf("%d",&ptime[i]);
}
printf("\n \n");
printf("\n process - name \t process - time \n");
for(i=0;i<n;i++)
{
printf("\t %s \t \t %d \n",name[i],ptime[i]);
}
printf("\n \n SJF SCHEDULING \n \n");
for(i=0;i<n;i++)
{
pas[i]=i;
time[i]=ptime[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(time[i]>time[j])
{
p=time[i];
time[i]=time[j];
time[j]=p;
t=pas[i];
pas[i]=pas[j];
pas[j]=t;
}
}
}
for(i=0;i<n;i++)
{
printf("process %s from %d to %d \n",name[pas[i]],k,(k+time[i]));
k+=time[i];
}
for(i=0;i<(n-1);i++)
{
s+=time[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n\n average waiting time: \t");
printf("%2fmsec",avg);
sum=avg=s=0;
for(i=0;i<n;i++)
{
s+=ptime[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n turn around time is \t");
printf("%2fmsec",avg);
getch();
}

CONCLUSION:
Thus the c program for SJF scheduling discipline was written and executed successfully.

RESULT:

You might also like