Operating systems interview questions
Processes, threads, scheduling, deadlock, memory management and synchronisation.
1. What is an operating system? What is the kernel?
Software that manages hardware and provides services to programs: process management, memory, files, devices and security. The kernel is its core part that runs with full privileges and talks to the hardware directly.
2. What is the difference between a process and a thread?
A process is a running program with its own memory space. A thread is a lighter unit of execution inside a process; threads of the same process share its memory, so switching between them is cheaper but shared data needs synchronisation.
3. What is a deadlock? What are its four conditions?
Two or more processes wait forever for resources held by each other. It needs all four conditions at once: mutual exclusion, hold and wait, no preemption and circular wait. Breaking any one prevents it.
Asked in: Infosys, Deloitte
4. What is the Banker's algorithm?
A deadlock-avoidance algorithm. Before granting a request, the OS checks whether the system would still be in a safe state, where some order exists in which every process can finish with the available resources. If not, the request waits.
5. What are the common CPU scheduling algorithms?
FCFS (first come, first served), SJF (shortest job first, optimal average waiting time), priority scheduling, Round Robin (each process gets a fixed time slice, good for time-sharing) and multilevel queue. Preemptive versions can interrupt a running process.
6. What is paging? What is virtual memory?
Paging splits memory into fixed-size frames and programs into pages of the same size, so a program need not be contiguous in RAM. Virtual memory uses disk space as an extension of RAM, loading pages only when needed (demand paging) so programs larger than RAM can run.
7. What are a page fault and thrashing?
A page fault happens when a program uses a page that is not in RAM, so the OS loads it from disk. Thrashing is when the system spends most of its time swapping pages in and out instead of doing work, usually because too many processes are running.
8. What is the difference between a semaphore and a mutex?
A mutex is a lock that one thread holds at a time and only the owner releases. A semaphore is a counter that allows up to N threads into a section (a binary semaphore allows one) and can be signalled by any thread. Both protect critical sections.
9. What is a context switch?
Saving the state (registers, program counter) of the running process or thread and loading another's so the CPU can switch between them. It is pure overhead, so too many switches slow the system.
10. What is fragmentation?
Wasted memory. Internal fragmentation is unused space inside an allocated block (common with fixed-size blocks). External fragmentation is free memory split into small gaps that cannot satisfy a large request; paging and compaction reduce it.
11. What are multiprogramming, multitasking and multiprocessing?
Multiprogramming keeps several programs in memory so the CPU always has work. Multitasking rapidly switches between tasks so they appear to run together. Multiprocessing uses two or more CPUs (or cores) running truly in parallel.