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.

THREE PROCESSES ALL ARRIVING AT TIME 0 — P1 needs 12, P2 needs 3, P3 needs 5 FIRST-COME FIRST-SERVED P1 P2 P3 0 12 15 20 avg wait 9.0 P2 waits 12 before running ROUND ROBIN — quantum 4 P1 P2 P3 P1 P3 P1 0 4 7 11 15 16 20 avg wait 7.67 P2 xong ngay lúc 7 WHAT ROUND ROBIN TRADES: the total completion time of all three is still 20 in BOTH methods What it improves is RESPONSE TIME — every process gets to run within the first 8 units THE COST: each time the process changes, all registers must be saved and reloaded — called a CONTEXT SWITCH If the quantum is too SMALL, context-switch overhead eats the processing time; if too LARGE, round robin degenerates into FCFS
This figure answers the most frequently asked question about scheduling: where is round robin "better", and what does it pay for it. Read the numbers first. With first-come first-served, P1 runs all 12 units before P2 gets a turn, so P2 — a process that needs only 3 units — must wait until time 12. The waiting times are 0, 12 and 15 respectively, an average of 9.0. With round robin at quantum 4, P2 runs right at time 4 and completes at time 7; the waiting times are 8, 4 and 11 respectively, an average of 7.67. But the most important number is the one that does NOT change. All three processes still complete at time 20 in both methods, because the total amount of work has not changed. Round robin does not make the computer faster — it only redistributes the waiting. What it truly improves is the response time: every process is reached within the first 8 units, instead of one process having to wait 15 units before being looked at. For a system with a user sitting in front of the screen, that is the difference between feeling smooth and feeling frozen. The cost is in the red box and must always be stated. Each time the system switches from one process to another, the operating system must save all registers and the instruction pointer of the old process and then load those of the new process — this is a context switch, and it consumes processing time without completing any work. So the quantum is a parameter that must be balanced: too small and the context-switch cost eats most of the processing time; too large and each process runs its whole task and round robin degenerates into first-come first-served. A third algorithm worth mentioning: shortest job first gives the lowest average waiting time — 3.67 in this example — but it requires knowing each process's run time in advance, which is usually not known, and it can cause a long process to be STARVED indefinitely if short processes keep arriving.

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.

Key takeaway

Lesson 1 wrap-up. Four things to take into the exam room. One, the three process states — running, ready, blocked — and the cost of a context switch. Two, four scheduling algorithms, each with its own weakness: first-come first-served makes short jobs wait long; shortest job first needs run times in advance and causes starvation; round robin costs context switches; priority also causes starvation. Three, round robin improves RESPONSE time, not average waiting time, and the conclusion must follow the context of batch versus interactive processing. Four, the cure for starvation is always to bring the time already waited into the scheduling decision.

← Applications of Stacks and Queues: Call Stacks, Undo, Buffers and Breadth-First Traversal · All IB Computer Science lessons · Practice IB Computer Science questions (free, instant scoring) · Memory Management, Virtual Memory and Bottlenecks →