Bird
Raised Fist0
Interview Prepoperating-systemseasyAmazonGoogleMicrosoftFlipkartRazorpay

System Call - Mechanism & Modes (User vs Kernel)

Choose your preparation mode3 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

User Process Starts Execution

The user process is created and starts executing in user mode. It is ready to run and waiting for CPU allocation.

💡 Initialization of the user process is essential to see how system calls originate from user mode.
Line:process = create_process('UserProcess', mode='user')
💡 User processes begin in user mode and must request kernel services via system calls.
📊
System Call - Mechanism & Modes (User vs Kernel) - Watch the Algorithm Execute, Step by Step
Watching this step-by-step helps you understand the critical mode switches and the mechanism behind system calls, which are fundamental to OS security and functionality.
Step 1/10
·Active fillAnswer cell
1
ready
0
new
Ready Queue
1
Waiting Queue
empty
🖥CPUidlet=0
Transition readyrunning pid:1 - scheduler dispatch
1
running
0
new
Ready Queue
empty
Waiting Queue
empty
🖥CPU1t=1
1
1
running
0
ready
Ready Queue
0
Waiting Queue
empty
🖥CPU1t=2
1
Transition readyrunning pid:0 - mode switch to kernel
1
waiting
0
running
Ready Queue
empty
Waiting Queue
1 (system_call_completion)
🖥CPU0t=3
1
0
1
waiting
0
running
Ready Queue
empty
Waiting Queue
1 (system_call_completion)
🖥CPU0t=4
1
0
1
waiting
0
running
Ready Queue
empty
Waiting Queue
1 (system_call_completion)
🖥CPU0t=5
1
0
Transition runningwaiting pid:0 - system call complete
1
ready
0
waiting
Ready Queue
1
Waiting Queue
empty
🖥CPUidlet=6
1
0
Transition readyrunning pid:1 - scheduler dispatch
1
running
0
waiting
Ready Queue
empty
Waiting Queue
empty
🖥CPU1t=7
1
0
1
1
running
0
waiting
Ready Queue
empty
Waiting Queue
empty
🖥CPU1t=8
1
0
1
1
running
0
waiting
Ready Queue
empty
Waiting Queue
empty
🖥CPU1t=9
1
0
1

Key Takeaways

System calls cause a controlled switch from user mode to kernel mode to safely execute privileged operations.

This mode switch is hard to grasp from code alone because it involves CPU privilege levels and hardware interrupts.

The kernel validates system call parameters to maintain system security and stability.

Seeing this validation step visually clarifies the kernel's protective role during system calls.

After kernel handling, control returns to the user process transparently, allowing seamless continuation of user code.

Understanding the full cycle of request, kernel handling, and return is easier when watching the process states and mode switches.

Practice

(1/5)
1. Which component is primarily responsible for saving and restoring CPU registers during a context switch?
easy
A. The Interrupt Handler
B. The CPU scheduler
C. The Memory Management Unit (MMU)
D. The Process Control Block (PCB)

Solution

  1. Step 1: Understand the role of PCB

    The PCB stores the CPU register states and other process information needed to resume execution later.
  2. Step 2: Differentiate from scheduler and interrupt handler

    The scheduler decides which process runs next but does not save registers; the interrupt handler triggers context switches but does not save registers itself.
  3. Step 3: MMU role

    The MMU handles memory address translation, unrelated to register saving.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    PCB contains saved CPU state -> correct component for saving/restoring registers.
Hint: PCB = process snapshot including CPU registers [OK]
Common Mistakes:
  • Confusing scheduler with register saving
  • Assuming interrupt handler saves registers
  • Thinking MMU handles CPU state
2. Which of the following statements about the LRU page replacement algorithm's complexity and implementation is TRUE?
medium
A. LRU requires hardware support or additional data structures to track usage efficiently
B. LRU can be implemented with O(1) time complexity per page reference using a stack
C. LRU always performs better than FIFO regardless of workload
D. LRU does not require any extra space beyond the page frames

Solution

  1. Step 1: Analyze LRU Implementation Complexity

    LRU needs to track the order of page usage, which is costly without hardware or data structures.
  2. Step 2: Evaluate Each Option

    LRU requires hardware support or additional data structures to track usage efficiently is true; LRU requires hardware support or additional data structures to track usage efficiently.
    LRU can be implemented with O(1) time complexity per page reference using a stack is false; a stack alone cannot provide O(1) updates on every reference.
    LRU always performs better than FIFO regardless of workload is false; LRU can perform worse than FIFO in some workloads (e.g., scan patterns).
    LRU does not require any extra space beyond the page frames is false; LRU requires extra space for tracking usage metadata.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Efficient LRU needs support beyond just frames.
Hint: LRU needs extra tracking structures or hardware support [OK]
Common Mistakes:
  • Assuming LRU is always better than FIFO
  • Believing LRU can be done with no extra space
  • Thinking a simple stack suffices for O(1) LRU
3. Why is it generally inefficient to keep a process in the Ready state for a long time without scheduling it to Running, especially in a multi-core system?
medium
A. Because the process wastes CPU cache locality and increases context switch overhead when scheduled later
B. Because the process holds resources like memory and I/O devices exclusively while Ready
C. Because the process consumes CPU cycles even in Ready state, reducing overall throughput
D. Because the process cannot perform I/O operations while in Ready state, causing system deadlocks

Solution

  1. Step 1: Understand Ready state resource usage

    Processes in Ready state do not consume CPU cycles but occupy scheduling queues.
  2. Step 2: Analyze each option

    A: Correct. Long Ready times cause loss of CPU cache locality and increase context switch overhead when finally scheduled.
    B: Incorrect. Processes do not hold exclusive I/O or memory resources just by being Ready.
    C: Incorrect. Ready processes do not consume CPU cycles.
    D: Incorrect. Ready processes do not cause deadlocks by not performing I/O.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Ready state delays hurt cache locality and increase context switch costs [OK]
Hint: Ready state processes wait without CPU but lose cache locality and increase context switch overhead [OK]
Common Mistakes:
  • Believing Ready processes consume CPU cycles
  • Assuming Ready processes hold exclusive resources
  • Confusing Ready state with Waiting state regarding I/O
4. Which of the following statements about processes and threads is INCORRECT?
medium
A. Threads within the same process share the same memory space
B. Context switching between threads is more expensive than between processes
C. Processes have separate memory spaces and do not share resources by default
D. Threads can communicate more efficiently than processes due to shared memory

Solution

  1. Step 1: Verify thread memory sharing

    Threads share memory within a process, so Threads within the same process share the same memory space is correct.
  2. Step 2: Verify process isolation

    Processes have separate memory spaces, so Processes have separate memory spaces and do not share resources by default is correct.
  3. Step 3: Compare context switching costs

    Thread context switching is lighter and faster than process switching, so Context switching between threads is more expensive than between processes is incorrect.
  4. Step 4: Confirm communication efficiency

    Threads communicate efficiently via shared memory, so Threads can communicate more efficiently than processes due to shared memory is correct.
  5. Final Answer:

    Option B -> Option B
  6. Quick Check:

    Thread context switches are cheaper than process context switches [OK]
Hint: Thread switches cheaper than process switches [OK]
Common Mistakes:
  • Assuming process context switches are cheaper
  • Confusing memory sharing between threads and processes
  • Believing threads cannot communicate efficiently
5. If a process requests resources that would keep the system in a safe state but the system is currently in an unsafe state, what does the Banker's Algorithm do and why?
hard
A. It denies the request because the system must always remain in a safe state, and starting from an unsafe state invalidates the algorithm's assumptions.
B. It restarts the system to reset resource allocations and ensure safety.
C. It preempts resources from other processes to restore a safe state before granting the request.
D. It grants the request because the immediate allocation is safe, ignoring the current unsafe state.

Solution

  1. Step 1: Recall Banker's Algorithm assumptions

    The algorithm assumes the system starts in a safe state to guarantee deadlock avoidance.
  2. Step 2: Analyze the scenario

    If the system is already unsafe, granting requests--even if individually safe--cannot guarantee overall safety.
  3. Step 3: Evaluate options

    A ignores the unsafe starting state.
    B and C describe actions outside the algorithm's scope.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Banker's Algorithm cannot recover from unsafe states; it only avoids entering them.
Hint: Banker's Algorithm requires starting safe state to function correctly [OK]
Common Mistakes:
  • Assuming safe requests can fix unsafe states
  • Believing the algorithm preempts resources
  • Thinking system restarts are part of the algorithm