Blog
Operating System BSCS 4th Term Past Paper 2014 UOS
University of Sargodha Operating System (CMP-320) past paper for BS Computer Science 4th term 2014 (session 2012-16), with solved answers below.
Solved answers
Q1 – Short questions
(1) Context switching: saving the state (PCB: registers, program counter) of the running process and loading the saved state of the next process when the CPU switches between processes. It is pure overhead.
(2) Batch system: an early OS type where jobs with similar needs were grouped into batches and run one after another without user interaction; turnaround was long and the CPU was often idle during I/O.
(3) Software interrupt: an interrupt generated by a program — a trap or exception — either deliberately (a system call) or by an error such as division by zero or invalid memory access.
(4) Degree of multiprogramming: the number of processes in main memory at the same time; it is controlled by the long-term scheduler.
(5) Cascading termination: when a parent process terminates, the OS also terminates all of its children (and their children), because some systems do not allow a child to exist without its parent.
(6) Round robin scheduling: a preemptive algorithm for time-sharing systems in which each process in the ready queue gets the CPU for a fixed time quantum and then moves to the back of the queue.
(7) Race condition: a situation where several processes access and change shared data concurrently and the final result depends on the order in which they execute; prevented by synchronisation (critical sections).
(8) Deadlock: a set of processes is deadlocked when each process waits for a resource held by another process in the set, so none can proceed.
(9) Circular wait: a closed chain of processes P0 → P1 → … → Pn → P0 where each waits for a resource held by the next — one of the four necessary conditions for deadlock.
(10) Dynamic linking: linking of library routines is postponed until run time; a small stub in the program locates and loads the shared library (DLL/.so) when it is first called, saving memory and disk space.
(11) Compaction: shuffling memory contents so that all free memory is placed together in one large block. It is used to solve external fragmentation in contiguous allocation (possible only with dynamic relocation).
(12) Demand paging: pages are loaded into memory only when they are needed (on a page fault), not all at once when the process starts — a lazy swapper.
Q2 (a) – Interrupts and their common functions
An interrupt is a signal from hardware (device controller, timer) or software (trap, system call) that causes the CPU to stop its current work and transfer control to an interrupt service routine.
- The interrupt transfers control to the service routine through the interrupt vector (table of addresses of handlers).
- The address of the interrupted instruction and the processor state are saved, so execution can resume afterwards.
- Incoming interrupts can be disabled/masked while one is being processed, and priorities decide which is handled first.
- Uses: signalling I/O completion, timer interrupts for CPU scheduling, handling errors (traps), and requesting OS services (system calls).
Q2 (b) – Inter-process communication and its strategies
IPC is the mechanism that lets cooperating processes exchange data and synchronise their actions (for information sharing, computation speed-up, modularity, convenience).
- Shared memory: processes share a region of memory and read/write it directly; very fast, but processes must synchronise themselves (e.g. producer–consumer with a bounded buffer).
- Message passing: processes communicate with send() and receive() through the OS; easier to use and works across a network. Variations: direct or indirect (mailboxes/ports) communication; synchronous (blocking) or asynchronous (non-blocking); buffering with zero, bounded or unbounded capacity queues.
- Others: pipes, sockets, signals and remote procedure calls (RPC).
Q3 (a) – User-level vs kernel-level threads
- User-level threads are managed by a thread library without kernel support, so creating and switching them is fast; kernel-level threads are managed by the OS, so operations need system calls and are slower.
- If one user-level thread makes a blocking system call, the whole process blocks; with kernel threads, the kernel can schedule another thread of the process (and threads can run in parallel on multiprocessors).
When each is better: user-level threads are better when threads are many, short-lived and rarely block, or when portability and fast switching matter; kernel-level threads are better when threads block often on I/O or when true parallelism on multiple CPUs is required.
Q3 (b) – Three methods for passing parameters to the OS
- Registers: pass parameters in CPU registers — simplest and fastest, but limited by the number of registers.
- Block/table in memory: store parameters in a block in memory and pass its address in a register (used by Linux and Solaris).
- Stack: the program pushes parameters onto the stack and the OS pops them off.
Q4 (a) – Short-term, medium-term and long-term scheduling
- Long-term (job) scheduler: selects which jobs from the job pool are loaded into memory; controls the degree of multiprogramming; runs infrequently (seconds or minutes); balances I/O-bound and CPU-bound processes.
- Short-term (CPU) scheduler: selects which ready process gets the CPU next; runs very frequently (milliseconds), so it must be fast.
- Medium-term scheduler: swaps processes out of memory and later back in, to reduce the degree of multiprogramming or improve the process mix (used in time-sharing systems).
Q4 (b) – Binding of instructions and data to memory
- Compile time: if the memory location is known in advance, absolute code is generated; the program must be recompiled if the start address changes.
- Load time: the compiler generates relocatable code and final binding happens when the program is loaded.
- Execution time: binding is delayed until run time, so the process can be moved in memory while running; needs hardware support (MMU with base/relocation registers). Used by most modern OSs.
Q5 – Average waiting time: SJF and RR (quantum = 10 ms)
Processes (all arriving at 0): P1 = 10, P2 = 29, P3 = 3, P4 = 7, P5 = 12.
SJF: order P3 (0–3), P4 (3–10), P1 (10–20), P5 (20–32), P2 (32–61).
| Process | P1 | P2 | P3 | P4 | P5 |
|---|---|---|---|---|---|
| Waiting time (SJF) | 10 | 32 | 0 | 3 | 20 |
Average = (10 + 32 + 0 + 3 + 20) / 5 = 65 / 5 = 13 ms.
RR (q = 10): P1 0–10, P2 10–20, P3 20–23, P4 23–30, P5 30–40, P2 40–50, P5 50–52, P2 52–61.
| Process | P1 | P2 | P3 | P4 | P5 |
|---|---|---|---|---|---|
| Completion | 10 | 61 | 23 | 30 | 52 |
| Waiting time (RR) | 0 | 32 | 20 | 23 | 40 |
Average = (0 + 32 + 20 + 23 + 40) / 5 = 115 / 5 = 23 ms. SJF gives the lower average waiting time.
Q6 – First-fit, best-fit and worst-fit
Definitions: first-fit allocates the first hole that is big enough; best-fit allocates the smallest hole that is big enough (least leftover); worst-fit allocates the largest hole (largest leftover).
Partitions (in order): 100K, 600K, 200K, 300K, 500K. Processes: 409K, 236K, 125K, 514K.
| Process | First-fit | Best-fit | Worst-fit |
|---|---|---|---|
| 409K | 600K (191K left) | 500K (91K left) | 600K (191K left) |
| 236K | 300K (64K left) | 300K (64K left) | 500K (264K left) |
| 125K | 191K hole of 600K (66K left) | 200K (75K left) | 300K (175K left) |
| 514K | must wait — no hole ≥ 514K | 600K (86K left) | must wait — no hole ≥ 514K |
Only best-fit places all four processes; first-fit and worst-fit leave 514K waiting, so best-fit makes the most efficient use of memory here.
Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
