Curated questions covering processes, threads, scheduling, deadlocks, memory management, virtual memory, file systems, and synchronization.
A context switch is the OS saving the state (PCB) of the current process/thread and loading the state of the next one. Overhead includes saving/restoring registers, program counter, and memory maps. Frequent context switches reduce CPU efficiency.
Virtual memory allows processes to use more memory than physically available by storing parts of the address space on disk (swap space). The OS uses demand paging to load pages only when needed. Provides memory isolation between processes.
A page fault occurs when a process accesses a page not currently in physical memory. The OS must load the page from disk (swap space) into a free frame. Types: minor (page in memory but not mapped) and major (page must be read from disk).
Thrashing occurs when a process spends more time swapping pages in and out than executing. Caused by insufficient physical memory. Solution: reduce degree of multiprogramming, use working set model, or add more RAM.
A semaphore is a synchronization primitive with an integer value. Operations: wait (P) decrements value; if negative, blocks. signal (V) increments value; wakes a blocked process. Binary semaphore (0/1) acts like a mutex. Counting semaphore allows N concurrent accesses.
// Semaphore usage (pseudocode)
semaphore mutex = 1;
wait(mutex); // P operation - enter critical section
// critical section
signal(mutex); // V operation - exit critical section
The producer-consumer (bounded buffer) problem: producers add items to a shared buffer, consumers remove them. Requires synchronization to prevent overflow, underflow, and race conditions. Solved with semaphores: empty, full, and mutex.
// Pseudocode
semaphore empty = N, full = 0, mutex = 1;
// Producer
wait(empty); wait(mutex);
add_item();
signal(mutex); signal(full);
// Consumer
wait(full); wait(mutex);
remove_item();
signal(mutex); signal(empty);
A system call is the interface between a user program and the OS kernel. Allows user-mode programs to request kernel services (file I/O, process creation, memory allocation). Examples: read(), write(), fork(), exec(), open(), close().
A file system organizes and stores files on storage devices. Manages file metadata (name, size, permissions, timestamps), directory structure, and free space. Examples: ext4 (Linux), NTFS (Windows), APFS (macOS), FAT32.
An inode (index node) is a data structure in Unix file systems storing file metadata: permissions, owner, size, timestamps, and pointers to data blocks. Does NOT store the filename. Directory entries map filenames to inode numbers.
ln file.txt hardlink.txt # hard link
ln -s file.txt softlink.txt # soft link (symlink)
Spooling (Simultaneous Peripheral Operations On-Line) stores data in a buffer (spool) for a device that cannot accept interleaved data streams. Classic example: print spooler queues print jobs so multiple processes can send print requests without waiting.
Belady's anomaly is the counterintuitive phenomenon where increasing the number of page frames causes more page faults with FIFO page replacement. Does not occur with LRU or Optimal algorithms. LRU and Optimal are stack algorithms, immune to Belady's anomaly.
pid_t pid = fork();
if (pid == 0) {
// child process
execl("/bin/ls", "ls", "-l", NULL);
} else {
// parent process
wait(NULL);
}
The TLB is a hardware cache that stores recent virtual-to-physical address translations. Speeds up memory access by avoiding page tl-table lookups. TLB hit: O(1) translation. TLB miss: must walk page table. TLB is flushed on context switch.
A file descriptor is a non-negative integer that uniquely identifies an open file within a process. Standard descriptors: 0 (stdin), 1 (stdout), 2 (stderr). Created by open(), socket(), pipe(). Closed with close().
int fd[2];
pipe(fd); // fd[0] = read end, fd[1] = write end
write(fd[1], "hello", 5);
read(fd[0], buf, 5);
A page tl-table maps virtual page numbers to physical frame numbers. Each process has its own page table. Multi-level page tables (2-level, 4-level in x86-64) reduce memory overhead. Inverted page tl-table uses one entry per physical frame.
The working set model tracks the set of pages a process actively uses in a time window (working set window). If a process's working set fits in memory, it runs efficiently. If not, thrashing occurs. Used to determine how many frames to allocate to each process.
Memory-mapped files map a file or device into the process's virtual address space. Allows file I/O using memory operations (read/write to pointers). Used for: shared memory between processes, loading executables, and large file processing.
// mmap example (C)
int fd = open("file.txt", O_RDONLY);
char *data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
// access data[i] directly
munmap(data, size);
DMA allows hardware devices to transfer data directly to/from memory without CPU involvement. The CPU initiates the transfer, then the DMA controller handles it. CPU is free to do other work. Used for disk I/O, network cards, and audio.
A race condition occurs when the outcome of a program depends on the relative timing of events (e.g., two threads reading and writing shared data without synchronization). Result is non-deterministic and can cause data corruption.
// Race condition example
int counter = 0;
// Thread 1: counter++ (read, increment, write)
// Thread 2: counter++ (read, increment, write)
// Final value may be 1 instead of 2 if interleaved
Priority inversion occurs when a high-priority task is blocked waiting for a resource held by a low-priority task, while a medium-priority task preempts the low-priority task. Solution: priority inheritance (low-priority task temporarily inherits high-priority task's priority).
The Banker's Algorithm is a deadlock avoidance algorithm. Before granting a resource request, it checks if the resulting state is safe (all processes can eventually complete). If safe, grant; otherwise, make the process wait. Requires knowing maximum resource needs in advance.
Explore 500+ free tutorials across 20+ languages and frameworks.