Skip to content

Operating Systems

~12 min

In short

Operating system questions test whether you understand what happens when many programs share one machine: who runs next, who gets memory, and what goes wrong when they wait on each other. You need clear definitions, the reason each one exists, and a small example. Practise explaining each idea in two sentences first, then add detail if asked.

Checklist

  • [ ] What is the difference between a process and a thread?
  • [ ] What information does a process control block (PCB) hold?
  • [ ] What is a context switch, and why does it cost time?
  • [ ] Draw the process states. What moves a process from running to waiting?
  • [ ] Compare first come first served, shortest job first and round robin.
  • [ ] What is starvation, and how does aging fix it?
  • [ ] What is preemptive scheduling? Give one example of each kind.
  • [ ] What is a race condition? Give a small example with a shared counter.
  • [ ] What is the difference between a mutex and a semaphore?
  • [ ] Name the four Coffman conditions for deadlock.
  • [ ] How can deadlock be prevented? How is it avoided?
  • [ ] What is the Banker's algorithm trying to ensure?
  • [ ] What is virtual memory?
  • [ ] How does paging work, and what is a page fault?
  • [ ] What is the TLB for?
  • [ ] What is the difference between internal and external fragmentation?
  • [ ] What is thrashing, and what causes it?
  • [ ] Compare FIFO and LRU page replacement.
  • [ ] What is a system call? What are user mode and kernel mode?
  • [ ] What is a zombie process, and how does it differ from an orphan?

Key ideas

Processes and threads. A process is a running program with its own address space and resources. A thread is a unit of execution inside a process. Threads of the same process share its memory and open files, which makes communication cheap but means they must synchronise access to shared data. Creating a process and switching between processes usually costs more than the same work with threads, because the address space changes too.

States and context switches. A process is new, ready, running, waiting (blocked on I/O or an event) or terminated. The scheduler picks a ready process to run. A context switch saves the registers and state of one process and loads another's. The OS keeps this bookkeeping in the PCB.

Scheduling. First come first served is simple, but one long job can delay everyone (the convoy effect). Shortest job first minimises average waiting time but needs run-time estimates and can starve long jobs. Round robin gives each ready process a time quantum. A small quantum improves responsiveness but adds switching overhead. Priority scheduling can starve low-priority processes; aging raises a process's priority the longer it waits.

Synchronisation. When threads share data, unsynchronised updates can interleave and corrupt the result. This is a race condition. A mutex lets one thread hold a lock at a time. A semaphore is a counter that limits how many threads may enter a section. A mutex is released by the thread that took it; a semaphore has no such ownership.

Deadlock. Deadlock needs all four conditions at once: mutual exclusion, hold and wait, no preemption and circular wait. Two threads that take lock A then lock B, while the other takes B then A, can deadlock this way. Prevention breaks one condition, for example by always acquiring locks in one global order, which breaks circular wait. Avoidance, such as the Banker's algorithm, checks before each allocation that the system stays in a safe state. Detection and recovery lets deadlock happen, then aborts or rolls back something to break it.

Memory. Virtual memory gives each process its own address space, and the hardware and OS map virtual pages to physical frames. Paging splits memory into fixed-size pages, and a page table records the mapping. Touching a page that is not in RAM causes a page fault; the OS loads it, possibly evicting another page. The TLB caches recent translations so the page table is not read on every access. Replacement policies such as FIFO and LRU decide which page to evict. If a system spends most of its time moving pages in and out, it is thrashing.

Fragmentation. Paging can waste space inside a process's last page (internal fragmentation). Contiguous allocation can leave gaps between allocations that are too small to use (external fragmentation).

User and kernel mode. Programs run in user mode and ask the OS for services through system calls, which switch the CPU into kernel mode. This boundary protects the OS and other processes.

Zombies and orphans. A zombie has exited, but its parent has not yet read its exit status, so its entry stays in the process table. An orphan is a process whose parent has exited. On Unix-like systems, init or a designated subreaper adopts it.

Try this

Write a short Python script with two threads. Thread A takes lock 1, sleeps 0.1 seconds, then takes lock 2. Thread B takes lock 2, sleeps 0.1 seconds, then takes lock 1. Run it and watch it hang. Then change B to take lock 1 first and run it again. Write two sentences on which Coffman condition you broke.

Sources