Category Archives: Scheduling Algorithms

Shortest-Job-First Scheduling : Non Preemptive

This algorithm associates with each process the length of the process’s next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU bursts of two processes are
the same, FCFS scheduling is used to break the tie.

SJF ( Non – Preemptive ) Implementation in C Lang :

<br />#include<stdio.h>
int main()
{
int i,n,p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
int bt[10],temp,j,at[10],wt[10],tt[10],ta=0,sum=0;
float wavg=0,tavg=0,tsum=0,wsum=0;
printf(" -------Shortest Job First Scheduling ( NP )-------\n");
printf("\nEnter the No. of processes :");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\tEnter the burst time of %d process :",i+1);
scanf(" %d",&bt[i]);
printf("\tEnter the arrival time of %d process :",i+1);
scanf(" %d",&at[i]);
}

/*Sorting According to Arrival Time*/

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(at[i]<at[j])
{
temp=p[j];
p[j]=p[i];
p[i]=temp;
temp=at[j];
at[j]=at[i];
at[i]=temp;
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
}
}
}

/*Arranging the table according to Burst time,
Execution time and Arrival Time
Arrival time <= Execution time
*/

for(j=0;j<n;j++)
{
btime=btime+bt[j];
min=bt[k];
for(i=k;i<n;i++)
{
if (btime>=at[i] && bt[i]<min)
{
temp=p[k];
p[k]=p[i];
p[i]=temp;
temp=at[k];
at[k]=at[i];
at[i]=temp;
temp=bt[k];
bt[k]=bt[i];
bt[i]=temp;
}
}
k++;
}
wt[0]=0;
for(i=1;i<n;i++)
{
sum=sum+bt[i-1];
wt[i]=sum-at[i];
wsum=wsum+wt[i];
}

wavg=(wsum/n);
for(i=0;i<n;i++)
{
ta=ta+bt[i];
tt[i]=ta-at[i];
tsum=tsum+tt[i];
}

tavg=(tsum/n);

printf("************************");
printf("\n RESULT:-");
printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around" );
for(i=0;i<n;i++)
{
printf("\n p%d\t %d\t %d\t\t %d\t\t\t%d",p[i],bt[i],at[i],wt[i],tt[i]);
}

printf("\n\nAVERAGE WAITING TIME : %f",wavg);
printf("\nAVERAGE TURN AROUND TIME : %f",tavg);
return 0;
}

Output :-

Shortest Job first (Non-preemptive)
Shortest Job first (Non-preemptive)

First Come First Served (FCFS) scheduling :-

First come first served algorithm is the simplest scheduling algorithm. The process that requests the
CPU first is allocated the CPU first. The implementation of the FCFS policy is easily managed with a FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. The code for FCFS scheduling is simple to write and understand.

Advantages:-
1. Easy to implement.
2. No starvation


Disadvantages :-
1. Not suitable for time sharing system.
2. Since it is non preemptive, CPU usage can be wasted in some situations.
3.  Throughput is not emphasized.
4. Convoy effect occurs.Even very small process should wait for its turn to come to utilize the CPU.

<br />#include<stdio.h>

int main()
{
int a[10],w[10],b[10],awt=0,tat[10],n,i;
float wsum=0;
float tsum=0;
printf("\t\t --First Come First Serve Scheduling--\n\n");
printf("Enter the number of processes :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Burst time for process %d : ",i+1);
scanf("%d",&b[i]);
printf("Arrival time for process %d : ",i+1);
scanf("%d",&a[i]);
}

printf("\nPROCESS\tBURST TIME\tARRIVAL TIME\n");
for(i=0; i<n; i++)
{
printf("%dt %dt %dtn",i+1,b[i],a[i]);
}
w[0]=0;
for(i=1; i<n; i++)
{
w[i]=b[i-1]-a[i]+w[i-1];
wsum=wsum+w[i];
}
printf("\nTotal Waiting Time : %f\n", wsum);
printf("Average Waiting Time : %f\n", wsum/n);

tat[0]=b[0]-a[0];
tsum=tat[0];
for(i=1; i<n; i++)
{
tat[i]=b[i]-a[i]+tat[i-1];
tsum=tsum+tat[i];
}

printf("\nTotal Turn Around Time : %f",tsum);
printf("\nAverage Turn Around Time : %f",(tsum/n));
}

Output :-

fcfss