Resources, Processes and Processor Scheduling
IB Computer Science · Resource Management (HL)
What counts as a resource. The resources an operating system manages are processor time, primary memory, secondary storage, network bandwidth, and the peripherals including screen, sound and input devices. Each has a finite capacity, and the key insight for this topic is that they are interdependent: a shortage of one leaves the others idle. A machine short of memory has a processor that spends its time waiting for the disk, so measuring processor utilisation alone gives a completely misleading picture of what is wrong.
The operating system as a manager. The operating system stands between hardware and applications. It schedules the processor between processes, allocates and protects memory so that one process cannot corrupt another, controls peripherals through device drivers, organises secondary storage as a file system, and presents a consistent interface so that applications do not need to know which particular hardware they are running on.
Processes and their states. A process is a program in execution together with its current state. At any moment a process is running on the processor, ready and waiting for its turn, or blocked while waiting for input, output or another event. Only one process per processor core can be running at a time; what looks like simultaneous execution is rapid switching. Each switch requires a context switch: the registers and program counter of the outgoing process are saved and those of the incoming process are restored. A context switch consumes processor time without completing any user work, so its cost is a real constraint on how finely the processor can be shared.
Scheduling algorithms. First come first served runs each process to completion in arrival order: simple and free of starvation, but a single long process makes every short process behind it wait. Shortest job first gives the lowest average waiting time of any algorithm, but it requires knowing run times in advance, which is rarely possible, and a long process can be starved indefinitely if short ones keep arriving. Round robin gives each process a fixed time slice called a quantum and cycles through them: no process is starved and response time is good, at the cost of extra context switches. Priority scheduling serves the most important process first and is used where some work is genuinely more urgent, but it too can starve low-priority work unless priority is raised as a process waits. Note that round robin does not reduce the total time to finish all work — it redistributes the waiting.
Worked example: Computing and comparing three scheduling algorithms on the same set of processes
Four processes all arrive at time 0 with run times: P1 needs 8, P2 needs 2, P3 needs 6, P4 needs 4. Compute the average waiting time for first-come first-served in the order P1 to P4, for shortest job first, and for round robin with quantum 4. Then conclude which one to choose.
Solution
First-come first-served, order P1, P2, P3, P4. P1 starts at 0 and finishes at 8; P2 waits 8 and finishes at 10; P3 waits 10 and finishes at 16; P4 waits 16 and finishes at 20. The waiting times are 0, 8, 10, 16, so the average equals 34 divided by 4 equals 8.5.
Shortest job first, order P2, P4, P3, P1. P2 waits 0 and finishes at 2; P4 waits 2 and finishes at 6; P3 waits 6 and finishes at 12; P1 waits 12 and finishes at 20. The waiting times are 0, 2, 6, 12, so the average equals 20 divided by 4 equals 5.0. This is the smallest value that can be achieved, and that is no coincidence: it can be proved that running the short job first always gives the minimum average waiting time, because each unit of time of a long job makes ALL the jobs behind it wait longer.
Round robin with quantum 4. Follow the timeline. From 0 to 4 run P1, 4 remaining. From 4 to 6 run P2, finishes at 6. From 6 to 10 run P3, 2 remaining. From 10 to 14 run P4, finishes at 14. From 14 to 18 run P1, finishes at 18. From 18 to 20 run P3, finishes at 20.
Completion times: P1 at 18, P2 at 6, P3 at 20, P4 at 14. Since all processes arrive at 0, waiting time equals completion time minus run time: P1 waits 10, P2 waits 4, P3 waits 14, P4 waits 10. The average equals 38 divided by 4 equals 9.5.
Read the result correctly, because this is the part that is marked. Round robin gives the worst average waiting time of the three algorithms in this example. If you looked only at that number the conclusion would be wrong. What round robin truly improves is the response time — the time from when a process arrives until it first runs. With round robin, all four processes are reached within the first 14 units; with first-come first-served, P4 must wait until time 16 before being looked at. On a system with a user sitting in front of the screen, that is the difference between feeling smooth and feeling frozen.
Conclude by context, not in general. For a batch system running overnight where no one is waiting, choose shortest job first if run times can be estimated, because throughput and average waiting time are the only criteria. For an interactive multi-user system, choose round robin, accepting a higher average waiting time in exchange for even response time and no one being starved. And the cost must be stated: each process switch costs one context switch, so a quantum that is too small makes this cost eat most of the processing time, while a quantum that is too large makes round robin degenerate into first-come first-served.
Worked example: Why starvation happens and how to prevent it
A system uses priority scheduling. Reports show a low-priority backup task has been in the ready state for six hours without running even once. Explain the phenomenon and state how to remedy it.
Solution
The starvation mechanism. Priority scheduling always chooses the process with the highest priority in the ready queue. If the system keeps receiving higher-priority processes — each time the processor is free there is such a process waiting — then a low-priority process never reaches the front of the queue. An important point to stress: this is not a bug; the algorithm is working exactly as designed. The problem is that the design has no mechanism to guarantee progress for every process.
Why starvation also happens with shortest job first. The same structure: if short jobs keep arriving, a long job is always placed behind and may wait indefinitely. The two algorithms share this weakness because both choose by a static attribute of the process without taking into account how long it has waited.
Remedy 1 — raise priority with waiting time. This is the standard solution and should be named: the system gradually increases a process's priority according to the time it spends in the ready queue. A low-priority task that waits long enough eventually rises above newly arriving high-priority processes, so it is certain to run. This keeps the benefit of priority in the short term while guaranteeing progress in the long term.
Remedy 2 — guarantee a minimum share of time. Reserve a certain proportion of processor time for the low-priority group, for example one scheduling turn in every ten must serve this group. This is simpler and gives more predictable behavior, but it reduces the responsiveness of high-priority processes.
Remedy 3 — multilevel feedback queues. Maintain several queues with different quanta: a new process enters the highest-priority queue with a short quantum; a process that uses up its quantum without finishing is moved down to a lower queue with a longer quantum. This mechanism automatically recognizes which processes are interactive and which are long-running computations, without knowing run times in advance — so it obtains most of the benefit of shortest job first without needing information that is not available in practice.
The general principle to write in the answer. Every scheduling algorithm that chooses by a single attribute risks starving some group. The cure is always to bring the time already waited into the scheduling decision, so that no process can be postponed indefinitely.
Common mistake
Trap 1 — concluding round robin is better because it is fair, without checking the numbers. Round robin usually gives a higher AVERAGE waiting time; what it improves is the RESPONSE time. And it does not reduce the total completion time of all jobs.Trap 2 — forgetting to state the context-switch cost when proposing a small quantum. Each time the process changes, all registers must be saved and reloaded, consuming time without completing any work. If the quantum is too small this cost eats most of the processing time.
Trap 3 — treating starvation as a programming bug. It is an inevitable consequence of scheduling by a static attribute; the cure is to bring the TIME ALREADY WAITED into the decision, for example by gradually raising priority.