CPU Scheduling, Deadlock and Paging: Solved OS Numericals
Lecturer, programmer and IT officer papers go deeper into operating systems than the basic questions on booting and file systems. They ask for the average waiting time under a scheduling algorithm, the four conditions for deadlock, the page number and offset of an address, and the number of page faults under FIFO or LRU. This lesson explains each method with a worked timeline; every figure was checked by simulation.
Asked in: PPSC, FPSC, NTS, ETEA, KPPSC, SSC, IBPS and ICS/ECAT computer
Scheduling terms and formulas
Arrival time is when a process enters the ready queue and burst time is the CPU time it needs. Completion time is when it finishes. Turnaround time is the total time spent in the system, and waiting time is the part of that spent waiting in the ready queue. Response time is the gap between arrival and the first time the process gets the CPU.
Scheduling can be non-preemptive, where a running process keeps the CPU until it finishes or blocks, or preemptive, where the scheduler can take the CPU away, for example when a time slice ends or a shorter job arrives. Draw a Gantt chart (a time line showing which process runs when) and the numbers follow directly.
- Turnaround time = completion time − arrival time
- Waiting time = turnaround time − burst time
- Average = sum ÷ number of processes
FCFS, SJF, SRTF, priority and Round Robin
First Come First Served (FCFS) runs processes in order of arrival without preemption. It is simple but suffers from the convoy effect: short jobs stuck behind one long job wait a long time. Shortest Job First (SJF) picks, among the processes that have arrived, the one with the smallest burst; for processes that are all available together it gives the minimum possible average waiting time, but long jobs may starve and burst times must be known in advance. Its preemptive form, Shortest Remaining Time First (SRTF), switches to a newly arrived process whose burst is shorter than the time remaining for the running one.
Priority scheduling runs the highest-priority process first and can be preemptive or not. Low-priority processes may wait indefinitely (starvation); the remedy is aging, which raises a process’s priority the longer it waits. Round Robin (RR) is FCFS with preemption by a fixed time quantum: a process that does not finish within its quantum goes to the back of the ready queue. RR is designed for time-sharing systems. A very large quantum makes it behave like FCFS; a very small one wastes time on context switches.
- SJF → minimum average waiting time (all jobs available)
- Starvation → cured by aging
- RR with a huge quantum ≈ FCFS
Deadlock
A deadlock is a situation in which a set of processes wait for ever, because each holds a resource that another one in the set is waiting for. Four conditions must all hold at the same time (the Coffman conditions): mutual exclusion (a resource can be used by only one process at a time), hold and wait (a process holds one resource while waiting for another), no preemption (a resource cannot be taken away by force) and circular wait (a closed chain of processes, each waiting for the next).
Deadlock prevention breaks at least one condition, for example by numbering resources and making every process request them in increasing order, which rules out circular wait. Deadlock avoidance checks each request and grants it only if the system stays in a safe state; the Banker’s algorithm, proposed by Dijkstra, is the standard method. Detection and recovery lets deadlocks happen, finds them (a cycle in a resource-allocation graph with single-instance resources means deadlock) and then ends or rolls back processes. Ignoring the problem, as many desktop systems do, is called the ostrich approach.
- Deadlock needs: mutual exclusion + hold and wait + no preemption + circular wait
- n processes each needing at most k units: n(k − 1) + 1 units guarantee no deadlock
Paging and address translation
Paging divides a process’s logical memory into fixed-size pages and physical memory into frames of the same size. Any page can go into any free frame, and a page table records which frame holds each page. Because frames are all the same size, paging removes external fragmentation, but the last page of a process is usually only partly used, which is internal fragmentation. Segmentation instead divides a program into variable-size logical parts such as code, data and stack, and can suffer from external fragmentation.
A logical address is split into a page number and an offset: page number = address ÷ page size (whole part) and offset = the remainder. The physical address is frame number × page size + offset. A page size of 2ᵏ bytes uses k bits for the offset. A Translation Lookaside Buffer (TLB) is a small, fast cache of recent page-table entries that speeds up this translation.
- Page number = logical address ÷ page size (whole part); offset = remainder
- Physical address = frame × page size + offset
- Number of pages = logical space ÷ page size
Virtual memory and page replacement
Virtual memory lets a process run even when only some of its pages are in RAM; the rest stay on disk. With demand paging a page is loaded only when it is first used. If the CPU refers to a page that is not in RAM, a page fault occurs and the operating system brings it in from disk. When no frame is free, a page replacement algorithm chooses a victim page. If processes have too few frames and spend most of their time on page faults, the system is thrashing.
FIFO replaces the page that has been in memory longest. LRU (Least Recently Used) replaces the page whose last use was furthest in the past. Optimal (OPT) replaces the page that will not be used for the longest time in the future; it gives the fewest faults but cannot be built in practice because the future is unknown, so it is used as a yardstick. FIFO can show Belady’s anomaly: giving it more frames can sometimes produce more page faults. LRU and OPT never show this anomaly. Count every first loading into an empty frame as a fault too.
- Page fault → page not in main memory
- Belady’s anomaly → FIFO only
- Optimal gives the fewest possible faults for any reference string
Solved examples
- Four processes have (arrival, burst) times in ms: P1 (0, 8), P2 (1, 4), P3 (2, 9), P4 (3, 5). Find the average waiting and turnaround times under FCFS.
- Gantt chart: P1 0 to 8, P2 8 to 12, P3 12 to 21, P4 21 to 26.
- Turnaround = completion − arrival: P1 8, P2 11, P3 19, P4 23; average 61 ÷ 4 = 15.25 ms.
- Waiting = turnaround − burst: P1 0, P2 7, P3 10, P4 18; average 35 ÷ 4 = 8.75 ms.
Answer: Average waiting 8.75 ms; average turnaround 15.25 ms - Four jobs arrive together with bursts P1 = 5, P2 = 9, P3 = 2 and P4 = 6 ms. Find the average waiting time under SJF and compare it with FCFS.
- SJF order by burst: P3 (2), P1 (5), P4 (6), P2 (9).
- Start times = waiting times: P3 0, P1 2, P4 7, P2 13. Average = 22 ÷ 4 = 5.5 ms.
- FCFS in the order P1, P2, P3, P4 waits 0, 5, 14, 16; average 35 ÷ 4 = 8.75 ms.
Answer: SJF 5.5 ms, against 8.75 ms for FCFS - P1 = 10, P2 = 4 and P3 = 7 ms all arrive at time 0 in that order. Using Round Robin with a quantum of 3 ms, find each completion time and the average waiting time.
- P1 0 to 3 (7 left), P2 3 to 6 (1 left), P3 6 to 9 (4 left).
- P1 9 to 12 (4 left), P2 12 to 13 (done at 13), P3 13 to 16 (1 left).
- P1 16 to 19 (1 left), P3 19 to 20 (done at 20), P1 20 to 21 (done at 21).
- Waiting = completion − burst (arrival 0): P1 11, P2 9, P3 13; average 33 ÷ 3 = 11 ms.
Answer: P2 at 13, P3 at 20, P1 at 21; average waiting 11 ms - A system has 4 processes, and each may need up to 3 units of the same printer resource. What is the smallest number of units that guarantees deadlock cannot occur?
- Worst case: every process holds one unit less than it needs, that is 2 units each, and waits: 4 × 2 = 8 units are tied up.
- One more unit lets some process finish and release its units, after which the others can finish too.
- Formula: n(k − 1) + 1 = 4 × 2 + 1.
Answer: 9 units - With a page size of 1 KB (1024 bytes), find the page number and offset of logical address 3500. If page 3 is in frame 7, what is the physical address?
- 3500 ÷ 1024 = 3 remainder 428, so page 3 and offset 428.
- Physical address = 7 × 1024 + 428 = 7168 + 428.
Answer: Page 3, offset 428; physical address 7596 - A process has 3 frames and the page reference string 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2. Count page faults under FIFO and LRU.
- FIFO: 2 F, 3 F, 2 hit, 1 F, 5 F (out 2), 2 F (out 3), 4 F (out 1), 5 hit, 3 F (out 5), 2 hit, 5 F (out 2), 2 F (out 4). Faults = 9.
- LRU: 2 F, 3 F, 2 hit, 1 F, 5 F (out 3, least recently used), 2 hit, 4 F (out 1), 5 hit, 3 F (out 2), 2 F (out 4), 5 hit, 2 hit. Faults = 7.
- For comparison, the Optimal algorithm gives 6 faults on this string.
Answer: FIFO 9 faults, LRU 7 faults
Exam shortcuts
- Always draw the Gantt chart first; then waiting = completion − arrival − burst for every process.
- In SJF with arrival times, choose only among processes that have already arrived when the CPU becomes free.
- In Round Robin, keep a written queue; a new arrival joins the queue before the process whose quantum just ended.
- For page faults, write the frame contents after each reference and mark F or hit; count the initial loads as faults.
- Remember the pairs: convoy effect with FCFS, starvation with SJF and priority, Belady’s anomaly with FIFO, Banker’s algorithm with avoidance.
Common mistakes
- Forgetting to subtract the arrival time, which turns completion time into a wrong turnaround time.
- Applying SJF to a process that has not arrived yet when the CPU is free.
- Calling starvation a deadlock condition; the four conditions are mutual exclusion, hold and wait, no preemption and circular wait.
- In LRU, replacing the page loaded earliest instead of the page used least recently.
- Mixing up fragmentation types: paging leaves internal fragmentation; variable partitions and segmentation suffer external fragmentation.
Practice MCQs
- Which of the following is NOT one of the four necessary conditions for a deadlock?
A) Mutual exclusion
B) Starvation
C) Hold and wait
D) Circular wait
Answer: B) Starvation — The four Coffman conditions are mutual exclusion, hold and wait, no preemption and circular wait. Starvation, where a process waits indefinitely while others run, is a different problem and is not needed for deadlock. - Short processes waiting behind one very long process, which raises the average waiting time, is called the convoy effect. It is typical of which scheduling algorithm?
A) Round Robin
B) SRTF
C) SJF
D) FCFS
Answer: D) FCFS — FCFS never preempts, so a long job at the front holds up everything behind it. Round Robin limits each turn to a quantum, and SJF and SRTF deliberately run short jobs first. - For a set of processes that are all available at the same time, which non-preemptive algorithm gives the minimum average waiting time?
A) Shortest Job First
B) First Come First Served
C) Round Robin
D) Priority scheduling
Answer: A) Shortest Job First — Running shorter jobs first reduces how long the others wait, and SJF is proven optimal for this case. FCFS and priority ignore burst length, and Round Robin is preemptive and aims at fair response, not minimum waiting. - In priority scheduling, which technique prevents a low-priority process from waiting for ever by gradually raising its priority the longer it waits?
A) Paging
B) Swapping
C) Aging
D) Spooling
Answer: C) Aging — Aging increases a waiting process’s priority over time, curing starvation. Paging is a memory scheme, swapping moves whole processes between RAM and disk, and spooling queues output for slow devices such as printers. - Belady’s anomaly, in which increasing the number of frames can increase the number of page faults, can occur with which page replacement algorithm?
A) LRU
B) FIFO
C) Optimal
D) Both LRU and Optimal
Answer: B) FIFO — FIFO can show Belady’s anomaly. LRU and Optimal are stack algorithms, for which more frames never produce more faults. - The Banker’s algorithm is used for:
A) Deadlock avoidance
B) Deadlock detection only
C) CPU scheduling
D) Page replacement
Answer: A) Deadlock avoidance — The Banker’s algorithm grants a request only if the system stays in a safe state, which is deadlock avoidance. Detection algorithms find deadlocks after they occur, and the algorithm has nothing to do with choosing processes for the CPU or pages for replacement. - A page fault occurs when:
A) The page table becomes full
B) Two processes use the same frame
C) The CPU finishes a time quantum
D) A referenced page is not in main memory
Answer: D) A referenced page is not in main memory — A page fault is the trap raised when a program refers to a page that is not currently in RAM, so the OS must load it from disk. A full page table, frame sharing and the end of a time quantum are unrelated events. - A system uses 2 KB (2048-byte) pages. A process needs 7,300 bytes. How much memory is lost to internal fragmentation in its last page?
A) 700 bytes
B) 1,156 bytes
C) 892 bytes
D) 0 bytes
Answer: C) 892 bytes — 7,300 ÷ 2,048 = 3.56, so the process needs 4 pages = 8,192 bytes, and 8,192 − 7,300 = 892 bytes of the last page are unused. 700 bytes wrongly takes a 2 KB page as 2,000 bytes, 1,156 bytes is the part of the process that spills into the fourth page rather than the unused part, and 0 is wrong because paging does not avoid internal fragmentation. - The page size is 1 KB (1024 bytes) and the page table maps page 4 to frame 2. What is the physical address of logical address 5000?
A) 2952
B) 5000
C) 2048
D) 4096
Answer: A) 2952 — 5000 ÷ 1024 = 4 remainder 904, so page 4 and offset 904. Page 4 is in frame 2, so the physical address is 2 × 1024 + 904 = 2952. 5000 ignores the mapping, 2048 is only the start of frame 2, and 4096 is the start of page 4 in logical memory. - Processes with (arrival, burst) times in ms are P1 (0, 5), P2 (2, 3), P3 (4, 6) and P4 (5, 2). What is the average waiting time under FCFS?
A) 6.75 ms
B) 4 ms
C) 8 ms
D) 2.75 ms
Answer: B) 4 ms — They run P1 0 to 5, P2 5 to 8, P3 8 to 14, P4 14 to 16. Waiting times: P1 0, P2 3, P3 4, P4 9, average 16 ÷ 4 = 4 ms. 6.75 ms uses start times without subtracting arrivals, 8 ms is the average turnaround time, and 2.75 ms is what SJF would give. - Processes with (arrival, burst) times in ms are P1 (0, 7), P2 (2, 4), P3 (3, 1) and P4 (5, 3). What is the average waiting time under non-preemptive SJF?
A) 4 ms
B) 5 ms
C) 2.75 ms
D) 7.75 ms
Answer: A) 4 ms — P1 runs 0 to 7 because it is alone at time 0. At 7 the shortest waiting job is P3 (7 to 8), then P4 (8 to 11), then P2 (11 to 15). Waiting: P1 0, P3 4, P4 3, P2 9, average 16 ÷ 4 = 4 ms. 5 ms is FCFS, 2.75 ms is preemptive SRTF, and 7.75 ms is the average turnaround time. - P1 = 9, P2 = 5 and P3 = 3 ms arrive at time 0 in that order and are scheduled by Round Robin with a quantum of 4 ms. At what time does P2 finish?
A) 14 ms
B) 8 ms
C) 16 ms
D) 17 ms
Answer: C) 16 ms — P1 0 to 4, P2 4 to 8 (1 left), P3 8 to 11 (done), P1 11 to 15 (1 left), P2 15 to 16 (done), P1 16 to 17. So P2 finishes at 16. 14 ms would be FCFS, 8 ms is only the end of P2’s first turn, and 17 ms is when P1 finishes. - Processes with (arrival, burst) times in ms are P1 (0, 8), P2 (1, 4) and P3 (2, 2). Under preemptive SJF (Shortest Remaining Time First), what is the average waiting time?
A) 5 ms
B) About 2.67 ms
C) About 5.67 ms
D) 4 ms
Answer: B) About 2.67 ms — P2 preempts P1 at time 1 (4 is less than 7 remaining), and P3 preempts P2 at 2 (2 is less than 3 remaining). Timeline: P1 0 to 1, P2 1 to 2, P3 2 to 4, P2 4 to 7, P1 7 to 14. Waiting: P1 6, P2 2, P3 0, average 8 ÷ 3 ≈ 2.67 ms. 5 ms is non-preemptive SJF and about 5.67 ms is FCFS. - With 3 frames, initially empty, how many page faults does FIFO produce for the reference string 3, 1, 4, 3, 2, 1, 5, 3, 4, 1?
A) 9
B) 6
C) 7
D) 8
Answer: D) 8 — FIFO: 3 F, 1 F, 4 F, 3 hit, 2 F (out 3), 1 hit, 5 F (out 1), 3 F (out 4), 4 F (out 2), 1 F (out 5), giving 8 faults. LRU would give 9 and Optimal 6 on this string. - With 3 frames, initially empty, how many page faults does LRU produce for the reference string 6, 2, 6, 1, 3, 6, 4, 2, 6, 1?
A) 8
B) 6
C) 10
D) 7
Answer: D) 7 — LRU: 6 F, 2 F, 6 hit, 1 F, 3 F (out 2), 6 hit, 4 F (out 1), 2 F (out 3), 6 hit, 1 F (out 4), giving 7 faults. FIFO would give 8, Optimal 6, and 10 would mean no hits at all.
Questions
What is the difference between waiting time and response time?
Waiting time is the total time a process spends in the ready queue. Response time is only the time until it first gets the CPU. In non-preemptive scheduling they are equal; in Round Robin they usually differ.
Why is the Optimal page replacement algorithm not used in real systems?
It needs to know which pages will be used in the future, which the operating system cannot know. It is used only to measure how good other algorithms are.
Is a cycle in a resource-allocation graph always a deadlock?
If every resource type has a single instance, a cycle means deadlock. If some resources have several instances, a cycle shows only that deadlock is possible.
What is the difference between paging and segmentation?
Paging splits memory into fixed-size pages that the programmer does not see. Segmentation splits a program into variable-size logical parts such as code, data and stack.