Blog
Operating System Past Paper 2016 – BS IT 5th Term UOS (CMP-3621)
University of Sargodha BS IT 5th term Operating System (CMP-3621) paper, 2016 exam (60 marks, 2:30 hours). The paper is retyped below without the handwritten notes, followed by solved answers for all 12 short questions and all five subjective questions: system calls, process communication, multithreading models, multilevel queues, the Banker’s algorithm, optimal page replacement (8 and 7 faults, checked by simulation), interrupts and paging.
Solved answers
Q1 – Short answers (12 × 2)
1. States of a process. New, Ready, Running, Waiting (Blocked) and Terminated. A process moves New → Ready → Running; from Running it may go to Waiting (I/O), back to Ready (interrupt/time-out) or Terminated.
2. Advantages of multiprocessor systems. Increased throughput, economy of scale (shared memory, disks, power supply) and increased reliability – if one processor fails the others continue (graceful degradation).
3. Thrashing. A situation in which a process does not have enough frames, so it page-faults continuously and spends more time swapping pages in and out than executing; CPU utilisation drops sharply. Cured by the working-set model or page-fault-frequency control.
4. Short-, medium- and long-term schedulers. Long-term (job) scheduler selects which jobs enter memory (controls degree of multiprogramming; runs rarely). Short-term (CPU) scheduler selects which ready process gets the CPU next (runs very often, must be fast). Medium-term scheduler swaps processes out of and back into memory to reduce load.
5. Virtual memory. A technique that lets a process execute even if it is not completely in main memory, giving each process a large logical address space; pages are loaded on demand from disk (demand paging).
6. Round Robin scheduling. Pre-emptive FCFS: each process gets a fixed time quantum (e.g. 10–100 ms); if it is not finished it goes to the back of the ready queue. Fair and good for time-sharing; performance depends on quantum size.
7. PCB. Process Control Block – the kernel data structure representing a process: process state, PID, program counter, CPU registers, scheduling information, memory-management information, accounting and I/O status.
8. Multitasking. Running several tasks (processes) apparently at the same time by rapidly switching the CPU among them (time-sharing), so the user can interact with each program.
9. Race condition. When several processes/threads access and change shared data concurrently and the result depends on the order of execution, e.g. two threads doing count++ at once. Prevented by synchronisation (critical sections, mutex, semaphores).
10. Monitor. A high-level synchronisation construct (abstract data type) whose procedures can be executed by only one process at a time; it has condition variables with wait() and signal() operations.
11. Kernel. The core of the operating system that is always in memory and runs in privileged mode; it manages processes, memory, devices, files and system calls.
12. User-level threads – pros and cons. Advantages: fast creation and switching (no kernel involvement), can run on any OS, application-specific scheduling. Disadvantages: if one thread makes a blocking system call the whole process blocks, and they cannot run in parallel on multiple CPUs because the kernel sees only one process.
Q1 (a) – Types of system calls
A system call is the interface through which a program requests a service from the OS kernel.
- Process control – create/terminate process, load, execute, wait, allocate memory:
fork(),exec(),exit(),wait(). - File management – create, delete, open, close, read, write files:
open(),read(),write(),close(). - Device management – request/release device, read/write,
ioctl(). - Information maintenance – get/set time, date, process attributes:
getpid(),time(). - Communication – create/delete connections, send/receive messages, shared memory:
pipe(),shmget(),socket(). - Protection – set/get permissions:
chmod(),umask().
Q1 (b) – Direct and indirect communication
Direct communication: processes name each other explicitly – send(P, message), receive(Q, message). A link is established automatically between exactly one pair of processes. Disadvantage: changing a process name requires changing all references.
Indirect communication: messages are sent to and received from mailboxes (ports) – send(A, message), receive(A, message) where A is a mailbox. A link may be shared by many processes, and a pair of processes may share several mailboxes; mailboxes can be owned by a process or the OS.
Q2 (a) – Multithreading models
- Many-to-one: many user threads map to one kernel thread. Thread management is efficient in user space, but one blocking call blocks all and threads cannot run in parallel (e.g. Green threads).
- One-to-one: each user thread maps to its own kernel thread. More concurrency and parallelism on multiprocessors; overhead of creating kernel threads limits their number (Windows, Linux).
- Many-to-many: many user threads are multiplexed onto a smaller or equal number of kernel threads; combines the benefits of both. A variant, the two-level model, also allows a user thread to be bound to a kernel thread.
Q2 (b) – Multilevel queue vs multilevel feedback queue
| Multilevel queue | Multilevel feedback queue |
|---|---|
| Ready queue split into several queues (e.g. system, interactive, batch), each with its own algorithm | Also several queues with different priorities/quanta |
| A process is permanently assigned to one queue | A process can move between queues based on its CPU behaviour |
| Simple, low overhead, but can cause starvation of low-priority queues | CPU-bound processes are moved down, I/O-bound and aged processes moved up – prevents starvation |
| Inflexible | Most general and flexible, but most complex to configure |
Q3 – Safe/unsafe state and Banker’s algorithm
A state is safe if the system can allocate resources to every process (up to its maximum) in some order and still avoid deadlock – i.e. there exists a safe sequence <P1, P2, …, Pn> such that each Pi’s remaining need can be met by currently available resources plus those held by all Pj before it. An unsafe state has no such sequence; it may lead to deadlock (not every unsafe state is a deadlock, but every deadlock is unsafe).
Banker’s algorithm (deadlock avoidance, multiple instances). Data: Available[m], Max[n][m], Allocation[n][m], Need = Max − Allocation.
- Safety algorithm: Work = Available; Finish[i] = false. Find i with Finish[i] = false and Need[i] ≤ Work; then Work = Work + Allocation[i], Finish[i] = true; repeat. If all Finish are true, the state is safe.
- Resource-request algorithm: when Pi requests Request[i]: if Request ≤ Need and Request ≤ Available, pretend to allocate (Available −= Request; Allocation += Request; Need −= Request) and run the safety algorithm; grant if safe, otherwise make Pi wait and restore the old state.
Example: 5 processes, resources A = 10, B = 5, C = 7; Available = (3, 3, 2).
| Process | Allocation | Max | Need |
|---|---|---|---|
| P0 | 0 1 0 | 7 5 3 | 7 4 3 |
| P1 | 2 0 0 | 3 2 2 | 1 2 2 |
| P2 | 3 0 2 | 9 0 2 | 6 0 0 |
| P3 | 2 1 1 | 2 2 2 | 0 1 1 |
| P4 | 0 0 2 | 4 3 3 | 4 3 1 |
Work = (3,3,2): P1 (need 1,2,2) → Work = (5,3,2); P3 → (7,4,3); P4 → (7,4,5); P0 → (7,5,5); P2 → (10,5,7). Safe sequence <P1, P3, P4, P0, P2>, so the state is safe.
Q4 – Optimal page replacement
Reference string: 1, 2, 1, 1, 1, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6. OPT replaces the page that will not be used for the longest time in future.
(a) Four frames (F = fault):
| Ref | 1 | 2 | 1 | 1 | 1 | 3 | 4 | 2 | 1 | 5 | 6 | 2 | 1 | 2 | 3 | 7 | 6 | 3 | 2 | 1 | 2 | 3 | 6 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Frames | 1 | 1 2 | 1 2 3 | 1 2 3 4 | 1 2 3 5 | 1 2 3 6 | 7 2 3 6 | 1 2 3 6 | |||||||||||||||
| Fault | F | F | F | F | F | F | F | F |
Page 5 replaces 4 (never used again), 6 replaces 5, 7 replaces 1 (1 is needed latest among 1, 2, 3, 6), and 1 later replaces 7. Total = 8 page faults.
(b) Seven frames: there are only 7 distinct pages (1–7), so only the first reference of each page faults (compulsory misses): 1, 2, 3, 4, 5, 6, 7 → 7 page faults. (Both results checked with a simulation program.)
Q5 (a) – Interrupts and traps
Purpose of interrupts: to let hardware devices (keyboard, disk, timer, network) signal the CPU that they need attention, so the CPU does not waste time polling; the CPU saves its state, runs the interrupt service routine through the interrupt vector, and resumes. They enable I/O overlap, time-sharing (timer interrupt) and event-driven operation.
| Interrupt | Trap (exception) |
|---|---|
| Generated by hardware devices, external to the running program | Generated by software/the running program |
| Asynchronous – can occur at any time | Synchronous – caused by a specific instruction |
| e.g. I/O completion, timer, keyboard | e.g. divide by zero, invalid memory access, system call |
Yes, a user program can generate traps intentionally – this is how system calls work (e.g. int 0x80/syscall instruction) to switch to kernel mode and request OS services; debuggers also use trap instructions for breakpoints.
Q5 (b) – Paging
Paging is a memory-management scheme that removes external fragmentation: physical memory is divided into fixed-size frames and logical memory into pages of the same size. A logical address = (page number p, offset d). The page table maps each page to a frame: physical address = frame × page size + d. A TLB (translation look-aside buffer) caches recent translations to speed up access. Paging allows non-contiguous allocation and supports virtual memory, but has internal fragmentation in the last page and page-table overhead.
