Blog
Operating System BSIT 5th Term Past Paper 2019 UOS
University of Sargodha BS IT 5th term Operating System (CMP-3621) past paper 2019, retyped from the original paper, with solved answers below.
Solved answers
Q1 – Short questions
(i) Necessary conditions for deadlock (Coffman): mutual exclusion, hold and wait, no preemption and circular wait. All four must hold at the same time.
(ii) Critical section: the part of a program that accesses shared data (variables, files, buffers) and must not be executed by more than one process at a time. Examples: updating a shared counter or bank balance, inserting into a shared buffer in producer–consumer.
(iii) Semaphore: an integer synchronisation variable accessed only through two atomic operations: wait(S) / P — while S ≤ 0 wait; then S−−; and signal(S) / V — S++. A binary semaphore (0/1) works as a mutex lock; a counting semaphore controls access to n instances of a resource.
(iv) Interrupt timeline: the CPU executes the user process; an I/O device finishes a transfer and raises an interrupt; the CPU saves the process state, jumps to the interrupt service routine, handles the I/O completion, then restores the state and resumes the user process. On a time axis: user process running → I/O device busy → transfer done → interrupt processing on CPU → user process continues while the device is idle until the next request.
(v) Paging and page tables: paging divides logical memory into fixed-size pages and physical memory into frames of the same size, so a process need not be contiguous and external fragmentation is avoided. The page table maps each page number to a frame number for address translation.
(vi) Disadvantages of single contiguous allocation: only one user program in memory (no multiprogramming), CPU and memory are poorly utilised, a program larger than the free area cannot run, and unused memory in the partition is wasted.
(vii) User-level vs kernel-level threads: user-level threads are managed by a thread library without kernel knowledge — fast to create and switch, but a blocking system call blocks the whole process and they cannot run in parallel on multiple CPUs. Kernel-level threads are managed by the OS — slower to create and switch, but one blocking thread does not block others and they can run on multiple processors.
(viii) Demand paging and performance: pages are loaded only when referenced, so memory use and start-up time drop and more processes fit in memory. But each page fault costs disk I/O. Effective access time = (1 − p) × memory access + p × page-fault time, so even a small fault rate p slows the system greatly; too many faults cause thrashing.
(ix) No. A zero-capacity buffer cannot hold any message; the sender must block until the receiver takes the message (rendezvous). Reserving an array for it would waste memory because nothing is ever stored in it.
(x) Safe, unsafe and deadlock states: a state is safe if there exists a sequence in which all processes can obtain their maximum needs and finish. An unsafe state has no such guaranteed sequence — it may lead to deadlock. The deadlock states are a subset of the unsafe states; avoidance algorithms (Banker’s) keep the system in safe states.
(xi) Multithreading: a process having several threads of execution that share its code, data and files but have their own registers and stack. Benefits: responsiveness, resource sharing, economy (cheaper than processes) and scalability on multiprocessors.
(xii) Real-time embedded systems: dedicated computers inside devices (cars, medical equipment, industrial controllers) that must respond within strict time limits. They are important because correctness depends on meeting deadlines, often affecting safety, and they must be reliable with limited resources.
Q2 – SJF and Round-Robin
All processes are assumed to arrive at time 0.
a) SJF: order P4 (2), P2 (3), P1 (4), P3 (4). Gantt chart: P4 0–2 | P2 2–5 | P1 5–9 | P3 9–13.
| Process | Burst | Waiting time |
|---|---|---|
| P1 | 4 | 5 |
| P2 | 3 | 2 |
| P3 | 4 | 9 |
| P4 | 2 | 0 |
Average waiting time = (5 + 2 + 9 + 0) / 4 = 16 / 4 = 4 units.
b) Round-Robin (time slice 2): Gantt chart: P1 0–2 | P2 2–4 | P3 4–6 | P4 6–8 | P1 8–10 | P2 10–11 | P3 11–13.
| Process | Completion | Waiting = completion − burst |
|---|---|---|
| P1 | 10 | 6 |
| P2 | 11 | 8 |
| P3 | 13 | 9 |
| P4 | 8 | 6 |
Average waiting time = (6 + 8 + 9 + 6) / 4 = 29 / 4 = 7.25 units.
Q3 – Address bits
Pages = 256 = 2⁸, page size = 4 KB = 2¹² bytes, frames = 64 = 2⁶.
a) Logical address = page bits + offset bits = 8 + 12 = 20 bits.
b) Physical address = frame bits + offset bits = 6 + 12 = 18 bits.
Q4 – Directory structure, open() and close()
A directory is a table that maps file names to their file-control blocks. Common structures:
- Single-level: one directory for all users — simple, but names must be unique and grouping is impossible.
- Two-level: a separate user file directory for each user under a master directory — isolates users, but no sub-grouping.
- Tree-structured: directories contain files and subdirectories; absolute and relative path names; most common.
- Acyclic-graph: allows shared files/subdirectories through links.
- General graph: allows cycles; needs garbage collection and cycle checks.
open(): searches the directory for the file, checks permissions, copies its FCB into the open-file table and returns a file descriptor/handle, so later reads and writes need not search the directory again. close(): writes back changed information, removes the entry from the per-process open-file table (decrementing the open count in the system-wide table) and frees resources.
Q5
a) (i) Batch system: jobs with similar needs grouped and run without user interaction; high throughput, long turnaround. (ii) Real-time system: must produce results within fixed deadlines; hard real-time (deadline must be met) or soft real-time. (iii) Embedded system: a dedicated computer inside a device, with limited memory and power, often real-time, little or no user interface. (iv) Distributed system: independent computers connected by a network that share resources and computation; provides resource sharing, speed-up, reliability and communication.
b) Dining philosophers problem: five philosophers sit around a table with five chopsticks; each needs both neighbouring chopsticks to eat. If every philosopher picks up the left chopstick at once, all wait forever (deadlock). Monitor solution: each philosopher has a state (THINKING, HUNGRY, EATING) and a condition variable self[i]. pickup(i) sets state[i] = HUNGRY, calls test(i), and waits on self[i] if not EATING. test(i) lets i eat only if both neighbours are not eating, then signals self[i]. putdown(i) sets state[i] = THINKING and tests both neighbours. Because a philosopher takes both chopsticks only when both are free inside the monitor, deadlock cannot occur (starvation is still possible).
Q6
a) P0 locks S then Q; P1 locks Q then S (opposite order). Deadlock occurs if P0 executes wait(S) and then P1 executes wait(Q) before P0 reaches wait(Q): P0 holds S and waits for Q, P1 holds Q and waits for S — circular wait. No deadlock if one process completes both wait operations before the other starts, e.g. P0 runs wait(S), wait(Q) before P1’s wait(Q); then P1 simply blocks until P0 signals. Using the same order (S then Q) in both processes removes the possibility.
b) Long-term scheduler (job scheduler) selects jobs from disk to load into memory and controls the degree of multiprogramming; runs infrequently. Short-term scheduler (CPU scheduler) selects one ready process to run on the CPU; runs very frequently, so it must be fast. Medium-term scheduler swaps processes out of memory and back in to reduce the degree of multiprogramming or improve the process mix.
Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
