📝 Nonblocking and asynchronous I/O (12 MCQs)
Separating Application Execution from I/O Waiting
Nonblocking and asynchronous I/O are techniques that reduce the time an application spends waiting for input or output operations. A nonblocking operation returns promptly when it cannot proceed immediately, while asynchronous I/O allows an operation to be submitted and completed later with notification or result retrieval. These approaches improve concurrency but have distinct completion semantics.
Why Blocking Calls Can Limit Concurrent Work
A conventional blocking read may suspend a thread until data becomes available. This is simple for sequential programs but can limit responsiveness when one thread handles many connections or devices. Nonblocking and asynchronous approaches let applications perform other work while waiting, although they require more careful management of request state and completion.
Immediate Return Versus Deferred Completion
With nonblocking I/O, a call returns immediately if the requested operation would otherwise wait; the application can retry or use readiness notification. With asynchronous I/O, the application submits an operation and continues executing. The kernel or subsystem completes it later and reports the result through an event, completion queue, or callback mechanism.
Example of a Nonblocking Socket Read
A server monitors 100 client sockets using a readiness interface. It attempts to read from a socket and receives an indication that no data is currently available rather than sleeping. The server handles another ready connection and returns to the first socket when it becomes readable, preserving responsiveness across clients.
Concurrency Benefits and State Management Costs
These techniques support responsive network servers, graphical applications, and high-concurrency storage workloads. They can reduce blocked-thread overhead and improve resource use. However, applications must manage partial transfers, pending requests, completion errors, buffers, and cancellation carefully. Nonblocking mode alone does not guarantee that every operation is fully asynchronous.
Misconceptions About Nonblocking and Asynchronous I/O
'Nonblocking and asynchronous mean exactly the same thing' is incorrect because prompt return and deferred completion are different behaviours. 'A nonblocking read always returns all available data' is false because it may return fewer bytes or indicate that none are ready. 'Asynchronous I/O removes completion handling' is wrong because applications must still collect results.
Exam Focus: Return Behaviour and Completion Events
Compare blocking, nonblocking, and asynchronous operations by when they return and how completion is reported. Trace a socket read when no data is available and an asynchronous request that finishes later. Questions may test readiness notification, completion queues, partial transfers, and why application state management becomes more complex.
Key Terms of Nonblocking and Asynchronous I/O
- Blocking I/O - Waits until an operation can proceed.
- Nonblocking I/O - Returns without waiting for readiness.
- Asynchronous I/O - Completes submitted operations later.
- Readiness - Indication that an operation can proceed.
- Completion queue - Holds finished operation results.
📝 All Nonblocking and asynchronous I/O MCQs
Q1. What is the CPU instruction commonly used to resume execution after handling an interrupt?
📖 Explanation: The instruction that restores processor state after an interrupt is called IRET. It pops the saved flags, segment selector, and instruction pointer, returning execution to the point where the interrupt occurred. Options A (RET) and B (JMP) are generic return or jump instructions that do not handle saved interrupt context. Option D (CALL) initiates a subroutine, not a return. IRET is defined by the CPU architecture for interrupt return, making it the correct choice.
Q2. Why does an interrupt controller dispatch the interrupt to the correct handler without the CPU polling all devices?
📖 Explanation: The controller contains a priority encoder that examines the interrupt request lines and determines which device raised the signal, then directly forwards the interrupt vector to the CPU. This eliminates the need for the CPU to sequentially check each device, saving cycles. Options B and C describe mechanisms unrelated to interrupt selection, and D would cause unnecessary processing for every device. The priority encoder provides efficient, deterministic dispatch.
Q3. How does multilevel interrupt priority improve system responsiveness in a multitasking OS?
📖 Explanation: When the OS assigns priority levels, a high‑priority interrupt can interrupt a lower‑priority handler, allowing urgent tasks to be serviced promptly. This pre‑emption reduces latency for critical events. Option B describes a simple queue that would delay urgent interrupts. Option C is incorrect because priority handling does not eliminate context switches; it may even increase them. Option D suggests combining interrupts, which is not how priority schemes work.
Q4. In nonblocking I/O, what is the primary advantage of using asynchronous notification over polling?
📖 Explanation: Asynchronous notification lets the OS inform a process when data is ready, so the CPU does not waste cycles repeatedly checking the device state. This contrasts with polling, which consumes CPU time while waiting. Option B is false because buffers are still required to hold data. Option C is unrelated; ordering is handled by other mechanisms. Option D may be partially true but is not the principal benefit. Reducing idle cycles directly improves efficiency.
Q5. Which feature allows a CPU to temporarily defer handling an interrupt during a critical section?
📖 Explanation: Interrupt masking lets the processor ignore certain interrupt lines while executing code that must not be interrupted, such as updating shared data structures. This deferral protects critical sections from unexpected pre‑emption. DMA (Direct Memory Access) moves data without CPU involvement but does not control interrupt acceptance. Cache coherency ensures memory consistency across cores, not interrupt handling. Context switching changes execution contexts but does not prevent an interrupt from occurring. Therefore, masking is the mechanism for temporary deferral.
Q6. A server process uses nonblocking read on a socket. If recv returns -1 with EAGAIN, what should the process do next?
📖 Explanation: When recv reports EAGAIN, it indicates no data is available and the operation would block. The correct response is to arrange for the OS to notify the process when the socket becomes readable, typically by registering a callback or using select/poll mechanisms. Terminating the connection (A) is unnecessary, retrying immediately (B) would just repeat the error, and blocking (C) defeats the purpose of nonblocking I/O.
Q7. Device A raises a high‑priority interrupt while Device B is processing a low‑priority interrupt. What must the OS ensure?
📖 Explanation: The OS must allow the high‑priority interrupt to pre‑empt the lower‑priority handler so that urgent work is addressed promptly. This pre‑emption is a core part of multilevel interrupt handling. Letting B finish first (A) would increase latency for critical events. Running both concurrently (C) would require separate CPUs and is not typical for a single processor. Ignoring interrupts (D) would cause missed deadlines.
Q8. A real‑time application requires data from a sensor every 5 ms. Using asynchronous I/O, which design ensures the deadline is met?
📖 Explanation: A callback that runs when the sensor data arrives lets the application handle the data immediately and then start the next asynchronous read, maintaining the 5 ms cadence without blocking. Blocking (A) would stall the thread, a busy‑wait (B) consumes CPU needlessly, and delayed queuing (D) violates the deadline. The callback approach aligns with nonblocking, event‑driven design.
Q9. A programmer mistakenly uses blocking I/O in a GUI thread. What symptom is most likely observed?
📖 Explanation: When a GUI thread performs a blocking call, it cannot process user events until the operation completes, causing the interface to appear frozen or unresponsive. Network throughput (A) is unrelated to UI responsiveness. CPU usage dropping (C) would indicate idleness, not blocking. Automatic thread creation (D) does not occur unless the programmer explicitly spawns them. Hence, UI freeze is the expected outcome.
Q10. A programmer implementing a nonblocking file copy checks for EOF after each read. Which mistake could cause data loss?
📖 Explanation: If the code interprets a return value of zero as end‑of‑file, it may stop copying before the final block is transferred, especially when the file size is a multiple of the buffer size. Partial reads (B) must be handled, but treating zero as EOF is the specific error leading to loss. Writing before reading (C) disrupts order, and closing early (D) would truncate data, but the described symptom matches mistake A.
Q11. An embedded system uses an interrupt line to signal when a UART transmission is complete. Which action should the ISR perform?
📖 Explanation: The interrupt service routine should acknowledge the interrupt by clearing its flag, preventing re‑triggering, and set a status variable so the main code knows the transmission finished. Starting another transmission (A) may be appropriate later but not within the ISR without proper coordination. Resetting the CPU (C) is drastic and unnecessary. Entering low‑power mode (D) would halt further processing, which is not the ISR’s purpose.
Q12. What term describes I/O where the request returns immediately and the program is later notified of completion?
📖 Explanation: Asynchronous I/O initiates an operation that returns at once, allowing the program to continue execution while the OS notifies it upon completion, typically via a callback or event. Polling (A) actively checks status, notifies later. Synchronous I/O (B) waits for the operation to finish before returning. Blocking I/O (D) also waits, making the call non‑returning until done. Therefore, asynchronous I/O matches the described behavior.)