The operating system is responsible for scheduling a job for the processor. There are various types of algorithms used for scheduling a job to the CPU for executing these processes. Shortest Job First (SJF) is a type of disk scheduling algorithm in the operating system in which the processor executes the job first that has the smallest execution time. In the shortest Job First algorithm, the processes are scheduled according to the burst time of these processes.
Scope
In this article, we are going to cover the following topics:
The 'Disk Structure in OS' is made in such a way that there are many layers of storage blocks on the disk. And when we need to access these data from disk, we initialize a 'request' to the system to give us the required data. These requests are done on a large scale. So, there is a large number of input and output requests coming to the disk. The operating system manages the timetable of all these requests in a proper algorithm. This is called as "Disk Scheduling Algorithm in OS".
This algorithm helps OS to maintain an efficient manner in which input-output requests can be managed to execute a process. There are various types of algorithms that an operating system uses for scheduling a process to the CPU. These algorithms have their own advantages and disadvantages and features. All the scheduling algorithms are useful in different conditions. The SJF (Shortest Job First) scheduling algorithm in which the CPU executes the job first has the shortest execution time. Also, the burst time is an important factor in SJF scheduling.
Burst time can be defined as the time required by the process to get executed in a millisecond. The process that has the lowest burst time of all the currently available processes, will get scheduled by the SJF algorithm and it will get executed first. Also, it is not an easy task to predict the burst of a process so, the implementation of the SJF algorithm is also not easy.
Following are the characteristics of (Shortest Job First) SJF scheduling program in C.
Algorithm
The algorithm of Shortest Job First is as follows:
Types of SJF
(Shortest Job First) SJF scheduling program in C is of two types. The first one is Pre-emptive SJF and the second one is Non-Preemptive SJF. Let us see them in detail.
Pre-emptive SJF
Pre-emptive SJF is a type of scheduling algorithm in which jobs are inserted into the ready queue as soon as they arrive at the disk. The process having the shortest burst time starts to get executed first, even if the shortest burst time arrives the current burst is removed from the execution process and the job having the shortest burst time gets allocated in the CPU cycle. Suppose there are five processes in a queue and their burst time and arrival time are given below in the table.
The processes will get executed in the following steps:
Now, the average waiting time will be calculated as:
Wait time
P4= 0-0=0
P1= (3-2) + 6 =7
P2= 5-5 = 0
P5= 4-4+2 =2
P3= 15-1 = 14
The average waiting time is: 0+7+0+2+14/5 = 23/5 =4.6
Non-Preemptive SJF
In this type of scheduling algorithm, if one process is allocated for the CPU, the process sticks to the CPU until the process becomes in a waiting state or gets executed. Suppose there are 5 processes shown in the below table and they have their own burst time and average time.
Given below are the steps through which the processes get executed:
Now the average waiting time will be calculated as follows:
Wait time
P4= 0-0=0
P1= 3-2=1
P2= 9-5=4
P5= 11-4=7
P3= 15-1=14
Average Waiting Time= 0+1+4+7+14/5 = 26/5 = 5.2
There are some factors that are responsible for calculating the SJF algorithm. Let us see them in brief.
BURST Time
The burst time is defined as the difference between the completion time and the waiting time for a particular process.
Average waiting time
The waiting time is defined as the difference between the turnaround time and burst time of a process.
Average turnaround time
The Turn around time is defined as the time taken by a process to complete after the arrival time. Or simply we can say that turnaround time is the difference between the arrival and completion time of a process.
Let's see an implementation of Non-Preemptive SJF Scheduling in C language:
#include<stdio.h>
int main() {
int time, burst_time[10], at[10], sum_burst_time = 0, smallest, n, i;
int sumt = 0, sumw = 0;
printf("enter the no of processes : ");
scanf("%d", & n);
for (i = 0; i < n; i++) {
printf("the arrival time for process P%d : ", i + 1);
scanf("%d", & at[i]);
printf("the burst time for process P%d : ", i + 1);
scanf("%d", & burst_time[i]);
sum_burst_time += burst_time[i];
}
burst_time[9] = 9999;
for (time = 0; time < sum_burst_time;) {
smallest = 9;
for (i = 0; i < n; i++) {
if (at[i] <= time && burst_time[i] > 0 && burst_time[i] < burst_time[smallest])
smallest = i;
}
printf("P[%d]\t|\t%d\t|\t%d\n", smallest + 1, time + burst_time[smallest] - at[smallest], time - at[smallest]);
sumt += time + burst_time[smallest] - at[smallest];
sumw += time - at[smallest];
time += burst_time[smallest];
burst_time[smallest] = 0;
}
printf("\n\n average waiting time = %f", sumw * 1.0 / n);
printf("\n\n average turnaround time = %f", sumt * 1.0 / n);
return 0;
}
Output:
enter the no of processes: 2
the arrival time for process P1: 10
the burst time for process P1: 5
the arrival time for process P2: 6
the burst time for process P2 : 3
P[10] | -22765 | -32764
the average waiting time = -16382.000000
the average turnaround time = -11382.500000
Code for Pre-emptive SJF Scheduling
Let's see an implementation of Pre-emptive SJF Scheduling in C language:
#include<stdio.h>
int main()
{
int burst_time[20],p[20],waiting_time[20],tat[20],i,j,n,total=0,pos,temp;
float avg_waiting_time,avg_tat;
printf("please enter number of process: ");
scanf("%d",&n);
printf("\n enter the Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&burst_time[i]);
p[i]=i+1;
}
// from here, burst times sorted
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(burst_time[j]<burst_time[pos])
pos=j;
}
temp=burst_time[i];
burst_time[i]=burst_time[pos];
burst_time[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
waiting_time[0]=0;
for(i=1;i<n;i++)
{
waiting_time[i]=0;
for(j=0;j<i;j++)
waiting_time[i]+=burst_time[j];
total+=waiting_time[i];
}
avg_waiting_time=(float)total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=burst_time[i]+waiting_time[i];
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],burst_time[i],waiting_time[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\n\n the average Waiting Time=%f",avg_waiting_time);
printf("\n the average Turnaround Time=%f\n",avg_tat);
}
Output:
please enter the number of processes: 2
enter the Burst Time:
p1:5
p2:6
the average Waiting Time=2.500000
the average Turnaround Time=8.000000
Advantages of SJF
The following are the advantages of SJF scheduling:
Disadvantages of SJF
The following are the disadvantages of the SJF algorithm:
To learn more about various scheduling algorithm in operating system click here Scheduling Algorithm in OS
Conclusion
10 videos|99 docs|33 tests
|
|
Explore Courses for Computer Science Engineering (CSE) exam
|