Shortest Job First Scheduling
Shortest job first scheduling means pick the job that will take the less time to complete. Shortest job first scheduling picks the shortest job in terms of burst time and places it on the CPU. Shortest job first having minimum average waiting time among all the scheduling algorithms. SJF scheduling is simple to implement in the batch operating system because in this CPU time is known in advance.
Shortest job first scheduling can be used in both non preemptive and preemptive mode.
(1) Non Preemptive
Once the CPU cycle is allocated to a process, it cannot be preempted until the current process reaches a waiting state or terminated.
(2) Preemptive
Jobs are moved into ready queue. Processes which have shortest burst time begins its execution first. When process with shorter burst time arrives, then the current process stops and other process which having shorter burst time is allocated with the CPU first.
Example (1)
Non Preemptive SJF
Suppose there are four processes are as below.
Process Name | Arrival Time | Burst Time |
Process 1 | 0 | 8 |
Process 2 | 2 | 3 |
Process 3 | 4 | 2 |
Process 4 | 5 | 4 |
Step (1)
Arrival time are important, since there is 1 process available at time 0, So process 1 is by default shortest process.
Step (2)
At time 2, process 2 arrives but process 1 is already running. So process 2 has to wait until process 1 finishes.
Step (3)
At time 3, Process 3 arrives that is shortest but process 1 has still not finished.
Step (4)
At time 4, Process 4 arrives but process 3 is the shortest. When process 1 finishes, then process 3 will run.
Step (5)
At time 5, When process 3 finishes, process 2 will run .
Step (6)
After process 2 finishes, process 4 will run.
Gantt Chart
Process 1 | Process 3 | Process 2 | Process 4 |
0 8 | 10 | 13 | 17 |
Waiting Time
Waiting time for a process can be measured by taking its finish time and subtracting burst time and arrival time.
Waiting Time = Finish time - Burst time - Arrival time
Process Name | Arrival Time | Burst Time | Finish Time | Waiting Time |
Process 1 | 0 | 8 | 8 | 0 |
Process 2 | 2 | 3 | 13 | 8 |
Process 3 | 4 | 2 | 10 | 4 |
Process 4 | 5 | 4 | 17 | 8 |
Waiting time for process
Process 1
Waiting time = 8-8-0
Waiting time=0
Process 2
Waiting Time= 13-3-2
Waiting Time=8
process3
Waiting Time=10-2-4
Waiting Time=4
Process4
Waiting Time=17-4-5
Waiting Time=8
Average Waiting Time
Average waiting time = (0+8+4+8)/4
Average waiting time= 20/4
Average waiting time= 5
(2) Preemptive SJF
There are four processes as below.
Process Name | Arrival Time | Burst Time |
Process 1 | 0 | 6 |
Process 2 | 2 | 4 |
Process 3 | 4 | 2 |
Process 4 | 5 | 4 |
Step (1)
Arrival time are important , since there is only one process available at time 0, that is process 1. Process i is by default the shortest process.
Step (2)
At time 2 ,process 2 arrives but process 1 is already running. There is a preemption, so process 1 is removed from the CPU and replaced with process 2.
Step (3)
At time 4, Process 3 arrives that is the shortest process, so it replace process 2 on CPU.
Step (4)
At time 6, process 3 finishes and process 4 arrives. Now process 2 is again shortest and it execute until the finish.
Step (5)
At time 8, process 4 is shortest and run until time 11. Process 1 which is the only process left is the shortest process and execute until the finishes.
Gantt Chart
Process 1 | Process 2 | Process3 | Process 2 | Process4 | Process 1 |
0 2 | 4 | 6 | 8 | 12 | 17 |
Waiting Time
Waiting Time = Finish time - Burst time - Arrival time
Process Name | Arrival Time | Burst Time | Finish Time | Waiting Time |
Process 1 | 0 | 6 | 17 | 11 |
Process 2 | 2 | 4 | 8 | 2 |
Process 3 | 4 | 2 | 6 | 0 |
Process 4 | 5 | 4 | 12 | 3 |
Waiting time for process
Process 1
Waiting time = 17-6-0
Waiting time=11
Process 2
Waiting Time= 8-4-2
Waiting Time=2
process3
Waiting Time=6-2-4
Waiting Time=0
Process4
Waiting Time=12-4-5
Waiting Time=3
Average Waiting Time
Average waiting time = (11+2+0+3)/4
Average waiting time= 16/4
Average waiting time= 4
Comments
Post a Comment
If you have any doubts, please let me know