Shortest Job First Scheduling or SJF allocates the CPU to the process which has the smallest CPU-bursts time.
SJF is also called Shortest Job Next scheduling shortly referred to as SJN. Since Job can be called as a process, SJF is also referred to as Shortest Process Next or SPN scheduling
Suppose, if the CPU bursts of two processes are the same, then we use FCFS scheduling to break the tie.
SJF is used frequently in long term scheduling but it cannot be implemented in short-term scheduling.
SJF can either be a preemptive or nonpreemptive.
Pros of using SJF:
Helps achieve lesser turnaround time
Disadvantages/drawbacks/cons of using SJF:
Jobs or processes that need more time will wait and result in Job starvation or process starvation
Preemptive SJF scheduling is sometime called Shortest remaining time first Scheduling.
Preemptive means a Job can be stopped midway to give CPU time to another Job. In Preemptive SJF, the job which has minimum remaining time will be executed next
For example, consider the set of processes that arrive at time 0 and CPU burst time given in millisecond:
Process | CPU-Burst Time |
---|---|
P1 | 20 |
P2 | 4 |
P3 | 3 |
In the above diagram: 3,7 and 27 are finish times
Process | Order | Arrival Time | CPU Burst | Waiting Time | Turn around time |
---|---|---|---|---|---|
P1 | Job executed Third | 0 | 20 | 7 | 27 |
P2 | Job executed Second | 0 | 4 | 3 | 7 |
P3 | Job executed First because it takes less time | 0 | 3 | 0 | 3 |
Let us say, the processes arrive in the different arrival time, and say, the process is served by using Preemptive SJF Scheduling.
Process | Arrival Time | CPU-Burst Time |
---|---|---|
P1 | 0 | 20 |
P2 | 1 | 4 |
P3 | 3 | 3 |
Now, based on the processes which are in queue at the start:
Process | Arrival Time | CPU Burst | Start Time | Waiting Time = Start Time - Arrival Time | Finish Time | Turnaround time= Finish Time - Arrival Time |
---|---|---|---|---|---|---|
P1 | 0 | 20 | 8 | 8-1=7 | 27 | 27-0=27 |
P2 | 1 | 4 | 1 | 1-1=0 | 5 | 5-1=4 |
P3 | 3 | 3 | 5 | 5-3=2 | 8 | 8-3=5 |
Shortest Job First Scheduling or SJF allocates the CPU to the process which has the smallest CPU-bursts time.
SJF is also called Shortest Job Next scheduling shortly referred to as SJN. Since Job can be called as a process, SJF is also referred to as Shortest Process Next or SPN scheduling
Suppose, if the CPU bursts of two processes are the same, then we use FCFS scheduling to break the tie.
SJF is used frequently in long term scheduling but it cannot be implemented in short-term scheduling.
SJF can either be a preemptive or nonpreemptive.
Pros of using SJF:
Helps achieve lesser turnaround time
Disadvantages/drawbacks/cons of using SJF:
Jobs or processes that need more time will wait and result in Job starvation or process starvation
Preemptive SJF scheduling is sometime called Shortest remaining time first Scheduling.
Preemptive means a Job can be stopped midway to give CPU time to another Job. In Preemptive SJF, the job which has minimum remaining time will be executed next
For example, consider the set of processes that arrive at time 0 and CPU burst time given in millisecond:
Process | CPU-Burst Time |
---|---|
P1 | 20 |
P2 | 4 |
P3 | 3 |
In the above diagram: 3,7 and 27 are finish times
Process | Order | Arrival Time | CPU Burst | Waiting Time | Turn around time |
---|---|---|---|---|---|
P1 | Job executed Third | 0 | 20 | 7 | 27 |
P2 | Job executed Second | 0 | 4 | 3 | 7 |
P3 | Job executed First because it takes less time | 0 | 3 | 0 | 3 |
Let us say, the processes arrive in the different arrival time, and say, the process is served by using Preemptive SJF Scheduling.
Process | Arrival Time | CPU-Burst Time |
---|---|---|
P1 | 0 | 20 |
P2 | 1 | 4 |
P3 | 3 | 3 |
Now, based on the processes which are in queue at the start:
Process | Arrival Time | CPU Burst | Start Time | Waiting Time = Start Time - Arrival Time | Finish Time | Turnaround time= Finish Time - Arrival Time |
---|---|---|---|---|---|---|
P1 | 0 | 20 | 8 | 8-1=7 | 27 | 27-0=27 |
P2 | 1 | 4 | 1 | 1-1=0 | 5 | 5-1=4 |
P3 | 3 | 3 | 5 | 5-3=2 | 8 | 8-3=5 |