Bird
Raised Fist0
Interview Prepoperating-systemsmediumAmazonGoogleMicrosoftRazorpayPhonePe

Page Replacement - FIFO, LRU, Optimal Algorithm

Choose your preparation mode3 modes available
▶
Steps
setup

Initialize FIFO Page Replacement

Start with an empty queue and empty set to track pages in memory. Page fault count is zero.

💡 Initialization sets up the data structures needed to track pages and detect faults efficiently.
Line:queue = [] # Queue to store pages in FIFO order page_set = set() # Set for O(1) lookup page_faults = 0
💡 The queue will maintain insertion order, crucial for FIFO eviction.
📊
Page Replacement - FIFO, LRU, Optimal Algorithm - Watch the Algorithm Execute, Step by Step
Watching the algorithm step-by-step reveals the dynamic decisions behind page replacement, which are hard to grasp from code alone.
Step 1/10
·Active fill★Answer cell
Transition new → ready pid:fifo - Initialization complete
fifo
ready
Ready Queue
fifo
Waiting Queue
empty
🖥CPUidlet=0
Transition ready → running pid:fifo - Processing page 7
fifo
running
Ready Queue
empty
Waiting Queue
empty
🖥CPUfifot=1
fifo
Transition running → running pid:fifo - Processing page 0
fifo
running
Ready Queue
empty
Waiting Queue
empty
🖥CPUfifot=2
fifo
Transition running → running pid:fifo - Processing page 1
fifo
running
Ready Queue
empty
Waiting Queue
empty
🖥CPUfifot=3
fifo
Transition running → running pid:fifo - Evicted page 7 to add page 2
fifo
running
Ready Queue
empty
Waiting Queue
empty
🖥CPUfifot=4
fifo
Transition running → running pid:fifo - Page 0 hit in memory
fifo
running
Ready Queue
empty
Waiting Queue
empty
🖥CPUfifot=5
fifo
Transition running → running pid:fifo - Evicted page 0 to add page 3
fifo
running
Ready Queue
empty
Waiting Queue
empty
🖥CPUfifot=6
fifo
Transition running → running pid:fifo - Evicted page 1 to add page 0
fifo
running
Ready Queue
empty
Waiting Queue
empty
🖥CPUfifot=7
fifo
Transition running → running pid:fifo - Evicted page 2 to add page 4
fifo
running
Ready Queue
empty
Waiting Queue
empty
🖥CPUfifot=8
fifo
Transition running → terminated pid:fifo - Algorithm completed
fifo
terminated
burst: 0
Ready Queue
empty
Waiting Queue
empty
🖥CPUidlet=8
fifo

Key Takeaways

✓ FIFO evicts the oldest page regardless of future use.

This insight is hard to see from code alone because the eviction is implicit in the queue structure.

✓ Page faults occur only when a page is not in memory.

Visualizing each page check clarifies when faults happen and when pages are hits.

✓ The queue order directly controls eviction order in FIFO.

Seeing the queue update step-by-step reveals how insertion order drives eviction.

Practice

(1/5)
1. Trace the sequence of events when a circular wait condition arises among three processes P1, P2, and P3, each holding one resource and waiting for another. What happens next?
easy
A. Each process waits indefinitely because each is holding a resource the next process needs, forming a cycle.
B. One process releases its resource voluntarily, breaking the cycle immediately.
C. The operating system preempts resources from one process to resolve the wait.
D. Processes detect the cycle and terminate themselves automatically.

Solution

  1. Step 1: Identify circular wait

    Each process holds a resource and waits for another held by the next process, forming a cycle.
  2. Step 2: Understand consequences

    Because each process waits for a resource held by another in the cycle, none can proceed, causing indefinite waiting.
  3. Step 3: Analyze other options

    One process releases its resource voluntarily, breaking the cycle immediately assumes voluntary release, which is not guaranteed. The operating system preempts resources from one process to resolve the wait involves preemption, which is not part of the circular wait condition. Processes detect the cycle and terminate themselves automatically assumes automatic termination, which is not standard behavior.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Circular wait leads to indefinite blocking without external intervention.
Hint: Circular wait means processes wait in a cycle, causing indefinite blocking [OK]
Common Mistakes:
  • Assuming processes will release resources voluntarily
  • Confusing circular wait with preemption or automatic recovery
2. In which scenario is Round Robin scheduling with a fixed time quantum most appropriate compared to First-Come-First-Serve or Priority scheduling?
easy
A. When processes have highly variable CPU burst times and fairness among all processes is required
B. When all processes have identical priorities and long CPU bursts
C. When minimizing average turnaround time is the only goal
D. When processes have strict deadlines and real-time constraints

Solution

  1. Step 1: Understand Round Robin's fairness goal

    Round Robin scheduling is designed to allocate CPU time slices fairly among all processes, especially when burst times vary widely.
  2. Step 2: Analyze other options

    When all processes have identical priorities and long CPU bursts is less suitable because identical priorities and long bursts favor FCFS or SJF for efficiency, not fairness. When minimizing average turnaround time is the only goal ignores fairness and focuses only on turnaround time, which RR does not optimize. When processes have strict deadlines and real-time constraints requires real-time guarantees, which RR cannot provide due to its time slicing.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    RR is best when fairness and responsiveness matter, especially with variable burst times.
Hint: RR = fairness with variable bursts
Common Mistakes:
  • Assuming RR always minimizes turnaround time
  • Believing RR suits real-time deadlines
  • Confusing RR with priority-based scheduling
3. Trace the sequence of events when a process's CPU burst exceeds the time quantum in Round Robin scheduling. What happens immediately after the quantum expires?
easy
A. The process is preempted and placed at the end of the ready queue
B. The process continues running until it voluntarily yields the CPU
C. The process is terminated and removed from the system
D. The process is moved to the waiting queue for I/O

Solution

  1. Step 1: Recall Round Robin preemption

    When a process's time quantum expires, it is preempted to ensure fairness and allow other processes CPU access.
  2. Step 2: Understand queue management

    The preempted process is placed at the end of the ready queue to wait for its next turn.
  3. Step 3: Analyze incorrect options

    The process continues running until it voluntarily yields the CPU contradicts RR's preemption principle. The process is terminated and removed from the system is incorrect because process termination depends on completion, not quantum expiry. The process is moved to the waiting queue for I/O is incorrect unless the process requests I/O, which is unrelated to quantum expiration.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Quantum expiry -> preempt -> enqueue at ready queue's end.
Hint: Quantum expiry means preempt and requeue
Common Mistakes:
  • Thinking process runs until completion ignoring quantum
  • Confusing preemption with process termination
  • Assuming process moves to waiting queue without I/O
4. What is a key limitation of the Banker's Algorithm that affects its practical use in modern operating systems?
medium
A. It requires processes to declare their maximum resource needs in advance, which is often impractical.
B. It can only handle a single resource type, limiting its applicability.
C. It always leads to deadlocks if the system is heavily loaded.
D. It preempts resources from processes, causing starvation.

Solution

  1. Step 1: Identify Banker's Algorithm assumptions

    The algorithm requires prior knowledge of maximum resource needs for each process.
  2. Step 2: Analyze limitations

    This requirement is often unrealistic in dynamic or unpredictable environments.
  3. Step 3: Evaluate other options

    B is false; Banker's Algorithm handles multiple resource types.
    C is false; it avoids deadlocks rather than causing them.
    D is false; it does not preempt resources.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Pre-declaration of max needs is the main practical limitation.
Hint: Banker's Algorithm needs max resource claims upfront [OK]
Common Mistakes:
  • Thinking it only supports one resource type
  • Believing it causes deadlocks under load
  • Confusing it with preemptive algorithms
5. Which of the following statements best describes a limitation of FCFS scheduling related to waiting time and system responsiveness?
medium
A. FCFS can cause long waiting times for short processes due to the convoy effect
B. FCFS guarantees the shortest average waiting time among all scheduling algorithms
C. FCFS allows preemption to improve responsiveness for interactive processes
D. FCFS scheduling complexity grows exponentially with the number of processes

Solution

  1. Step 1: Evaluate each statement

    A is correct because FCFS can cause long waiting times for short processes due to the convoy effect.
    B is incorrect because FCFS does not guarantee the shortest average waiting time; algorithms like SJF do.
    C is incorrect because FCFS is non-preemptive and does not allow preemption.
    D is incorrect because FCFS scheduling complexity is O(n), simple queue processing.
  2. Step 2: Identify the limitation

    The convoy effect causing long waiting times for short processes is a key limitation.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    FCFS is simple but can cause poor responsiveness due to the convoy effect.
Hint: FCFS = simple but convoy effect hurts short jobs
Common Mistakes:
  • Believing FCFS minimizes average waiting time
  • Confusing FCFS with preemptive algorithms
  • Overestimating FCFS scheduling complexity
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard