Bird
Raised Fist0
Interview Prepoperating-systemsmediumAmazonGoogleMicrosoftRazorpay

Semaphore vs Mutex - When to Use Which

Choose your preparation mode3 modes available
▶
Steps
setup

Initialize Mutex and Semaphore

The Mutex and Semaphore objects are created. Mutex starts unlocked with no owner. Semaphore is initialized with a count of 3, allowing up to 3 concurrent accesses.

💡 Initialization sets the starting state for synchronization primitives before any process uses them.
Line:mutex = Mutex() semaphore = Semaphore(3)
💡 Mutex ownership is empty and Semaphore count is at maximum capacity initially.
📊
Semaphore vs Mutex - When to Use Which - Watch the Algorithm Execute, Step by Step
Watching each step of lock acquisition and release helps you understand the fundamental difference between Mutex (exclusive ownership) and Semaphore (counted access).
Step 1/10
·Active fill★Answer cell
1
ready
2
ready
Ready Queue
12
Waiting Queue
empty
🖥CPUidlet=0
Transition ready → running pid:1 - Process 1 starts executing mutex.acquire()
1
running
2
ready
Ready Queue
2
Waiting Queue
empty
🖥CPU1t=1
Transition running → running pid:1 - Process 1 executes critical section under mutex lock
1
running
2
ready
Ready Queue
2
Waiting Queue
empty
🖥CPU1t=2
Transition running → ready pid:1 - Process 1 executes mutex.release()
1
ready
2
ready
Ready Queue
12
Waiting Queue
empty
🖥CPUidlet=3
1
Transition ready → running pid:2 - Process 2 starts executing semaphore.wait()
1
ready
2
running
Ready Queue
1
Waiting Queue
empty
🖥CPU2t=4
1
Transition running → running pid:2 - Process 2 executes critical section under semaphore control
1
ready
2
running
Ready Queue
1
Waiting Queue
empty
🖥CPU2t=5
1
Transition running → ready pid:2 - Process 2 executes semaphore.signal()
1
ready
2
ready
Ready Queue
12
Waiting Queue
empty
🖥CPUidlet=6
1
2
Transition ready → running pid:1 - Process 1 starts executing mutex.acquire() again
1
running
2
ready
Ready Queue
2
Waiting Queue
empty
🖥CPU1t=7
1
2
Transition running → ready pid:1 - Process 1 executes mutex.release() again
1
ready
2
ready
Ready Queue
12
Waiting Queue
empty
🖥CPUidlet=8
1
2
1
1
terminated
burst: 0
2
terminated
burst: 0
Ready Queue
empty
Waiting Queue
empty
🖥CPUidlet=9
1
2
1

Key Takeaways

✓ Mutex enforces exclusive ownership, allowing only one process to enter the critical section at a time.

This exclusivity is hard to grasp from code alone but becomes clear when you see ownership change and lock state visually.

✓ Semaphore controls concurrent access by maintaining a count, allowing multiple processes up to the count limit to enter simultaneously.

Visualizing the semaphore count decrement and increment clarifies how concurrency limits are enforced.

✓ Proper acquire and release cycles are essential to avoid deadlocks and ensure synchronization primitives are available for other processes.

Seeing the full lifecycle of locking and unlocking helps understand the importance of releasing locks.

Practice

(1/5)
1. When a file grows beyond the capacity of direct block pointers in its inode, what sequence of steps occurs to locate the additional data blocks?
easy
A. The file system uses single indirect pointers to reference blocks that contain further direct block pointers.
B. The inode immediately switches to double indirect pointers, skipping single indirect pointers.
C. The file system allocates new direct pointers dynamically within the inode structure.
D. The inode stores the entire file content directly once direct pointers are exhausted.

Solution

  1. Step 1: Recall inode pointer hierarchy

    Inodes have a fixed number of direct pointers, followed by single, double, and triple indirect pointers for larger files.
  2. Step 2: Understand pointer escalation

    When direct pointers are full, the file system uses single indirect pointers, which point to blocks containing more direct pointers.
  3. Step 3: Eliminate incorrect escalations

    The inode does not skip levels; it uses single indirect before double indirect pointers.
  4. Step 4: Clarify inode content storage

    Inodes never store file content directly; they only store metadata and pointers.
  5. Final Answer:

    Option A -> Option A
  6. Quick Check:

    Single indirect pointers extend data block addressing after direct pointers [OK]
Hint: Direct -> Single Indirect -> Double Indirect pointer escalation
Common Mistakes:
  • Skipping pointer levels (double indirect before single indirect)
  • Assuming inode can dynamically add direct pointers
  • Thinking inode stores file content directly
2. In which scenario is preemptive Shortest Job First (SJF) scheduling preferred over non-preemptive SJF?
easy
A. When new processes with shorter burst times can arrive at any time during execution
B. When fairness is not a concern and starvation is acceptable
C. When minimizing context switch overhead is the highest priority
D. When all processes arrive at the same time and have similar burst times

Solution

  1. Step 1: Understand preemptive SJF behavior

    Preemptive SJF allows the CPU to switch to a newly arrived process if it has a shorter burst time than the currently running process.
  2. Step 2: Analyze scenarios

    If new processes with shorter burst times can arrive anytime, preemptive SJF ensures the CPU always runs the shortest job next, improving average waiting time.
  3. Step 3: Why other options are incorrect

    A: If all processes arrive simultaneously with similar burst times, preemptive advantage is minimal.
    B: Starvation is a concern in preemptive SJF, so it's not chosen when fairness is ignored.
    C: Preemptive SJF increases context switches, so it's not preferred when minimizing overhead.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Preemptive SJF is best when shorter jobs can arrive anytime, requiring immediate preemption.
Hint: Preemptive SJF shines when shorter jobs arrive unpredictably [OK]
Common Mistakes:
  • Assuming preemptive SJF always reduces overhead
  • Believing preemptive SJF is best when all jobs arrive simultaneously
3. Why does increasing the frequency of context switches beyond a certain point degrade overall system performance?
medium
A. Because saving and restoring CPU registers is a costly operation that consumes CPU cycles
B. Because the scheduler runs out of processes to schedule
C. Because the MMU has to reload page tables more frequently
D. Because the CPU cache is flushed automatically on every context switch

Solution

  1. Step 1: Identify overhead of context switch

    Saving/restoring CPU registers and PCB updates consume CPU time, causing overhead.
  2. Step 2: Scheduler availability

    The scheduler can always select processes; running out is not a cause of performance degradation.
  3. Step 3: MMU and page tables

    MMU page table reloads happen on address space switches, but not necessarily on every context switch.
  4. Step 4: CPU cache behavior

    CPU cache is not flushed automatically on every context switch; cache invalidation is more complex.
  5. Final Answer:

    Option A -> Option A
  6. Quick Check:

    Context switch overhead is mainly CPU cycles spent saving/restoring registers.
Hint: More switches -> more register save/restore overhead -> slower system [OK]
Common Mistakes:
  • Believing scheduler runs out of processes
  • Assuming MMU reloads page tables every switch
  • Thinking CPU cache flushes on every context switch
4. Which of the following statements about linked file allocation is INCORRECT?
medium
A. Linked allocation eliminates external fragmentation by allowing non-contiguous storage
B. Linked allocation supports efficient direct access to any block in the file
C. Each file block contains a pointer to the next block in the chain
D. Linked allocation requires only the starting block address to access the entire file

Solution

  1. Step 1: Recall linked allocation properties

    Linked allocation stores file blocks non-contiguously with pointers linking blocks sequentially.
  2. Step 2: Analyze linked allocation eliminates external fragmentation by allowing non-contiguous storage

    Correct: linked allocation avoids external fragmentation by allowing scattered blocks.
  3. Step 3: Analyze linked allocation supports efficient direct access to any block in the file

    Incorrect: linked allocation does not support efficient direct access; it requires sequential traversal.
  4. Step 4: Analyze each file block contains a pointer to the next block in the chain

    Correct: each block contains a pointer to the next.
  5. Step 5: Analyze linked allocation requires only the starting block address to access the entire file

    Correct: only the starting block address is needed to traverse the file.
  6. Final Answer:

    Option B -> Option B
  7. Quick Check:

    Linked allocation -> no efficient direct access, only sequential traversal.
Hint: Linked allocation -> sequential access only, no direct access [OK]
Common Mistakes:
  • Assuming linked allocation supports direct access
  • Confusing external fragmentation with internal fragmentation
5. Which of the following statements about user mode and kernel mode is INCORRECT?
medium
A. Mode switches are triggered by traps or interrupts to ensure controlled privilege escalation.
B. Kernel mode has unrestricted access to all hardware and memory.
C. User mode code cannot directly access kernel memory for protection reasons.
D. System calls execute entirely in user mode without switching to kernel mode.

Solution

  1. Step 1: Review privilege separation

    User mode is restricted; kernel mode has full privileges.
  2. Step 2: Validate each statement

    User mode code cannot directly access kernel memory for protection reasons. is true; user mode cannot access kernel memory. Kernel mode has unrestricted access to all hardware and memory. is true; kernel mode has full access. Mode switches are triggered by traps or interrupts to ensure controlled privilege escalation. is true; traps/interrupts cause mode switches.
  3. Step 3: Identify incorrect statement

    System calls execute entirely in user mode without switching to kernel mode. is false; system calls require switching to kernel mode to execute privileged operations.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    System calls always involve mode switch to kernel [OK]
Hint: System calls require kernel mode execution
Common Mistakes:
  • Believing system calls run entirely in user mode
  • Confusing hardware interrupts with system calls
  • Assuming user mode can access kernel memory
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