Course contentsShow
← Linux
Lesson 492 of 1,8195. Process Management and SignalingFree lesson

Signal Delivery to Multi-Threaded Processes

Discuss how Linux directs a signal to specific threads versus the whole process group.

In this lesson, you’ll learn how Linux differentiates between directing a signal to a specific thread versus targeting an entire multi-threaded process.

The Process vs. Thread Distinction

When you work with multi-threaded processes in Linux, it is important to remember that all threads share the same process ID (PID), but each has a unique thread ID (TID). Because they share the same memory space, Linux must handle incoming signals with precision to avoid crashing the entire application when only one thread needs to be interrupted.

Signals Directed to the Process

When you use a command like kill <pid> or send a signal to the process group, the kernel treats it as a signal for the entire process. In this scenario, the kernel selects one thread within the process that does not have the signal blocked and delivers it there. If the signal causes termination (like SIGKILL or SIGTERM), the entire process—including all its threads—is destroyed.

Signals Directed to a Specific Thread

Sometimes, you want to target only a single thread (for instance, to send a SIGUSR1 to trigger a specific routine). You can use functions like pthread_kill() to send a signal to a specific TID.

When a signal is sent to a specific thread:

  1. It is delivered only to that target thread.
  2. If that thread has the signal blocked, the signal remains pending for that specific thread until it unblocks it.
  3. If the signal is a "terminating" signal (e.g., SIGSEGV), it will still terminate the entire process because the process shares the same memory and life cycle.

A Quick Illustration

Consider a process with a main thread and a worker thread. If the worker thread crashes (generating a hardware exception like SIGSEGV), the entire process dies because the signal impacts the process's shared state.

// Conceptually, sending a signal to a specific thread:
pthread_kill(worker_thread_id, SIGUSR1); 
// This lands only on the worker thread, 
// keeping the main thread running smoothly.

The kernel acts as a traffic controller: it checks the signal's destination to see if you specified a process or a thread, then ensures the delivery adheres to the signal masks of the recipient.

Key Takeaway: Signals sent to a process group or PID may be handled by any eligible thread, while signals sent to a specific thread are delivered exclusively to that thread, though terminating signals will always kill the entire process regardless of the target.