Interview Q&A / Operating Systems

INTERVIEW Q&A · 42 QUESTIONS

Operating Systems interview questions

Processes, threads, scheduling, memory and deadlocks, in plain words.

  1. What is a race condition, and what is the critical section problem?

    A race condition happens when two or more threads access shared data at the same time and the result depends on the exact timing of their steps. The critical section is the part of code that touches that shared data, and the critical section problem is designing a way so only one thread runs it at a time, with progress and bounded waiting.

    BeginnerIn-depth3 min read
  2. What is context switching in an operating system?

    Context switching is when the OS pauses the process or thread running on a CPU, saves its state (registers, program counter, stack pointer) in its control block, and loads the saved state of another one so it can continue. It lets many programs share one CPU, but each switch is pure overhead.

    BeginnerIn-depth3 min read
  3. What is the difference between a mutex and a semaphore?

    A mutex is a lock that lets only one thread into a critical section at a time, and only the thread that locked it should unlock it. A semaphore is a counter that allows up to N threads to use a resource at once, and any thread can signal it, which also makes it useful for one thread to notify another.

    BeginnerIn-depth3 min read
  4. What is the difference between a process and a thread?

    A process is a running program with its own memory. A thread is a unit of work inside a process; threads of the same process share that memory, so they are lighter to create but need care to avoid clashing.

    BeginnerIn-depth2 min read
  5. What are the FCFS, SJF, Round Robin and Priority CPU scheduling algorithms, and how do you calculate waiting time?

    FCFS runs processes in arrival order, SJF picks the shortest job next and gives the lowest average waiting time, Round Robin gives each process a fixed time slice in turn for fair response, and Priority runs the most important process first but can starve low-priority ones unless aging is used.

    IntermediateIn-depth4 min read
  6. What is a deadlock and how can you prevent it?

    A deadlock is when two or more processes wait for each other forever, each holding something the other needs. It needs four conditions together; breaking any one of them, for example always taking locks in the same order, prevents it.

    IntermediateIn-depth2 min read
  7. What is virtual memory, and how does paging work?

    Virtual memory gives every process its own private address space that looks large and continuous, while the OS maps it onto real RAM in fixed-size blocks called pages (often 4 KB). A page table translates each virtual page to a physical frame, and pages not in RAM can be kept on disk and loaded on demand through a page fault.

    IntermediateIn-depth4 min read
  8. What is thrashing in an operating system, and how can it be prevented?

    Thrashing is when a system spends most of its time moving pages between RAM and disk instead of running programs, because the running processes together need more memory than is available. It is prevented by giving each process enough frames for its working set, controlling page-fault rate, and reducing the number of running processes.

    AdvancedIn-depth4 min read
  9. A system has 4 KB pages. Translate logical address 20000 if page 4 is stored in frame 9.

    20000 = 4 x 4096 + 3616, so page number 4 and offset 3616. Frame 9 starts at 9 x 4096 = 36864, so the physical address is 36864 + 3616 = 40480.

    BeginnerQuick answer1 min read
  10. For burst times 24, 3 and 3 (all arriving at time 0), what is the average waiting time under non-preemptive SJF?

    SJF runs the shortest burst first: P2, P3, then P1. Waiting times are P2 0, P3 3, P1 6, so the average is (0 + 3 + 6) / 3 = 3 ms, which is the minimum possible.

    BeginnerQuick answer1 min read
  11. Three processes with burst times 24, 3 and 3 arrive together. What is the average waiting time under FCFS?

    Run in arrival order P1, P2, P3. Waiting times are 0, 24 and 27, so the average is (0 + 24 + 27) / 3 = 17 ms. Waiting time is the time a process spends in the ready queue before it gets the CPU.

    BeginnerQuick answer1 min read
  12. What are the states of a process in its life cycle?

    A process moves through new, ready, running, waiting (blocked) and terminated. The scheduler moves it between ready and running; I/O or events move it between running and waiting.

    BeginnerQuick answer1 min read
  13. What do threads of the same process share, and what does each thread keep private?

    Threads in one process share code, global data, the heap and open files. Each thread has its own stack, program counter, registers and thread ID.

    BeginnerQuick answer1 min read
  14. What does fork() return, and how does a program use that value?

    fork() creates a copy of the calling process. It returns 0 in the child, the child's PID in the parent, and -1 if creation failed. Programs branch on that value to run different code in each.

    BeginnerQuick answer1 min read
  15. What is a counting semaphore and when would you use one?

    A counting semaphore holds a non-negative integer that tracks how many units of a resource are free. wait() takes one unit (blocking at zero) and signal() returns one. It limits how many threads use a pool at once.

    BeginnerQuick answer1 min read
  16. What is a Process Control Block (PCB) and what does it store?

    A PCB is the kernel data structure holding everything the OS needs to manage one process: its ID, state, saved registers, program counter, memory information, open files and scheduling data.

    BeginnerQuick answer1 min read
  17. What is the difference between a logical address and a physical address?

    A logical (virtual) address is what the program generates; a physical address is the real location in RAM. The Memory Management Unit translates one into the other at run time.

    BeginnerQuick answer1 min read
  18. What is the difference between a zombie process and an orphan process?

    A zombie has finished running but still has an entry in the process table because its parent has not collected its exit status. An orphan is still running but its parent has died; init adopts it.

    BeginnerQuick answer1 min read
  19. What is the difference between paging and segmentation?

    Paging divides memory into fixed-size pages invisible to the programmer; segmentation divides it into variable-size logical units like code, stack and heap. Paging causes internal fragmentation, segmentation causes external.

    BeginnerQuick answer1 min read
  20. What is the difference between turnaround time, waiting time and response time?

    Turnaround time is completion minus arrival. Waiting time is turnaround minus burst, the total time spent in the ready queue. Response time is the time from arrival to the first time the process gets the CPU.

    BeginnerQuick answer1 min read
  21. A system has 32-bit logical addresses and 4 KB pages. How many bits are the page number and offset, and how large is the page table?

    4 KB = 2^12, so the offset is 12 bits and the page number is 32 - 12 = 20 bits. That gives 2^20 entries; at 4 bytes each, the page table is 4 MB per process.

    IntermediateQuick answer1 min read
  22. Burst times 24, 3 and 3, all arriving at time 0, with a time quantum of 4 ms. What is the average waiting time under Round Robin?

    P1 runs 0-4, P2 4-7, P3 7-10, then P1 alone finishes at 30. Waiting times are P1 6, P2 4, P3 7, so the average is 17 / 3, about 5.67 ms.

    IntermediateQuick answer1 min read
  23. Five processes arrive together with bursts 10, 1, 2, 1, 5 and priorities 3, 1, 4, 5, 2 (lower number is higher priority). What is the average waiting time under non-preemptive priority scheduling?

    The order is P2, P5, P1, P3, P4. Waiting times are 0, 1, 6, 16 and 18, a total of 41, so the average is 41 / 5 = 8.2 ms.

    IntermediateQuick answer1 min read
  24. Four processes arrive at 0, 1, 2 and 3 with bursts 8, 4, 9 and 5. What is the average waiting time under preemptive SJF (SRTF)?

    Run order by remaining time: P1 0-1, P2 1-5, P4 5-10, P1 10-17, P3 17-26. Waiting times are P1 9, P2 0, P3 15, P4 2, so the average is 26 / 4 = 6.5 ms.

    IntermediateQuick answer1 min read
  25. Given a segment table, how do you translate the logical address (1, 500)?

    Look up segment 1, compare the offset with its limit, and add the offset to its base. If the offset is not below the limit the hardware traps. With limit 400 here, (1, 500) is invalid; (0, 500) maps to 1900.

    IntermediateQuick answer1 min read
  26. How can you detect a deadlock using a resource allocation graph?

    Draw processes and resources as nodes, with request and assignment edges. A cycle means deadlock if every resource has a single instance; with multiple instances a cycle is necessary but not enough.

    IntermediateQuick answer1 min read
  27. How do you solve the producer-consumer problem using semaphores?

    Use three semaphores: empty (counts free slots, starts at N), full (counts filled slots, starts at 0) and mutex (starts at 1). Producers wait on empty, consumers wait on full, and mutex guards the buffer.

    IntermediateQuick answer1 min read
  28. How does the time quantum affect Round Robin performance?

    A very large quantum makes Round Robin behave like FCFS. A very small one wastes time on context switches. A good quantum is longer than most CPU bursts, often cited as 80% of bursts finishing within one quantum.

    IntermediateQuick answer1 min read
  29. If a program calls fork() three times in a row, how many processes run in total?

    Each unconditional fork() doubles the number of processes, so n sequential calls give 2^n processes in total: 8 for three calls, meaning 7 new children plus the original.

    IntermediateQuick answer1 min read
  30. If a TLB lookup takes 10 ns, memory access 100 ns and the hit ratio is 90%, what is the effective access time?

    EAT = 0.9 x (10 + 100) + 0.1 x (10 + 100 + 100) = 99 + 21 = 120 ns. A TLB hit needs one memory access; a miss needs two, one for the page table and one for the data.

    IntermediateQuick answer1 min read