OS services and hardware abstraction
When you click Scan, you never tell the scanner which signals to send, or the disk where to put the image. The operating system does that. It manages the processor, memory, files and devices, and gives applications an interface to request services without controlling every hardware detail.
What the OS does for each user action
Picture one office workstation: a user scans an image, opens it, and views it while a copy is saved. Each numbered action matches a row of the table: the OS part that serves it and the hardware detail it hides.
| At the workstation | OS service and hardware detail hidden |
|---|---|
| Select Scan in a graphical interface. | A system call requests input; a device driver controls the scanner. Hidden: device commands, control registers and transfer timing. |
| Open the saved image by its file name. | The file system locates the file and reads its blocks. Hidden: physical locations of the stored bytes. |
| Display the image while a copy is saved. | Processor and memory management share resources between processes. Hidden: which CPU interval and RAM frame each process receives. |
How the OS hides device differences
Every scanner model speaks its own language, yet the user scans the same way on all of them:
- A graphical user interface represents actions as windows, icons and menus.
- A driver translates a standard OS request into commands for a particular device; changing the scanner need not change the user’s scanning steps.
- Device mapping gives a device a usable name or logical identity, so software can select it and inspect its status. The mapping connects that logical identity to the actual device.
1 Which OS part turns a standard request into commands for one particular scanner?
2 The user opens a file by its name. What does the OS hide from them?
3 The workstation gets a new scanner model. Why need the user’s scanning steps not change?
- 1the device driver
- 2the physical locations of the file’s stored bytes (the file system finds and reads its blocks)
- 3the application still makes the same standard request; only the driver for the new device changes
Processes and multitasking
A processor can only do one thing at a time, yet the workstation scans, displays, stores and syncs all at once. To share it out, the OS separates a program, a stored set of instructions, from a process: an executing instance of a program, with its own current execution state and resources.
Four jobs share the workstation’s one processor. Arrival is when each becomes ready; its CPU burst is the processor time it needs to finish.
| Job | Purpose | Arrival | CPU burst |
|---|---|---|---|
| A | Scan | 0 | 7 |
| B | Display | 1 | 3 |
| C | Store | 3 | 2 |
| D | Sync | 5 | 1 |
What the OS saves for each process
A process can be paused at any instruction, so the OS must keep enough to pick it up again exactly there.
Each entry on Scan’s card has a job to do when Scan is paused and later picked up again:
| Process-control block (PCB) | Why the OS records it |
|---|---|
| Process identifier and state | Identify the process and whether it may run. |
| Program counter and register values | Resume the interrupted calculation correctly. |
| Memory-management information | Locate its address space and resident pages. |
| Scheduling information / priority | Place and select it within the scheduler’s policy. |
- Multitasking shares processor time between processes.
- Rapid switching makes several tasks appear to run together on a single CPU.
- While Scan is blocked for a device, Display can use the processor.
1 How is a process different from a program?
2 Scan is paused part-way through. Which PCB entries let it carry on at the right instruction?
3 Which job arrives last, and how much CPU time does it need?
- 1a program is stored instructions; a process is that program executing, with its own state and resources
- 2the program counter and the register values
- 3D (Sync): it arrives at 5 and needs 1
Ready, running and blocked
A process spends much of its life not running: sometimes it waits for its turn, sometimes for a device. The OS tells these apart: running while it has the CPU, ready while it needs only its turn, blocked while it waits for an event.
Follow Scan A round the arrows. Each arrow is named after the event that moves a process along it.
Follow the event, then change the state
Every state change is caused by an event, so follow the events. One row per event. Read across: each process’s state after it. Only one process can be Running.
| Scan A | Display B | Event | |
|---|---|---|---|
| event 1 | Running | Ready | A is dispatched. |
| A is dispatched. | |||
| event 2 | Blocked | Running | A requests scanner input; B is dispatched. |
| A requests scanner input; B is dispatched. | |||
| event 3 | Ready | Running | Scanner interrupt reports completion: A becomes Ready, not Running. |
| Scanner interrupt reports completion: A becomes Ready, not Running. | |||
| event 4 | Running | Ready | B’s time slice expires; A is selected. |
| B’s time slice expires; A is selected. | |||
- I/O completion makes A eligible to run again: it returns to the ready queue and waits for dispatch.
- Completion alone does not give it ownership of the processor.
1 Display B’s time slice ends. Which state does B move to?
2 Scan A asks the scanner for data. Which state does A move to?
3 The scanner finishes. Can A go straight from Blocked to Running?
- 1Ready
- 2Blocked
- 3no: it goes to Ready and waits to be dispatched
Kernel and scheduling
Someone has to decide who runs next, and stop one process cleanly so another can start. That job belongs to the kernel, the privileged core of the OS. It handles interrupts and controls protected resources. Scheduling decides which eligible process receives processor time.
Steps of a context switch
A switch must not lose a single register of the paused process. Each numbered step matches a mark on the time line.
Step 3 is the low-level scheduler at work. A second scheduler decides earlier, and less often, which jobs get into memory at all:
| Scheduler | Decision → consequence |
|---|---|
| High-level / job scheduler | Choose which jobs enter memory → avoid loading more active work than memory can support. |
| Low-level / CPU scheduler | Choose the next ready process → give Scan or Display the next CPU interval. |
What a scheduling policy aims for
No single order is best for every job, so a policy balances several aims:
- Running another ready process during an I/O wait reduces CPU idle time.
- Time sharing can give interactive work a prompt response.
- A policy must also consider fair access, priority and whether long jobs can be starved.
1 What does the kernel save when it switches away from Scan A, and where?
2 Which scheduler chooses the next ready process?
3 Give one cost of making the time quantum very short.
- 1its program counter and registers, in its PCB (with its new state)
- 2the low-level (CPU) scheduler
- 3more context switches, so more CPU time is spent saving and restoring state instead of running jobs
FCFS and round robin
The order jobs get the CPU decides how long each one waits. The simplest rule is first come first served (FCFS); round robin makes the jobs take turns instead. Both run the workstation’s same four jobs, with one CPU, no I/O during these bursts and zero context-switch time.
| Job | Purpose | Arrival | CPU burst |
|---|---|---|---|
| A | Scan | 0 | 7 |
| B | Display | 1 | 3 |
| C | Store | 3 | 2 |
| D | Sync | 5 | 1 |
Read each time line from 0. A block is one job holding the CPU; the numbers mark each hand-over. Find where B first gets a turn in each.
The two rules, and what each one means for B:
| Policy | Selection rule → response |
|---|---|
| FCFS: non-preemptive | Run the earliest queued job until it finishes or blocks. B arrives at 1; A’s 7-unit burst delays B’s first run until 7. |
| Round robin: preemptive | Give the head of the queue a fixed time quantum; unfinished work rejoins the tail. Quantum = 2: B first runs at 2; A resumes later with its saved state. |
Follow the ready queue in round robin
Round robin is easy to get wrong unless you keep track of the queue. One row each time the CPU chooses. The head of the queue runs; arrivals join before the expired job rejoins the tail.
| Runs | Ready queue (head first) | What happens | |
|---|---|---|---|
| t = 0 | A | A | A runs 2; 5 left, rejoins the tail · B arrived |
| A runs 2; 5 left, rejoins the tail · B arrived | |||
| t = 2 | B | B A | B runs 2; 1 left, rejoins the tail · C arrived |
| B runs 2; 1 left, rejoins the tail · C arrived | |||
| t = 4 | A | A C B | A runs 2; 3 left, rejoins the tail · D arrived |
| A runs 2; 3 left, rejoins the tail · D arrived | |||
| t = 6 | C | C B D A | C runs 2; finished |
| C runs 2; finished | |||
| t = 8 | B | B D A | B runs 1; finished |
| B runs 1; finished | |||
| t = 9 | D | D A | D runs 1; finished |
| D runs 1; finished | |||
| t = 10 | A | A | A runs 2; 1 left, rejoins the tail |
| A runs 2; 1 left, rejoins the tail | |||
| t = 12 | A | A | A runs 1; finished |
| A runs 1; finished | |||
Arrivals join the ready queue immediately; at a slice boundary, they enter before the expired process rejoins.
At t = 2, the queue is B, A. At t = 4 it becomes A, C, B. A job that finishes early releases the rest of its slice.
1 Under FCFS, at what time does C first run?
2 Under round robin, which job runs from t = 6 to t = 8?
3 Under round robin, at what time does A finish?
- 110
- 2C
- 313
Shortest job and shortest remaining time
Under FCFS, three short jobs waited behind A’s long burst. Letting short jobs go first cuts that wait. Shortest job first (SJF) chooses the shortest ready burst and is non-preemptive. Shortest remaining time (SRT) is preemptive: when a job arrives, compare how much CPU work each still needs.
The same four jobs: A: arrival 0, burst 7; B: 1, 3; C: 3, 2; D: 5, 1. Assume no I/O, zero switch time and known burst lengths. Ties favour the earlier arrival.
Compare the two time lines at t = 1, when B arrives: SJF leaves A running, while SRT stops A and hands the CPU to B.
SJF and SRT when a job arrives
The two rules only behave differently when a shorter job turns up mid-run:
| Decision | SJF | SRT |
|---|---|---|
| At t = 1, B arrives. | A continues: non-preemptive. | A has 6 left; B needs 3. Save A and run B. |
| At t = 3, C arrives. | A still continues. | B has 1 left, less than C’s 2: keep B. |
| When A finishes at t = 7 in SJF. | Ready D = 1, C = 2, B = 3: choose D. | In SRT, D has finished and A resumes its final 6 units. |
Follow SRT one decision at a time
Each row is a moment when a job arrives or the choice changes. The cells are the work each job still needs.
| A | B | C | D | Runs | Why | |
|---|---|---|---|---|---|---|
| t = 0 | 7 | — | — | — | A | A arrives; A has the least left: run A |
| A arrives; A has the least left: run A | ||||||
| t = 1 | 6 | 3 | — | — | B | B arrives; B has the least left: run B |
| B arrives; B has the least left: run B | ||||||
| t = 3 | 6 | 1 | 2 | — | B | C arrives; B still has the least left: keep B |
| C arrives; B still has the least left: keep B | ||||||
| t = 4 | 6 | done | 2 | — | C | C has the least left: run C |
| C has the least left: run C | ||||||
| t = 5 | 6 | done | 1 | 1 | C | D arrives; C still has the least left: keep C |
| D arrives; C still has the least left: keep C | ||||||
| t = 6 | 6 | done | done | 1 | D | D has the least left: run D |
| D has the least left: run D | ||||||
| t = 7 | 6 | done | done | done | A | A has the least left: run A |
| A has the least left: run A | ||||||
Compare waiting using the same jobs
1 Under SJF, why does A keep the CPU when B arrives at t = 1?
2 Under SJF, find B’s waiting time.
3 Which of the four policies gives the lowest mean wait for these jobs?
- 1SJF is non-preemptive: a running job is never interrupted for a shorter one
- 29: it finishes at 13, so 13 − 1 − 3
- 3SRT, mean wait 2
Paging and page tables
Several processes share RAM, so no process can count on one neat block of it, and with paging it does not need to. Paging divides a process’s logical address space into equal-sized pages. RAM is divided into same-sized frames; a page table records each page’s frame or its absence from RAM.
Follow each arrow from one of A’s pages to the frame that holds it. The pages land wherever a frame was free, and page 3 has no arrow at all.
A uses 256-byte pages. Its page 2 is resident in frame 5. Translate logical byte address 700 into a physical address.
- Split page number and offset700=2 × 256 + 188Page=2; offset = 188
- Look up page 2Page table: page 2 → frame 5
- Keep the offsetPhysical address=5 × 256 + 188=1468
- AnswerPhysical address 1468
1 Translate logical address 300 for process A (256-byte pages).
2 Translate logical address 1000. What happens?
3 Which part of the address is never changed by translation?
- 1page 1, offset 44; page 1 → frame 4; 4 × 256 + 44 = 1068
- 2page 3, offset 232; page 3 is absent, so there is no frame: a page fault
- 3the offset
Page faults and replacement
A’s page 3 had no frame at all. That is normal: RAM is rarely big enough for every page of every process. Virtual memory lets a process use a logical address space larger than available RAM. Non-resident pages stay on secondary storage. Referencing one causes a page fault: the OS loads it before the access can continue.
Step through one fault: page 0 leaves frame 1, page 3 arrives in it, and A’s page table changes to match.
Handle a page fault step by step
A’s whole page table before and after. Only two entries change: the victim and the page that took its frame.
| A page | Before fault | After fault |
|---|---|---|
| 0 | Frame 1 | Absent |
| 1 | Frame 4 | Frame 4 |
| 2 | Frame 5 | Frame 5 |
| 3 | Absent | Frame 1 |
1 What is a page fault?
2 Why is page 0 the FIFO victim here?
3 When must the victim page be written back to secondary storage?
- 1a reference to a page that is not in RAM; the OS must load it before the access continues
- 2it was loaded earliest of the three resident pages
- 3only if it was modified while in RAM
Segmentation and paging
Paging cuts a program’s address space into fixed-sized blocks, wherever the cuts happen to fall. But a program’s own parts are not equal in size. Segmentation divides a program into meaningful, variable-sized regions, such as code, data and stack.
A’s program has three segments. Each has its own length and starts at its own base address in RAM:
| A’s logical segment | Length (bytes) | Physical base |
|---|---|---|
| Code | 384 | 256 |
| Data | 448 | 768 |
| Stack | 160 | 1280 |
Translate a segment address
Each segment has a base and a length, so an offset past its end must be refused before it reaches memory.
Both methods map a logical address to RAM. Compare them row by row, ending on the space each one wastes:
| Comparison | Segmentation | Paging |
|---|---|---|
| Unit of allocation | Logical region; sizes differ. | Page and frame have fixed, equal size. |
| Translation | Segment number → base and limit; add a valid offset. | Page number → frame; keep the page offset. |
| Placement | Each segment needs a contiguous region. | A process’s pages can occupy separated frames. |
| Unused space | Free gaps between allocated segments can be too small: external fragmentation. | Unused space inside the last frame: internal fragmentation. |
The same program in segments and in pages
Both bars hold the same bytes on one scale. Amber marks the waste: gaps between segments, and the unused end of the last page.
Code 384 + data 448 + stack 160 = 992 bytes. Packing that address space into 256-byte pages needs 4 pages = 1024 bytes, leaving 32 bytes unused in its final page. Segment boundaries need not coincide with page boundaries.
1 Find the physical address of offset 50 in A’s stack segment.
2 Offset 200 is requested in the stack segment. What happens?
3 Which method causes external fragmentation, and which causes internal fragmentation?
- 11280 + 50 = 1330 (50 is less than the length 160)
- 2200 ≥ 160: outside the segment, so the access is rejected
- 3segmentation: external; paging: internal
Thrashing and resource choices
Running more processes at once should get more done. Past a point it does the opposite. Thrashing occurs when repeated page transfers consume so much time that little useful processing takes place. Too many active processes can leave each with too few frames for the pages it repeatedly needs.
A repeatedly visits pages 0, 1, 2, 3 in that order. Begin with no A pages resident and use FIFO. Compare three frames with four frames.
Now check every reference. One row per page request: what the three frames hold after it, and whether it hits or faults with three frames and with four.
| Page | Three frames (oldest first) | 3 frames | 4 frames | With three frames | |
|---|---|---|---|---|---|
| ref 1 | 0 | 0 | fault | fault | fault: load into a free frame |
| fault: load into a free frame | |||||
| ref 2 | 1 | 01 | fault | fault | fault: load into a free frame |
| fault: load into a free frame | |||||
| ref 3 | 2 | 012 | fault | fault | fault: load into a free frame |
| fault: load into a free frame | |||||
| ref 4 | 3 | 123 | fault | fault | fault: evict 0 |
| fault: evict 0 | |||||
| ref 5 | 0 | 230 | fault | hit | fault: evict 1 |
| fault: evict 1 | |||||
| ref 6 | 1 | 301 | fault | hit | fault: evict 2 |
| fault: evict 2 | |||||
| ref 7 | 2 | 012 | fault | hit | fault: evict 3 |
| fault: evict 3 | |||||
| ref 8 | 3 | 123 | fault | hit | fault: evict 0 |
| fault: evict 0 | |||||
| Reference | Three-frame contents (oldest first) | 3 frames | 4 frames |
|---|---|---|---|
| 0 | 0 | Fault | Fault |
| 1 | 0, 1 | Fault | Fault |
| 2 | 0, 1, 2 | Fault | Fault |
| 3 | 1, 2, 3 | Fault | Fault |
| 0 | 2, 3, 0 | Fault | Hit |
| 1 | 3, 0, 1 | Fault | Hit |
| 2 | 0, 1, 2 | Fault | Hit |
| 3 | 1, 2, 3 | Fault | Hit |
Why three frames cause repeated faults
- With three frames, FIFO evicts the page needed next: page 3 displaces 0, then 0 displaces 1.
- Four frames retain all four pages after the initial loads.
- Faults: 8 with three frames; 4 with four frames.
- A working set is the collection of pages a process is actively using.
- When active working sets exceed available RAM, repeated faults block processes for storage transfers and reduce CPU progress.
Three changes the OS could make. Only those that leave each process more frames help:
| Resource choice | Why it changes useful progress |
|---|---|
| Allocate enough frames / add RAM. | Keep more actively used pages resident, reducing page faults. |
| Admit fewer concurrent processes. | Give the remaining processes room for their working sets. |
| Change CPU scheduling alone. | Changes ready-process order; it cannot make an absent page resident. |
1 What is thrashing?
2 With three frames, which page does the second reference to page 0 evict?
3 Give one change that reduces thrashing, and one that does not.
- 1so much time is spent moving pages in and out that little useful processing happens
- 2page 1
- 3reduces it: more frames or RAM, or fewer active processes; does not: changing CPU scheduling alone