SIMULATION OF SHORTEST JOB FIRST(SJF) PROCESS SCHEDULING USING C PROGRAM

AIM:
To write a c program in UNIX environment to implement the following scheduling discipline
Shortest Job First (SJF)
SJF:
SJF define as non preemptive scheduling discipline.
In these case waiting job in smallest estimated run time to complete in the next.
SJF decrease average waiting time compare to FIFO.


ALGORITHM: OPERATING SYSTEM (OS) LAB IN LINUX ENVIRONMENT



· 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>
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);
return 0;
}
CONCLUSION: Thus the c program for SJF scheduling discipline was written and executed successfully


Related post