Bird
Raised Fist0

In the following pseudo-code snippet for a system call handler, which line contains a subtle bug related to mode switching?

medium🐞 Bug Identification Q7 of Q15
Operating Systems - System Call - Mechanism & Modes (User vs Kernel)
In the following pseudo-code snippet for a system call handler, which line contains a subtle bug related to mode switching? 1: save_user_state(); 2: switch_to_kernel_mode(); 3: perform_system_call(); 4: switch_to_user_mode(); 5: restore_user_state();
ALine 1: save_user_state() should be after switch_to_kernel_mode()
BLine 2: switch_to_kernel_mode() is unnecessary because traps handle mode switch
CLine 4: switch_to_user_mode() should happen before restore_user_state()
DLine 5: restore_user_state() should be called before perform_system_call()
Step-by-Step Solution
Solution:
  1. Step 1: Understand mode switch mechanism

    Mode switch to kernel mode is triggered automatically by the trap instruction, not manually.
  2. Step 2: Identify redundant line

    Line 2 manually switching mode is unnecessary and incorrect.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Traps handle mode switch; manual switch causes errors. [OK]
Quick Trick: Traps auto-switch mode; manual switch is a bug [OK]
Common Mistakes:
MISTAKES
  • Thinking mode switch must be called explicitly
  • Confusing order of saving/restoring state
  • Assuming user state restored before system call
Trap Explanation:
PITFALL
  • Candidates often think mode switches require explicit calls rather than hardware traps.
Interviewer Note:
CONTEXT
  • Tests subtle understanding of system call handler internals.
Master "System Call - Mechanism & Modes (User vs Kernel)" in Operating Systems

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Operating Systems Quizzes