Operating Systems Interview Questions
A Process is an execution instance of a program running on an operating system. A Thread is a lightweight unit of execution managed independently by the scheduler within a process.
| Feature | Process | Thread |
|---|---|---|
| Memory Space | Has its own independent address space (code, data, heap). | Shares the address space of its parent process (shares heap, code, data). |
| Creation Cost | Expensive (requires OS allocation of resource structures). | Cheap (reuses existing process descriptors). |
| Context Switching | Slow (involves page-table updates, TLB flushes). | Fast (only registers and stack pointer are swapped). |
| Crash Safety | Isolation protects others; one process crash doesn't affect others. | A crash in one thread can corrupt shared process space, crashing all threads. |
Key Points
Address Space, Context Switching, Inter-Process Communication (IPC), Safety
Common Follow-ups
How do processes communicate with each other (IPC) compared to threads?
A Deadlock is a state where a set of processes are blocked because each process is holding a resource and waiting for another resource held by some other process in the set.
For a deadlock to occur, the following four Coffman conditions must hold simultaneously:
- Mutual Exclusion: At least one resource must be held in a non-shareable mode (only one process can use it at a time).
- Hold and Wait: A process must currently hold at least one resource and be waiting to acquire additional resources that are being held by other processes.
- No Preemption: Resources cannot be forcibly taken from a process holding them; they must be released voluntarily.
- Circular Wait: A process must be waiting for a resource held by a second process, which is waiting for a resource held by a third process... up to the first process, forming a circular chain.
Key Points
Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait
Common Follow-ups
How does the Banker's Algorithm prevent deadlocks dynamically?
Virtual Memory is a memory management technique that gives each process the illusion of a large, contiguous address space, while the actual physical memory (RAM) may be fragmented and smaller than the virtual address space.
Paging: - The virtual address space is divided into fixed-size blocks called pages (typically 4 KB). - Physical memory is divided into frames of the same size. - A page table maps virtual page numbers to physical frame numbers. - When a process accesses an unmapped page, a page fault occurs and the OS loads the page from disk (swap) into a free frame.
Benefits: - Processes can use more memory than physically available (using disk as backing store). - Isolates processes from each other (each has its own page table). - Simplifies memory allocation (no external fragmentation). - Supports shared memory via shared pages (e.g., shared libraries).
TLB (Translation Lookaside Buffer): A hardware cache for recently used page table entries, reducing the overhead of virtual-to-physical address translation.
Key Points
Page table, Page fault, Swap space, TLB, MMU, Demand paging, Internal fragmentation
Common Follow-ups
What is the difference between paging and segmentation? What is thrashing?
Contiguous Memory Allocation: - Each process occupies a single contiguous block of memory. - Fixed Partitioning: Memory divided into fixed-size partitions; internal fragmentation problem. - Dynamic Partitioning: Partitions created dynamically based on process needs; external fragmentation (solved by compaction). - Pros: Simple implementation, fast address translation. - Cons: External fragmentation limits utilization; difficult to grow processes dynamically.
Non-Contiguous Memory Allocation: - Process memory is divided into pages/segments that can be placed anywhere in physical memory. - Paging: Fixed-size pages (no external fragmentation, some internal fragmentation). - Segmentation: Variable-size segments matching logical divisions (code, data, stack); external fragmentation possible. - Pros: Efficient memory utilization, supports virtual memory, processes can grow easily. - Cons: Overhead of page table lookups (mitigated by TLB), more complex memory management hardware.
Key Points
External vs Internal fragmentation, Compaction, Paging vs Segmentation, MMU complexity
Common Follow-ups
What is the optimal page size? How does the OS choose which page to evict during a page fault?