🎓 BookMCQ
← Back to 4. Threads in Operating System

📝 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.
11
Easy
11
Medium
11
Hard

📝 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?

A.Each thread has an independent SIGTERM disposition that must be installed separately
B.The disposition is process-wide, so the same handler applies to every thread ✅
C.Only the thread that installed the handler can execute it
D.The disposition applies only to threads created after installation
💡 Difficulty: easy | ✅ Correct: B

📖 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?

A.Signal dispositions are stored separately for every thread
B.Every signal is delivered only to the main thread
C.A process can maintain only one signal mask
D.Each thread maintains its own signal mask ✅
💡 Difficulty: easy | ✅ Correct: D

📖 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?

A.Both T1 and T2 must execute the handler together
B.T3 receives it because blocked threads have priority
C.Either T1 or T2 may be selected; T3 is not eligible ✅
D.The signal is discarded because the masks differ
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.Only the receiving worker terminates
B.The receiving worker terminates and the others continue
C.The operating system creates a replacement worker
D.The process terminates, affecting all of its threads ✅
💡 Difficulty: easy | ✅ Correct: D

📖 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?

A.kill with the process identifier
B.pthread_kill with the target thread identifier ✅
C.pthread_sigmask with the target thread identifier
D.sigwait with the target thread identifier
💡 Difficulty: easy | ✅ Correct: B

📖 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?

A.T4, because the fault arose from its executing instruction ✅
B.The main thread, because dispositions are process-wide
C.Any thread that currently has the most free CPU time
D.Every thread, because a memory fault is process-wide
💡 Difficulty: easy | ✅ Correct: A

📖 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?

A.The signal disposition changes after every delivery
B.A process-directed signal is always delivered to the oldest thread
C.The operating system selects an eligible thread, and the choice is not a worker-affinity guarantee ✅
D.Process-directed signals can be received only by newly created threads
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.A blocked signal is always discarded before reaching any thread
B.Blocking changes T1 mask only; an unblocked T2 may remain eligible ✅
C.The signal becomes thread-directed to T1 after it is blocked
D.Only the main thread can change a process signal mask
💡 Difficulty: easy | ✅ Correct: B

📖 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?

A.Whether the helper inherited the creator blocked signal mask ✅
B.Whether the process installed a separate handler for the helper
C.Whether the helper has a different process identifier
D.Whether SIGUSR2 is automatically converted into a synchronous fault
💡 Difficulty: easy | ✅ Correct: A

📖 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?

A.Every eligible thread receives a separate copy of the signal
B.The signal is broadcast only when at least three threads are eligible
C.A single process-directed signal reaches one eligible thread, not every eligible thread ✅
D.No signal can be delivered when more than one thread is eligible
💡 Difficulty: easy | ✅ Correct: C

📖 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?

A.sigaction
B.pthread_kill
C.kill
D.pthread_sigmask ✅
💡 Difficulty: easy | ✅ Correct: D

📖 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?

A.The signal is immediately delivered to every blocked thread
B.The signal remains pending for the process until it can be accepted ✅
C.The signal is permanently discarded because no thread was eligible
D.The signal is converted into a thread-directed signal for the main thread
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.Treating signal generation as distinct from signal delivery
B.Assuming a blocked signal can remain pending
C.Equating blocked delivery with immediate signal loss ✅
D.Recognizing that signal masks are per-thread
💡 Difficulty: medium | ✅ Correct: C

📖 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?

A.T1 only, because it is the first worker
B.T3 only, because it blocks other notifications
C.All three threads equally, regardless of their masks
D.T2 only, because it is the sole thread that does not block SIGHUP ✅
💡 Difficulty: medium | ✅ Correct: D

📖 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?

A.They inherit a copy of the creator signal mask, including the blocked SIGTERM ✅
B.They begin with every signal unblocked regardless of the creator
C.They inherit the process disposition but never the signal mask
D.They inherit only signals that were pending at creation time
💡 Difficulty: medium | ✅ Correct: A

📖 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?

A.pthread_sigmask changes the signal disposition instead of a mask
B.pthread_sigmask changes only the calling thread mask, so each thread needs its own configuration ✅
C.pthread_sigmask can be called only before a process creates threads
D.Signal masks are shared only among threads with equal priorities
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.Three workers execute the handler and one remains idle
B.The signal is discarded because most workers block it
C.The one unblocked worker is the only eligible recipient ✅
D.The blocked workers receive it before the unblocked worker
💡 Difficulty: medium | ✅ Correct: C

📖 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?

A.Block SIGINT in every thread and have the designated thread include it in its sigwait set ✅
B.Unblock SIGINT in every worker and make each worker call sigwait concurrently
C.Install a different SIGINT disposition in every worker and call kill from the receiver
D.Block SIGINT only in the receiver so that workers cannot observe it
💡 Difficulty: medium | ✅ Correct: A

📖 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?

A.A new process that inherits the pending signal set
B.A way to replace the process-wide signal disposition permanently
C.A broadcast of the signal to every thread in the set
D.Synchronous acceptance of one selected signal, reported to the waiting thread ✅
💡 Difficulty: medium | ✅ Correct: D

📖 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?

A.The handler should have assumed M was process-wide
B.A signal handler may safely acquire any mutex if it runs in the owner thread
C.The handler tries to reacquire a mutex already held by the interrupted thread, causing deadlock ✅
D.The operating system automatically releases M before invoking a handler
💡 Difficulty: medium | ✅ Correct: C

📖 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?

A.Those routines can be unavailable whenever a process has more than one thread
B.They may use shared internal state or locks and are not generally async-signal-safe ✅
C.They automatically deliver the same signal to every worker
D.They always change the disposition of the signal being handled
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.The signal is converted into a synchronous fault in the event loop
B.The handler can safely perform all logging because the pipe serializes threads
C.The pipe changes the process-wide signal disposition for future signals
D.The handler performs minimal notification while complex work runs in normal execution context ✅
💡 Difficulty: medium | ✅ Correct: D

📖 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?

A.The flag makes every shared object automatically thread-safe
B.The flag causes process-directed signals to be broadcast
C.The flag is suitable for simple handler communication but does not replace general interthread synchronization ✅
D.The flag allows a handler to call nonreentrant library functions safely
💡 Difficulty: hard | ✅ Correct: C

📖 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?

A.The handler records a shutdown request, then normal control flow stops admission, coordinates workers, and releases resources ✅
B.The handler immediately frees every shared object and exits one worker
C.Every worker independently commits or rolls back the same transaction
D.The handler waits for all worker mutexes while still executing asynchronously
💡 Difficulty: hard | ✅ Correct: A

📖 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?

A.One function call is always atomic with respect to signal delivery
B.The function is safe because the handler executes on a separate stack
C.Allocation and locking are safe whenever only one signal is pending
D.The safety of the call depends on its internals; one call can still invoke non-async-signal-safe operations ✅
💡 Difficulty: hard | ✅ Correct: D

📖 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?

A.Every send creates a separately queued instance, but the handler discards extras
B.The process mask changes the signal disposition after the first send
C.The sends are converted into thread-directed signals for different workers
D.Repeated instances of the same pending standard signal can coalesce into one pending notification ✅
💡 Difficulty: hard | ✅ Correct: D

📖 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?

A.The second call may reenter shared library state that the interrupted call left temporarily inconsistent ✅
B.printf is defined to terminate every thread except the one receiving the signal
C.The handler always runs in a separate process with a copied output buffer
D.The signal mask prevents printf from accessing its own buffer
💡 Difficulty: hard | ✅ Correct: A

📖 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?

A.It is redirected automatically to T1 because T1 is eligible
B.It remains pending for T3 until T3 can accept it, rather than becoming a process-directed signal for T1 or T2 ✅
C.It is delivered to all three threads because the disposition is process-wide
D.It is discarded immediately whenever the target blocks it
💡 Difficulty: hard | ✅ Correct: B

📖 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?

A.An unblocked thread receives the signal only if it is the main thread
B.The signal is copied to each worker but handlers are serialized
C.Only one eligible thread is selected for a single process-directed signal ✅
D.A process-directed signal can be delivered only when every thread blocks it
💡 Difficulty: hard | ✅ Correct: C

📖 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?

A.Free the cache first, then ask workers to stop using it
B.Allow each worker to free cache entries as soon as it notices the signal
C.Terminate the receiver, continue accepting work, and rely on process exit for consistency
D.Stop new submissions, let workers finish or cancel safely, join them, then release shared cache resources ✅
💡 Difficulty: hard | ✅ Correct: D

📖 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?

A.The signal was necessarily delivered to the wrong process because the job waited
B.Signal reception succeeded; worker-pool capacity or scheduling delayed the application task ✅
C.sigwait guarantees that every submitted task starts before any worker continues
D.The delay proves that SIGHUP was blocked in every thread at reception time
💡 Difficulty: hard | ✅ Correct: B

📖 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?

A.Only M can receive S because it created both workers
B.T2 must receive S because it was created most recently
C.T1 and T2 are both eligible, and the system may choose either one ✅
D.No thread is eligible because M originally blocked S
💡 Difficulty: hard | ✅ Correct: C

📖 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?

A.The shared disposition applies process-wide, but the fault signal is directed to T4 ✅
B.The handler belongs only to T1 because it was executing when the fault occurred
C.The fault is process-directed and may be delivered to any unblocked worker
D.Each worker must install its own handler before T4 can receive the fault
💡 Difficulty: hard | ✅ Correct: A

📖 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.

🔗 Related Topics (MCQs)