• retagged by
29,232 views
91 91 votes

​​​​Consider a process $\text{P}$ running on a $\text{CPU}$. Which one or more of the following events will always trigger a context switch by the $\text{OS}$ that results in process $\mathrm{P}$ moving to a non-running state (e.g., ready, blocked)?

  1. $\text{P}$ makes a blocking system call to read a block of data from the disk
  2. $\text{P}$ tries to access a page that is in the swap space, triggering a page fault
  3. An interrupt is raised by the disk to deliver data requested by some other process
  4. A timer interrupt is raised by the hardware

5 Answers

117 117 votes

A: True
If a process itself makes a block() call, then it will be blocked until the I/O operation completes.

B: True
If a page is not in the physical memory, then the OS invokes the Page Fault Handler, and the process remains blocked until the page is read (i.e., the disk I/O is complete).

By the way, there are two types of page faults:

  • Minor Page Fault: Occurs when a page is already in physical memory but was brought in earlier by another process, and the current process is not aware of it. This typically happens when pages are shared.
  • Major Page Fault: Occurs when the OS needs to access the disk to retrieve the page into physical memory.

Since this question refers to retrieving a page from the disk, it is considered a Major Page Fault. Therefore, the current process will be blocked until this I/O operation is completed.

References:  Wikipedia: Page Fault, One more: Stack Overflow Discussion


C: False
Such a request will be served by DMA (Direct Memory Access), and the CPU does not need to get involved. Hence, the current program will continue using the CPU.

D: False
Timer interrupts may be ignored in many cases and do not necessarily lead to a context switch.

  • Case 1: Consider non-preemptive scheduling like FCFS. In that case, we either disable the timer interrupt or ignore it.
    Reference: (Q8) IITB OS Notes
  • Case 2: Consider a scheduling algorithm such as RR with Priority. Suppose we want to run process \( P_1 \) (high-priority) for a quantum of 50 ms and \( P_2 \) (low-priority) for a quantum of 10 ms.
    Then, the timer interrupt interval should be:

\[ \text{Timer Interval} = \gcd(10, 50) = 10 \text{ ms} \]

Now, whenever \( P_1 \) is running and a timer interrupt occurs, it will be ignored four times and a context switch will occur on the fifth occurrence. Therefore, it is clear that it is not necessary to perform a context switch on every timer interrupt.

• edited by
6 6 votes
Option a is correct because P itself makes a blocking call and goes into blocked state

option b :When a page fault occurs the process is blocked(basically it waits for the missing page to be fetched) and then goes into the ready state

option c:Interrupt is raised to deliver data fetched by other process.here the phrase other process is important because in this case when we are talking about p context switch doesnt happen

option d:Here one situation can be lets consider a round robin scheduer in which the time interrupt occurs but the time quanta does not expire in that case  context switch doesnt happen so d is false
6 6 votes

Correct Answer: A only

  • A. Blocking system call: This always causes the process to enter the blocked state while waiting for I/O, so the OS must perform a context switch.

  • B. Page fault: Since the required page is in swap space (disk), it must be fetched into RAM. P has to wait for the disk I/O, so it enters the blocked state, triggering a context switch.

  • C. Disk interrupt for another process: Does not require the running process P to be switched out.

  • D. Timer interrupt: Causes a switch only in preemptive systems, so it is not guaranteed in all cases.

Therefore, options A & B  triggers a context switch and moves process P to a non-running state.

• edited by
1 flag:
✌ Edit necessary (supratikkarmakr “B is also a solution”)
0 0 votes

The correct options are A and B.

Detailed Explanation

  • A. P makes a blocking system call to read a block of data from the disk (Correct)
    • Why: A "blocking" call means the process cannot proceed with its execution until the slow disk input/output (I/O) operation completes. The operating system (OS) moves process P into the blocked/waiting state and performs a context switch to run a different process.
  • B. P tries to access a page that is in the swap space, triggering a page fault (Correct)
    • Why: Since the requested page is in the swap space (on the disk), the OS must initiate a disk read operation to bring the page back into physical memory (RAM). Because disk access is extremely slow compared to the CPU, process P is placed into the blocked state while waiting for the I/O to finish, triggering a context switch.
  • C. An interrupt is raised by the disk to deliver data requested by some other process (Incorrect)
    • Why: When this interrupt occurs, the CPU temporarily pauses P to execute the Interrupt Service Routine (ISR). Once the ISR finishes processing the data for the other process, the scheduler can immediately resume process P without moving it to a non-running state like "ready" or "blocked."
  • D. A timer interrupt is raised by the hardware (Incorrect)
    • Why: A timer interrupt happens periodically to update the system clock or track time slices. It does not always result in a context switch. For example, if process P's assigned time quantum (time slice) has not expired yet, or if P is the highest-priority process available, the OS will simply return control back to P.

NOTE: If option A is changed to "P wants to read a block of data from the disk", it becomes ambiguous and would not always trigger a context switch.

Here is why:

  • Buffer Cache Hit (No Disk I/O): The operating system often keeps frequently accessed disk data in a memory buffer (or page cache). If the data process (P) wants is already sitting in RAM, the OS can fulfill the read request instantly. No slow disk operations happen, and process (P) continues running without a context switch.
  • Non-blocking/Asynchronous Read: If the process initiates an asynchronous read, it tells the OS to start fetching the data but continues executing its own code in the meantime. It does not wait or block, meaning it stays in the running state.
Answer:
Position:
Show:

Related questions

55 55 votes
2 2 answers
17.7k
17.7k views
Arjun asked Feb 16, 2024
17,696 views
​Which of the following tasks is/are the responsibility/responsibilities of the memory management unit $\text{(MMU)}$ in a system with paging-based memory management?Allo...
27 27 votes
5 5 answers
17.0k
17.0k views
Arjun asked Feb 16, 2024
17,022 views
​​​​​Consider a multi-threaded program with two threads $\mathrm{T} 1$ and $\mathrm{T} 2$. The threads share two semaphores: $s1$ (initialized to $1$) and $s2$ (initializ...
51 51 votes
8 8 answers
24.7k
24.7k views
Arjun asked Feb 16, 2024
24,676 views
​Node $\text{X}$ has a $\text{TCP}$ connection open to node $\text{Y}$. The packets from $\text{X}$ to $\text{Y}$ go through an intermediate $\text{IP}$ router $\mathrm{R...
50 50 votes
6 6 answers
22.5k
22.5k views
Arjun asked Feb 16, 2024
22,532 views
​​​​​Which of the following file organizations is/are $\text{I/O}$ efficient for the scan operation in $\text{DBMS}$?SortedHeapUnclustered tree indexUnclustered hash inde...