Sub topic 3
Shortest Job First and Shortest Remaining Time First
This chapter assumes you know what a CPU scheduler does, can read a Gantt chart, and can compute simple time differences. Each process has a CPU burst, the amount of CPU time it needs before it blocks or finishes. All times below are in arbitrary units.
Shortest Job First (SJF)
SJF runs the ready process with the smallest next CPU burst. Once a process starts, it keeps the CPU until it finishes. This is non-preemptive SJF. It gives the lowest average waiting time when all processes arrive at time 0.
def sjf_finish(bursts):
order = sorted(range(len(bursts)), key=lambda i: bursts[i])
t = 0
finish = [0] * len(bursts)
for i in order:
t += bursts[i]
finish[i] = t
return finish
bursts = [6, 8, 7, 3]
finish = sjf_finish(bursts)
waiting = [f - b for f, b in zip(finish, bursts)]
print(finish) # [9, 24, 16, 3]
print(waiting) # [3, 16, 9, 0]Shortest Remaining Time First (SRTF) is the preemptive form. When a new process arrives with a burst shorter than the running process's remaining time, the CPU switches to the new process.
Example: P1 arrives at 0 with burst 7, P2 at 2 with burst 4, and P3 at 4 with burst 1. P1 runs 0 to 2, then P2 preempts it. P3 preempts P2 at 4 and finishes at 5. P2 finishes at 7, and P1 finishes at 12. So waiting times are P1 = 5, P2 = 1, P3 = 0.
Round Robin and Choosing the Time Quantum
Round Robin gives each ready process one slice of CPU time, called the time quantum. If the process is not done, it goes to the back of the queue. A small quantum gives quick responses but more context switches. A large quantum behaves like FCFS.
Priority Scheduling, Starvation, and Aging
Priority scheduling runs the highest-priority ready process. A low-priority process can wait forever if higher-priority work keeps arriving. This is starvation. Aging fixes it by raising a waiting process's priority a little over time.
Multilevel Queue and Multilevel Feedback Queue
A multilevel queue keeps separate queues, such as interactive and batch, each with its own algorithm. A multilevel feedback queue lets a process move between queues. A process that uses its whole quantum moves down, so short interactive jobs stay near the top.
Multiprocessor and Real-Time Scheduling
On a multiprocessor, the scheduler must also choose which CPU runs each process. Real-time systems add deadlines, and the scheduler must meet them. GATE questions usually stay with single-CPU models, so this chapter does not go deeper.
Worked GATE-Style Problems: Fork Counting and Gantt Charts
For fork counting, each fork() doubles the number of processes. Three forks give 2^3 = 8 processes. If each prints hello, the output has 8 lines.
For scheduling problems, draw the Gantt chart first, then fill a table with finish, turnaround, and waiting times. Round Robin with quantum 2 on bursts 5, 3, and 8 gives finish times 12, 9, and 16.
Exercise
Three processes arrive at time 0 with bursts 5, 3, and 8. Using non-preemptive SJF, what is the average waiting time?
Show answer
The order is 3, 5, 8. Finish times are 3, 8, and 16. Waiting times are 0, 3, and 8. The average is (0 + 3 + 8) / 3 = 11/3, about 3.67.