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.
acquire
Process 1 acquires Mutex lock
Process 1 calls mutex.acquire(), which locks the mutex and sets Process 1 as the owner.
💡 Acquiring the mutex means exclusive access to the critical section is granted to Process 1.
Line:mutex.acquire()
💡 Mutex ownership changes from None to Process 1, preventing others from entering the critical section.
critical_section
Process 1 enters critical section protected by Mutex
Process 1 is inside the critical section after acquiring the mutex lock. No other process can enter this section until the mutex is released.
💡 This step shows the exclusive access granted by the mutex lock.
Line:# critical section
💡 Mutex ensures mutual exclusion by allowing only the owner process to proceed.
release
Process 1 releases Mutex lock
Process 1 calls mutex.release(), which clears ownership and unlocks the mutex, allowing others to acquire it.
💡 Releasing the mutex is necessary to avoid deadlock and allow other processes to enter the critical section.
Line:mutex.release()
💡 Mutex ownership is reset to None, and the lock is available again.
acquire
Process 2 attempts to acquire Semaphore
Process 2 calls semaphore.wait(), which decrements the semaphore count from 3 to 2, allowing Process 2 to enter the critical section.
💡 Semaphore count controls how many processes can concurrently access the resource.
Line:semaphore.wait()
💡 Semaphore count decreases, showing one less available slot for concurrent access.
critical_section
Process 2 enters critical section protected by Semaphore
Process 2 is inside the critical section after successfully decrementing the semaphore count. Up to 2 more processes can enter concurrently.
💡 Semaphore allows multiple processes to enter critical section up to its count limit.
Line:# critical section
💡 Semaphore count enforces a limit on concurrent access rather than exclusive ownership.
release
Process 2 signals Semaphore to release slot
Process 2 calls semaphore.signal(), which increments the semaphore count from 2 back to 3, freeing a slot for other processes.
💡 Signaling the semaphore releases one concurrent access slot.
Line:semaphore.signal()
💡 Semaphore count increases, indicating more available concurrent access.
acquire
Process 1 attempts to acquire Mutex again
Process 1 calls mutex.acquire() again. Since the mutex is unlocked, Process 1 acquires it and becomes the owner.
💡 Mutex acquisition blocks if locked, but here it succeeds immediately.
Line:mutex.acquire()
💡 Mutex ownership transfers to Process 1 again, enforcing exclusive access.
release
Process 1 releases Mutex after second critical section
Process 1 calls mutex.release(), unlocking the mutex and clearing ownership.
💡 Releasing the mutex allows other processes to acquire it later.
Line:mutex.release()
💡 Mutex ownership is cleared, making the lock available again.
final
Final state: Mutex unlocked, Semaphore count full
Both synchronization primitives are free: Mutex is unlocked with no owner, and Semaphore count is back to 3. Processes have completed their critical sections.
💡 The final state confirms that locks are properly released, preventing deadlocks.
Line:# end of usage example
💡 Proper acquire and release cycles ensure safe synchronization.
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
Transitionready → running pid:1 - Process 1 starts executing mutex.acquire()
1
running
2
ready
Ready Queue
2
Waiting Queue
empty
🖥CPU1t=1
Transitionrunning → running pid:1 - Process 1 executes critical section under mutex lock
1
running
2
ready
Ready Queue
2
Waiting Queue
empty
🖥CPU1t=2
Transitionrunning → ready pid:1 - Process 1 executes mutex.release()
1
ready
2
ready
Ready Queue
12
Waiting Queue
empty
🖥CPUidlet=3
1
Transitionready → running pid:2 - Process 2 starts executing semaphore.wait()
1
ready
2
running
Ready Queue
1
Waiting Queue
empty
🖥CPU2t=4
1
Transitionrunning → running pid:2 - Process 2 executes critical section under semaphore control
1
ready
2
running
Ready Queue
1
Waiting Queue
empty
🖥CPU2t=5
1
Transitionrunning → ready pid:2 - Process 2 executes semaphore.signal()
1
ready
2
ready
Ready Queue
12
Waiting Queue
empty
🖥CPUidlet=6
1
2
Transitionready → running pid:1 - Process 1 starts executing mutex.acquire() again
1
running
2
ready
Ready Queue
2
Waiting Queue
empty
🖥CPU1t=7
1
2
Transitionrunning → 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
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.
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.
Step 3: Eliminate incorrect escalations
The inode does not skip levels; it uses single indirect before double indirect pointers.
Step 4: Clarify inode content storage
Inodes never store file content directly; they only store metadata and pointers.
Final Answer:
Option A -> Option A
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
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.
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.
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.
Final Answer:
Option A -> Option A
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
Step 1: Identify overhead of context switch
Saving/restoring CPU registers and PCB updates consume CPU time, causing overhead.
Step 2: Scheduler availability
The scheduler can always select processes; running out is not a cause of performance degradation.
Step 3: MMU and page tables
MMU page table reloads happen on address space switches, but not necessarily on every context switch.
Step 4: CPU cache behavior
CPU cache is not flushed automatically on every context switch; cache invalidation is more complex.
Final Answer:
Option A -> Option A
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
Correct: linked allocation avoids external fragmentation by allowing scattered blocks.
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.
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.
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.
Final Answer:
Option B -> Option B
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
Step 1: Review privilege separation
User mode is restricted; kernel mode has full privileges.
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.
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.
Final Answer:
Option D -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.