Why an operating system?
An operating system (OS) sits between hardware and applications so programmers do not have to control the hardware directly. Its main jobs:
- Process management: starting, pausing and ending programs, and deciding which runs next on the CPU.
- Memory management: giving each process its own region of RAM and preventing one from overwriting another.
- File management: organising data into files and folders, and controlling access rights.
- Device management: talking to hardware through drivers and buffering input and output.
- Security: user accounts, authentication and permissions.
- User interface: a GUI or command line.
Scheduling
Only one process can use a core at a time, so the scheduler decides the order. Common approaches:
| Approach | Rule | Strength | Weakness |
|---|---|---|---|
| First come, first served | Run in arrival order to completion | Simple, fair by order | Short jobs wait behind long ones |
| Shortest job first | Run the shortest remaining job | Lowest average waiting time | Long jobs may starve; lengths must be known |
| Round robin | Each process gets a fixed time slice, then goes to the back | Responsive, nobody starves | Frequent switching costs time |
| Priority | Highest priority runs first | Urgent tasks served fast | Low priority can starve (fix: ageing) |
Worked round-robin trace: P1 needs 4 ms, P2 needs 6 ms, P3 needs 2 ms, slice 3 ms, queue order P1, P2, P3.
| Time (ms) | Running | Remaining after slice |
|---|---|---|
| 0-3 | P1 | P1 = 1 |
| 3-6 | P2 | P2 = 3 |
| 6-8 | P3 | P3 = 0, finishes at 8 |
| 8-9 | P1 | P1 = 0, finishes at 9 |
| 9-12 | P2 | P2 = 0, finishes at 12 |
A process that needs less than a full slice gives up the CPU early, so the next one starts sooner.
Polling versus interrupts
- Polling: the CPU repeatedly checks a device ("any data yet?"). Simple and predictable, but it wastes CPU time when events are rare.
- Interrupt: the device sends a signal when it needs attention. The CPU finishes its current instruction, saves its state, runs an interrupt service routine (ISR), then restores the state and carries on.
flowchart TD
A[CPU running a program] --> B{Interrupt pending at end of instruction?}
B -- No --> A
B -- Yes --> C[Save registers and PC on the stack]
C --> D[Run the interrupt service routine]
D --> E[Restore registers and PC]
E --> AKeyboards and network cards normally use interrupts because key presses are unpredictable and rare compared with CPU speed. Polling suits a sensor that must be read at a steady rate anyway, or simple embedded devices.
Control systems
A control system keeps a physical quantity at a target value.
- Sensor: measures the environment (temperature, light, pressure) and produces a signal, often converted by an analogue-to-digital converter.
- Controller (microprocessor): compares the reading with the set point and decides what to do.
- Actuator: acts on the world (motor, heater, valve, pump).
- Feedback: the result of the action changes what the sensor reads next time, closing the loop.
TARGET = 21.0
while True:
temp = read_sensor()
if temp < TARGET - 0.5:
heater_on()
elif temp > TARGET + 0.5:
heater_off()
wait(5) # secondsThe 0.5 margin stops the heater flicking on and off constantly. An open-loop system (a basic toaster timer) has no feedback; a closed-loop system does.
Multitasking, resources and memory (HL)
- Multitasking: the OS switches between processes so quickly that they appear to run together. Each switch is a context switch: the state of one process is saved and another is loaded.
- Resource allocation: the OS shares CPU time, memory and devices. Deadlock happens when processes each hold a resource the other needs; starvation when a process never gets a turn.
- Paging: memory is split into fixed-size pages and frames, so a process need not sit in one continuous block. Virtual memory uses disk space as an overflow for RAM; if pages are swapped in and out constantly the system thrashes and slows dramatically.
Common mistakes
- Saying an interrupt stops the CPU mid-instruction (it waits for the instruction to finish).
- Describing round robin without stating what happens to unfinished processes (back of the queue).
- Confusing sensors with actuators.
- Leaving out the comparison with a set point when describing feedback.
Exam tips
- For "evaluate a scheduling method", give one benefit and one drawback linked to the scenario.
- Draw a timeline (Gantt) for any scheduling calculation; it avoids arithmetic slips.
- In control-system answers, name the specific sensor and actuator for the context.