📝 Signal handling in multithreaded programs (33 MCQs)
Signal Delivery and Thread-Level Event Handling
Signal handling in multithreaded programs coordinates asynchronous notifications and certain synchronous events across threads within one process. In POSIX systems, signal dispositions are generally process-wide, while signal masks are maintained separately for each thread. A signal may be directed to a particular thread or generated for the process, requiring defined rules for identifying an eligible recipient.
Why Multiple Threads Complicate Signal Delivery
A single-threaded process has one execution context that can respond to a delivered signal. Multithreaded applications contain several execution contexts, creating questions about which thread should handle a process-directed signal. Independent signal masks and thread-specific execution states make delivery more complex, particularly when signals interrupt library operations or interact with shared application resources.
Signal Masks, Handlers, and Synchronous Signal Waiting
Each thread can block selected signals using its signal mask. A process-directed signal is delivered to an eligible thread that has not blocked it, according to system rules. A synchronous fault such as an invalid memory access is directed to the thread that caused it. Applications can use sigwait() to synchronously accept selected blocked signals in a designated thread.
Handling a Process-Directed Signal with Three Threads
Suppose a process contains three threads, T1, T2, and T3. All block the SIGTERM signal except T2, which leaves it unblocked. When a process-directed SIGTERM is generated, T2 is eligible to receive it. If the application blocks it in every thread, a designated signal-handling thread can wait for it using sigwait().
Handler Safety, Signal Masks, and Application Reliability
Correct signal handling supports orderly termination, notification, and recovery-related operations. However, asynchronous handlers have strict safety limitations because they may interrupt code while shared library or application state is inconsistent. POSIX requires care with functions used inside signal handlers, and complex work is often deferred to a suitable thread through controlled notification mechanisms.
Misconceptions About Signals and Thread Selection
'Every thread receives a process-directed signal' is incorrect because it is normally delivered to one eligible thread. 'Signal handlers are independently installed by every thread' is incorrect because dispositions are process-wide. 'Blocking a signal in one thread blocks it for the entire process' is incorrect because signal masks are per-thread.
Exam Focus: Signal Dispositions, Masks, and Delivery
Distinguish process-directed signals from thread-directed signals and explain how signal masks influence eligibility. Identify which thread receives a synchronous fault and how sigwait() can support controlled handling. Questions may test process-wide dispositions, per-thread masks, handler restrictions, blocked signals, and the problems caused by asynchronous execution.
Key Terms of Multithreaded Signal Handling
- Signal - Notification of an event.
- Signal handler - Function responding to a signal.
- Signal mask - Set of blocked signals.
- Process-directed signal - Signal addressed to a process.
- Thread-directed signal - Signal addressed to one thread.
- sigwait() - Synchronously accepts selected signals.
📝 All Signal handling in multithreaded programs MCQs
Q1. A multithreaded service installs a handler for SIGTERM before creating its worker threads. Which statement best describes the handler disposition?
📖 Explanation: Signal dispositions are process-wide attributes, unlike signal masks. Installing a handler changes the action associated with that signal for the process, so any thread receiving the signal uses the same installed disposition.
Q2. A program needs to prevent selected signals from being delivered to one worker while leaving other workers eligible. Which property makes this possible?
📖 Explanation: A signal mask belongs to an individual thread and records which signals that thread blocks. Changing one worker mask therefore changes that worker eligibility without automatically changing the masks of its peers.
Q3. A web server has threads T1, T2, and T3. T1 and T2 leave SIGUSR1 unblocked, while T3 blocks it. A process-directed SIGUSR1 arrives. What should the server expect?
📖 Explanation: A process-directed signal is delivered to one eligible thread, not broadcast to all threads. Since T1 and T2 have SIGUSR1 unblocked, either may receive it, while blocked T3 is excluded from selection.
Q4. A process installs the default termination action for a signal. The signal is delivered to one worker thread. What is the resulting scope of the default action?
📖 Explanation: Signal disposition determines the action at process scope. A default action that terminates the process does not merely stop the thread selected for delivery; the whole process and its threads are affected.
Q5. A monitoring component must target a particular POSIX worker thread with a signal. Which interface is designed for that purpose?
📖 Explanation: The pthread_kill interface directs a signal to a specified thread in the same process. The kill interface normally targets a process or process group, while pthread_sigmask changes a calling thread mask.
Q6. In a four-thread numerical service, T4 performs an invalid memory access that generates a synchronous SIGSEGV. Which thread is the signal directed to?
📖 Explanation: A synchronous fault such as an invalid memory access is associated with the instruction executed by the faulting thread. The resulting signal is thread-directed to T4, even though its disposition is process-wide.
Q7. A developer assumes that the same worker will always receive a process-directed signal because that worker handled the previous occurrence. Why is this assumption unsafe?
📖 Explanation: When more than one thread is eligible, the application must not depend on a particular worker being chosen. Delivery selection is not a stable affinity rule, so designs should control eligibility explicitly.
Q8. A student writes: T1 blocks SIGTERM, therefore SIGTERM is blocked for the entire process and cannot reach T2. Which correction identifies the flaw?
📖 Explanation: The flawed step treats a thread mask as a process-wide setting. Signal masks are per-thread, so blocking SIGTERM in T1 does not block it in T2; an unblocked T2 may still receive a process-directed signal.
Q9. A worker blocks SIGUSR2 and then creates a helper thread. The helper is intended to receive SIGUSR2, but it is never selected. What should be checked first?
📖 Explanation: A newly created POSIX thread inherits a copy of its creator signal mask. The helper may therefore block SIGUSR2 from birth; the program must explicitly arrange the desired mask before expecting delivery.
Q10. A test sends one process-directed signal while varying the number of eligible threads. The measured pairs are (eligible threads, handler threads invoked): (0,0); (1,1); (2,1); (3,1); (4,1); (5,1). Which interpretation fits the data?
📖 Explanation: The measurements show one handler invocation whenever at least one thread is eligible. Process-directed delivery selects one eligible recipient; increasing the eligible population does not turn one signal into a broadcast.
Q11. Which interface is explicitly specified for examining or changing a calling thread signal mask in a POSIX multithreaded program?
📖 Explanation: The pthread_sigmask interface examines or changes the calling thread signal mask in a POSIX multithreaded application. It is intended for this setting, whereas sigaction changes a signal disposition shared by the process.
Q12. A process-directed signal is generated while every thread blocks it. Assuming the signal is caught rather than ignored, what is the expected state before a thread becomes able to accept it?
📖 Explanation: Blocking prevents delivery rather than necessarily discarding a signal. With no eligible thread, a process-directed signal can remain pending for the process until an appropriate thread unblocks it or accepts it synchronously.
Q13. A student observes that a process-directed signal arrived while all threads blocked it and concludes that the operating system must discard it. Which step in this reasoning is incorrect?
📖 Explanation: The incorrect step equates blocking with discarding. A generated signal can remain pending while delivery is prevented; it may be accepted later when an eligible thread unblocks it or uses a synchronous waiting mechanism.
Q14. A database process has three threads. T1 and T3 block SIGHUP, while T2 leaves it unblocked. An administrator sends SIGHUP to the process to request a reload. Which thread is eligible to receive it?
📖 Explanation: The process-directed SIGHUP can be delivered to a thread that has not blocked it. T1 and T3 are ineligible because their masks block the signal, leaving T2 as the only eligible recipient.
Q15. A server blocks SIGTERM in its main thread before creating workers. What initial SIGTERM mask state do newly created workers receive?
📖 Explanation: A new POSIX thread inherits a copy of the creating thread signal mask. Blocking SIGTERM before worker creation therefore establishes the blocked state in each new worker unless application code changes it afterward.
Q16. A developer calls pthread_sigmask in worker T1 and expects it to block SIGINT in T1, T2, and T3 simultaneously. Why will this design fail?
📖 Explanation: pthread_sigmask operates on the calling thread signal mask, not a process-wide collection of masks. To establish uniform blocking, the application must configure each existing thread or create them from a thread with the intended mask.
Q17. A process has four workers and no thread waiting synchronously for signal S. Measurements show (workers blocking S, workers eligible): (0,4); (1,3); (2,2); (3,1); (4,0). If three workers block S when a process-directed S arrives, what follows?
📖 Explanation: The table leaves one eligible worker when three of four block S. A process-directed signal can be delivered to that remaining unblocked thread; blocked workers are not selected merely because they form a majority.
Q18. A service wants one dedicated thread to accept SIGINT with sigwait while worker threads continue ordinary processing. Which configuration is appropriate?
📖 Explanation: sigwait synchronously accepts signals that are blocked and included in its set. Blocking SIGINT across the process threads prevents asynchronous worker delivery, allowing the designated receiver to handle it predictably.
Q19. What does sigwait provide to a thread that is waiting for a selected set of blocked signals?
📖 Explanation: sigwait suspends the calling thread until an eligible signal from its specified set is available, then reports which signal was accepted. This handles notification synchronously rather than invoking an asynchronous handler.
Q20. A handler is invoked in T1 after T1 was interrupted while holding mutex M. The handler calls pthread_mutex_lock on M and execution stops progressing. Which explanation best identifies the flawed step?
📖 Explanation: The flawed step is acquiring M from the handler while the interrupted execution already owns it. The handler cannot resume until it returns, so it may wait forever for a mutex only that suspended execution can release.
Q21. Why should a signal handler avoid calling complex library routines such as printf or malloc?
📖 Explanation: Complex library routines may be interrupted while their internal buffers, allocator metadata, or locks are in an inconsistent state. Calling them again from a handler can corrupt that state or deadlock, so handlers should remain minimal.
Q22. A Unix service uses a signal handler to write one byte to a self-pipe monitored by its event loop. The event loop then performs logging and configuration parsing. What is the main advantage?
📖 Explanation: A minimal handler can use an async-signal-safe notification operation such as write, while the event loop performs complex work later. This separates asynchronous delivery from operations that may use locks or shared library state.
Q23. A C program uses a volatile sig_atomic_t flag that a handler sets and the main loop reads. Which limitation should the developer still recognize?
📖 Explanation: volatile sig_atomic_t can support simple communication between a signal handler and interrupted code under the relevant C rules. It does not make compound operations or unrelated shared data structures safe across multiple threads.
Q24. A worker pool receives SIGTERM during a large transaction. Which response best combines prompt notification with safe cleanup?
📖 Explanation: The handler should request shutdown without performing complex transaction work. Normal application logic can stop new tasks, let active operations reach safe boundaries, coordinate workers, and release shared resources only after their use ends.
Q25. A programmer argues that a handler is safe because it makes only one call to a logging function, and that function internally allocates memory and acquires a lock. What is wrong with the argument?
📖 Explanation: The flawed reasoning counts source-level calls rather than examining what the function does. Internal allocation or locking can touch inconsistent state or deadlock when interrupted, so the function is not safe merely because it is called once.
Q26. A process blocks one ordinary signal S, then sends S repeatedly before unblocking it. The measured pairs are (send operations, handler invocations after unblocking): (0,0); (1,1); (2,1); (5,1); (10,1); (20,1). Which explanation best fits?
📖 Explanation: Traditional standard signals do not provide a durable count of repeated occurrences while one instance is already pending. The flat measurements are consistent with coalescing, so applications needing exact event counts require another communication mechanism.
Q27. A signal interrupts a worker while the C library is midway through updating a buffered output structure. Why can calling printf again from the handler corrupt execution?
📖 Explanation: Buffered I/O routines maintain shared bookkeeping such as buffer positions and counters. If a handler reenters printf while the interrupted call has only partly updated that state, the nested operation can corrupt the stream.
Q28. A controller uses pthread_kill to send signal S to worker T3, but T3 currently blocks S. T1 and T2 leave S unblocked. What should happen to this thread-directed signal?
📖 Explanation: pthread_kill targets a particular thread, so T3 remains the intended recipient. If T3 blocks the signal, it can remain pending for that thread; other workers do not become substitute recipients just because they are unblocked.
Q29. A student says: A process-directed signal is generated once, so every unblocked worker must run the handler once. Which correction fixes the reasoning?
📖 Explanation: The mistaken step treats a process-directed signal as a broadcast event. The delivery rules select one eligible thread for the signal; other unblocked workers do not receive automatic copies of that same occurrence.
Q30. A file server must shut down after receiving a termination request. Which sequence best protects shared file-cache objects?
📖 Explanation: The server should prevent new references before reclaiming shared objects. Workers must reach a controlled stopping point and be joined or otherwise proven quiescent; only then can shared cache resources be released safely.
Q31. A dedicated thread accepts SIGHUP with sigwait immediately, then submits a reload job to a saturated worker pool. The reload starts much later. Which diagnosis best separates the two stages?
📖 Explanation: The receiver accepted the signal promptly, so delivery itself need not be failing. The later delay occurs after reception, when the reload waits for execution capacity in a saturated pool or its scheduling policy.
Q32. Main thread M blocks signal S, creates T1, and T1 unblocks S before creating T2. M remains blocked. A process-directed S arrives. Which conclusion is correct?
📖 Explanation: T1 changed its own mask before creating T2, so T2 inherited an unblocked copy. M remains blocked, leaving T1 and T2 eligible; the application cannot assume which of those two receives the process-directed signal.
Q33. A process-wide handler is installed for a synchronous fault signal. Worker T4 then causes an invalid memory access while T1 is executing normally. Which statement correctly combines disposition scope and delivery target?
📖 Explanation: The handler disposition is shared by the process, but a synchronous fault is tied to the thread whose instruction caused it. Therefore T4 is the delivery target, and the process-wide handler runs in that thread.