Skip to content
wins.solutions

Operating Systems Important Questions (with Answers)

Important operating systems questions with concise answers, grouped by unit: processes and threads, CPU scheduling, synchronization, deadlocks, memory management and file systems, with solved scheduling, Banker's algorithm, page replacement and disk scheduling problems.

FreePractice sheet12 min readAll levelsBy wins.solutions teamUpdated

Standard operating systems questions from university exams and technical interviews, grouped by unit. Each answer gives the definition first, then the reasoning or the working, and numericals show every step so you can check your method as well as your result.

Try each question before opening its answer, and tick it once you can answer without looking. Progress is saved in this browser.

Processes and threads

A process is the unit of resource ownership; a thread is the unit of scheduling. These questions test the life cycle of a process and what the kernel stores to pause and resume it.

Processes and threads

0 / 5 done
  1. What is the difference between a process and a thread?Easy
    Show answer

    A process is a program in execution with its own address space (code, data, heap, stack) and resources such as open files, described by its process control block. A thread runs inside a process with its own program counter, registers and stack, but shares the code, data, heap and open files with the process's other threads.

    Threads are cheaper to create and switch, and communicate through shared memory; processes need IPC. Processes are isolated, while a crashing thread can bring down its whole process.

  2. Explain the process state diagram.Easy
    Show answer

    New → Ready (admitted), Ready → Running (dispatched), Running → Ready (preempted, for example when the time slice ends), Running → Waiting (I/O or event wait), Waiting → Ready (I/O done or event occurred), Running → Terminated (exit).

    A waiting process never goes straight to Running; it returns through Ready. Systems that swap processes out add suspended-ready and suspended-waiting states.

  3. What is a PCB, and what happens during a context switch?Medium
    Show answer

    The process control block stores the process ID, state, program counter, registers, scheduling information, memory-management information (page table pointer), accounting data and open files.

    In a context switch, the kernel saves the running process's state into its PCB and loads the next one's. It is pure overhead, and switching address spaces can also flush the TLB and leave caches cold. Switching between threads of one process avoids the address-space change.

  4. What does fork() return, and how many times is hello printed?Medium
    Show answer
    fork();
    fork();
    fork();
    printf("hello\n");

    fork() returns the child's PID in the parent, 0 in the child, and −1 if no child was created. Each call doubles the processes running the remaining code, so n calls give 2ⁿ processes: here 8 (the original and 7 children), so hello is printed 8 times.

  5. What are zombie and orphan processes?Easy
    Show answer

    A zombie has exited, but its parent has not collected its exit status with wait(), so its process-table entry remains. An orphan is still running after its parent has exited; on Linux it is re-parented, usually to init or systemd (PID 1), which reaps it when it exits.

CPU scheduling

Scheduling questions are mostly numerical. With arrival time (AT), burst time (BT) and completion time (CT): turnaround time TAT = CT − AT, waiting time WT = TAT − BT, and response time = first run − AT. Draw the Gantt chart first; every other number comes from it.

CPU scheduling

0 / 4 done
  1. What is the difference between preemptive and non-preemptive scheduling?Easy
    Show answer

    In non-preemptive scheduling a process keeps the CPU until it exits or blocks (FCFS, SJF). In preemptive scheduling the OS can take the CPU away on a timer interrupt or when a higher-priority or shorter process arrives (Round Robin, SRTF). Preemption improves response time at the cost of more context switches.

    Under FCFS, short processes stuck behind one long CPU-bound process cause the convoy effect: long waits and idle I/O devices.

  2. Why is SJF optimal, and why can't it be used directly?Medium
    Show answer

    Moving a short job ahead of a longer one cuts the short job's wait by the long job's burst but adds only the short job's burst to the long job's wait, so the average falls. Hence SJF minimizes average waiting time for processes available together; its preemptive form, SRTF, extends this to staggered arrivals.

    But the next burst length is unknown, so it is predicted: τ(n+1) = α × t(n) + (1 − α) × τ(n), where t(n) is the last actual burst and 0 ≤ α ≤ 1. SJF can also starve long processes.

  3. How does the time quantum affect Round Robin?Medium
    Show answer

    Each process runs for at most one quantum q, then rejoins the back of the queue, so with n processes none waits more than (n − 1) × q for its next turn. A very large q turns Round Robin into FCFS; a very small q spends too much time on context switches. The quantum should be large compared with the switch time and, ideally, longer than most CPU bursts.

  4. What are starvation and aging, and how does a multilevel feedback queue use them?Hard
    Show answer

    Starvation is indefinite waiting while others are always chosen first, as with low-priority processes under priority scheduling. Aging gradually raises the priority of processes that have waited long.

    A multilevel feedback queue has queues of decreasing priority. New processes enter the top queue, which has a short quantum; a process that uses its whole quantum moves down to a queue with a longer one. CPU-bound processes sink while interactive ones stay high, and aging moves long-waiting processes back up.

Worked example: FCFS, SJF and Round Robin

ProcessArrival timeBurst time
P105
P213
P328
P436
FCFS       | P1 0-5 | P2 5-8 | P3 8-16 | P4 16-22 |
SJF        | P1 0-5 | P2 5-8 | P4 8-14 | P3 14-22 |
RR (q = 2) | P1 0-2 | P2 2-4 | P3 4-6 | P1 6-8 | P4 8-10 | P2 10-11 |
           | P3 11-13 | P1 13-14 | P4 14-16 | P3 16-18 | P4 18-20 | P3 20-22 |

In non-preemptive SJF, only P1 has arrived at t = 0, so it runs to completion; at t = 5 the shortest waiting job is P2, then P4, then P3. In Round Robin, when a new arrival and a preempted process join the queue at the same instant, the new arrival goes first. This is the usual convention, but state it in your answer.

ProcessATBTFCFS CT / TAT / WTSJF CT / TAT / WTRR CT / TAT / WT
P1055 / 5 / 05 / 5 / 014 / 14 / 9
P2138 / 7 / 48 / 7 / 411 / 10 / 7
P32816 / 14 / 622 / 20 / 1222 / 20 / 12
P43622 / 19 / 1314 / 11 / 520 / 17 / 11
Total– / 45 / 23– / 43 / 21– / 61 / 39
AverageFCFSSJFRR (q = 2)
Turnaround time11.2510.7515.25
Waiting time5.755.259.75
Response time5.755.25(0 + 1 + 2 + 5) / 4 = 2.00

SJF gives the lowest waiting time of the three. Round Robin has the worst waiting and turnaround times here but the best response time, which is why time-sharing systems use it. Preemptive SRTF would cut average waiting time to 5.0, because P2 (burst 3) preempts P1 (4 remaining) at t = 1.

Synchronization

Synchronization is about shared data whose final value depends on the order in which processes happen to run. Know the three requirements for a critical-section solution and be able to write the classic semaphore solutions from memory.

Synchronization

0 / 5 done
  1. What is a race condition, and what must a critical-section solution satisfy?Easy
    Show answer

    A race condition occurs when the result depends on the timing of accesses to shared data. count++ is a load, an add and a store; if two threads interleave them, an increment is lost. A critical-section solution must provide:

    1. Mutual exclusion: at most one process in the critical section.
    2. Progress: if it is free, only processes wanting to enter decide who goes next, without indefinite delay.
    3. Bounded waiting: a limit on how often others can enter before a waiting process does.
  2. What is the difference between a mutex and a semaphore?Easy
    Show answer

    A mutex is a lock with an owner: only the thread that locked it may unlock it. A semaphore is an integer used only through wait(), which decrements it and blocks when no units are left, and signal(), which increments it and wakes a waiter. A counting semaphore set to N guards N identical resources. A binary semaphore has no owner, so it also works for signalling between processes.

  3. Explain Peterson's solution.Medium
    Show answer
    // Process i; the other process is j = 1 - i
    flag[i] = true;          // I want to enter
    turn = j;                // but you go first
    while (flag[j] && turn == j)
        ;                    // busy wait
    /* critical section */
    flag[i] = false;

    If both processes want to enter, turn holds only one value, so the one that wrote it last waits: mutual exclusion. A waiting process enters as soon as the other resets its flag, giving progress and bounded waiting. Modern CPUs reorder memory operations, so real systems use atomic instructions such as compare-and-swap instead.

  4. Solve the bounded-buffer producer-consumer problem with semaphores.Hard
    Show answer

    Use mutex = 1, empty = N (free slots) and full = 0 (filled slots).

    // Producer                 // Consumer
    wait(empty);                wait(full);
    wait(mutex);                wait(mutex);
    /* add item */              /* remove item */
    signal(mutex);              signal(mutex);
    signal(full);               signal(empty);

    If the producer took mutex before empty while the buffer was full, it would block holding the mutex, the consumer could never free a slot, and both would deadlock.

  5. Where is the deadlock in the dining philosophers problem, and how can it be avoided?Hard
    Show answer

    Each of five philosophers picks up the left chopstick, then the right. If all pick up their left one at once, each waits forever for the right: circular wait. Fixes: allow at most four at the table; pick up both chopsticks only when both are free, inside a critical section; or have odd-numbered philosophers pick left first and even-numbered right first. Each prevents deadlock, but starvation needs a separate fairness guarantee.

Deadlocks

A deadlock is a set of processes each waiting for a resource held by another in the set. The theory centres on the four necessary conditions and the ways of handling them; the standard numerical is the Banker's algorithm.

Deadlocks

0 / 5 done
  1. What are the four necessary conditions for deadlock?Easy
    Show answer
    1. Mutual exclusion: a resource can be held by only one process at a time.
    2. Hold and wait: a process holds resources while waiting for more.
    3. No preemption: resources are released only voluntarily.
    4. Circular wait: a cycle of processes, each waiting for a resource held by the next.

    All four must hold at once, so preventing any one prevents deadlock.

  2. Compare deadlock prevention, avoidance and detection.Medium
    Show answer
    • Prevention makes a condition impossible, for example by requesting every resource at once (no hold and wait) or by numbering resource types and requesting them only in increasing order (no circular wait).
    • Avoidance needs each process's maximum claim in advance and grants a request only if the resulting state is safe (Banker's algorithm).
    • Detection and recovery allows deadlocks, finds them periodically with a wait-for graph or a detection algorithm, then aborts processes or preempts resources.
  3. Banker's algorithm: is this state safe, and can a request be granted?Hard
    Show answer

    Four processes share resources A, B and C, with totals (8, 5, 5).

          Allocation   Max       Need = Max - Allocation
          A  B  C      A  B  C   A  B  C
    P0    1  0  1      4  2  2   3  2  1
    P1    2  1  1      3  2  2   1  1  1
    P2    2  1  2      5  1  3   3  0  1
    P3    1  1  0      2  2  1   1  1  1
     
    Available = (8, 5, 5) - (6, 3, 4) = (2, 2, 1)

    Safety. Start with Work = (2, 2, 1). P0's need (3, 2, 1) does not fit, but P1's (1, 1, 1) does, so P1 finishes and Work = (4, 3, 2). Then P2 fits: Work = (6, 4, 4). Then P3: Work = (7, 5, 4). Then P0: Work = (8, 5, 5). The state is safe, with safe sequence P1, P2, P3, P0.

    Request. P0 asks for (2, 0, 0). That is within its need and within Available, so pretend to grant it: Available becomes (0, 2, 1). Every process still needs at least one A, so none can finish. The state would be unsafe, so P0 must wait.

  4. Does a cycle in a resource-allocation graph always mean deadlock?Medium
    Show answer

    Only if every resource type has a single instance; then a cycle is necessary and sufficient. With multiple instances, a cycle is necessary but not sufficient, because a process outside the cycle may release an instance and break it. With no cycle, there is no deadlock.

  5. How many resource instances guarantee that deadlock cannot occur?Hard
    Show answer

    With n processes each needing at most k instances, the worst case is every process holding k − 1 and waiting for one more. One extra instance lets a process finish, so n(k − 1) + 1 instances guarantee no deadlock. For 3 processes needing up to 4 drives each: 3 × 3 + 1 = 10. With 9, each can hold 3 and wait forever.

Memory management

Memory management mixes definitions with short numericals on address translation, access time and page replacement. Write the formula before substituting numbers, so your method is visible even if an arithmetic step slips.

Memory management

0 / 5 done
  1. What is the difference between internal and external fragmentation?Easy
    Show answer

    Internal fragmentation is unused space inside an allocated block, such as the unused part of a process's last page. External fragmentation is free memory split into pieces each too small for a request, though together they would be enough; variable partitions and segmentation cause it. Compaction removes external fragmentation at a high cost, and paging avoids it entirely.

  2. How is a logical address translated to a physical address under paging?Medium
    Show answer

    The logical address splits into page number p and offset d, and the page table maps p to frame f: physical address = f × page size + d.

    With 1 KB pages and page 2 in frame 5, logical address 3000 has p = ⌊3000 / 1024⌋ = 2 and d = 3000 − 2048 = 952, so the physical address is 5 × 1024 + 952 = 6072.

    With 32-bit addresses and 4 KB pages, the page number has 20 bits, so a flat page table has 2²⁰ entries, or 4 MB at 4 bytes each. Multi-level page tables avoid storing all of it.

  3. Calculate the effective memory access time with a TLB.Medium
    Show answer

    With hit ratio h, TLB lookup time t and memory access time m: EAT = h × (t + m) + (1 − h) × (t + 2m), since a miss needs an extra access to the page table. For t = 20 ns, m = 100 ns and h = 0.8: 0.8 × 120 + 0.2 × 220 = 140 ns, against 200 ns without a TLB. Some textbooks ignore t, so state your assumption.

  4. Count page faults for FIFO, LRU and Optimal replacement.Hard
    Show answer
    Reference   1  2  3  4  1  2  5  1  2  3  4  5
    FIFO (3)    F  F  F  F  F  F  F  -  -  F  F  -    9 faults
    LRU  (3)    F  F  F  F  F  F  F  -  -  F  F  F   10 faults
    OPT  (3)    F  F  F  F  -  -  F  -  -  F  F  -    7 faults
    FIFO (4)    F  F  F  F  -  -  F  F  F  F  F  F   10 faults

    FIFO evicts the oldest page, LRU the least recently used, and Optimal the page used furthest in the future, which needs future knowledge and so serves as a benchmark. The last row shows Belady's anomaly: FIFO with 4 frames faults more than with 3. LRU and Optimal never show it.

  5. What is thrashing, and how is it controlled?Medium
    Show answer

    Thrashing is when processes spend more time paging than executing, because the available frames are fewer than their combined working sets. CPU utilization drops, and admitting more processes makes it worse. Control it by giving each process frames for its working set and suspending processes when demand exceeds memory, or by adjusting frames to keep each process's page-fault frequency between two bounds.

File systems

File system questions cover how a file's blocks are placed and found on disk, and how the disk head is scheduled. The numericals follow fixed methods, so practise them until the steps are automatic.

File systems

0 / 4 done
  1. Compare contiguous, linked and indexed allocation.Medium
    Show answer
    • Contiguous: fast sequential and direct access, but external fragmentation and files are hard to grow.
    • Linked: each block points to the next, so there is no external fragmentation, but direct access means following the chain and one bad pointer loses the rest. FAT keeps the pointers in a table.
    • Indexed: an index block lists the file's blocks, giving direct access without external fragmentation at the cost of the index block. Unix inodes combine direct and indirect pointers.
  2. What is the maximum file size with an inode?Hard
    Show answer

    An inode has 12 direct pointers plus single, double and triple indirect pointers. With 4 KB blocks and 4-byte pointers, a block holds 1024 pointers.

    Maximum = (12 + 1024 + 1024² + 1024³) × 4 KB = 48 KB + 4 MB + 4 GB + 4 TB, about 4 TB. A real file system may set a lower limit through other fields.

  3. What is the difference between a hard link and a symbolic link?Medium
    Show answer

    A hard link is another directory entry for the same inode. The data is freed only when the inode's link count reaches 0, so deleting the original name does not break it; it cannot cross file systems. A symbolic link is a small file holding a path. It can cross file systems and point to directories, but dangles if the target is removed.

  4. Calculate total head movement for FCFS, SSTF, SCAN and LOOK.Hard
    Show answer

    Cylinders 0 to 199, head at 50 moving up, queue 82, 170, 43, 140, 24, 16, 190.

    FCFS  50→82→170→43→140→24→16→190   32+88+127+97+116+8+174 = 642
    SSTF  50→43→24→16→82→140→170→190   7+19+8+66+58+30+20     = 208
    SCAN  50→...→190→199→43→24→16      149 + 183              = 332
    LOOK  50→...→190→43→24→16          140 + 174              = 314

    SCAN reaches the end of the disk before reversing; LOOK reverses at the last request. For C-SCAN, textbooks differ on counting the return seek, so state your convention.

  • Free

    DBMS Quick Revision Notes

    Exam-focused DBMS revision notes covering keys, the ER model, relational algebra, SQL, normalization up to BCNF with one worked example, transactions, concurrency control and indexing, ending with a last-minute checklist.

    GuideWebAll levels

    FreeRead