进程信号处理顺序、未递送信号存储结构及SIGCONT/SIGHUP处理疑问
Hey there! Let's unpack your questions one by one—this is a really interesting deep dive into how Unix signals work under the hood.
1. What's the order in which a process handles multiple incoming signals?
The handling order depends on a few key factors:
- Signal priority: Kernel-defined priorities take precedence here. Critical signals like
SIGKILLandSIGSTOPalways get top billing. For other signals, most Unix systems follow a default priority hierarchy (minor variations exist across implementations). - Signal masking: If a signal is blocked (via
sigprocmask), it stays in a pending state until unblocked. When unblocked, it’s processed in line with its priority. - Real-time vs non-real-time signals:
- Non-real-time signals (e.g.,
SIGHUP,SIGCONT) don’t queue—multiple instances of the same signal only mark it as pending once. They’re processed by priority when the process is ready. - Real-time signals (SIGRTMIN to SIGRTMAX) use a FIFO queue, so every arrival is tracked and delivered in the order it was sent (still respecting priority relative to non-real-time signals).
- Non-real-time signals (e.g.,
- Process state: If the process is stopped, only
SIGCONTandSIGKILLtrigger immediate action—all other signals are held pending until the process resumes.
2. What data structure stores signals that have arrived but not yet been delivered?
It varies by signal type:
- Non-real-time signals: A signal set (bitmap). Each bit represents whether a specific signal is pending. Since these signals don’t queue, multiple instances of the same signal just set the same bit once.
- Real-time signals: A FIFO queue. Each entry holds the signal number and accompanying data, ensuring every arrival is tracked and delivered in order.
The SIGHUP/SIGCONT Orphan Process Group Conundrum
Let’s resolve the apparent conflict between APUE and the Stack Overflow observation by clarifying two key terms first:
- Sending: The kernel dispatches the signal to the process.
- Delivery: The process actually runs the signal handler (or takes the default action).
POSIX.1 Mandate vs. Real-World Execution
POSIX.1要求向组内所有停止状态的进程先发送挂起信号(SIGHUP),再发送继续信号(SIGCONT),预期子进程处理完SIGHUP后恢复执行。
POSIX is referring to the sending order: the kernel sends SIGHUP first, then SIGCONT. But here’s the catch: when the process is stopped, SIGHUP can’t be delivered immediately—it moves into the pending signal set.
SIGCONT is a special case: regardless of the process’s stopped state, the kernel immediately processes SIGCONT to resume the process. Once awake, the process then handles the pending SIGHUP.
Your Specific Questions Answered
How does SIGCONT skip SIGHUP for delivery?
The kernel treatsSIGCONT(andSIGKILL) as exceptions to the "stopped processes block signal delivery" rule. WhenSIGCONTis sent to a stopped process, the kernel first resumes the process, then deliversSIGCONT(its default action is already in motion). After that, the process checks its pending signals and handlesSIGHUP.Why isn't SIGHUP discarded?
SIGHUPis stored in the process’s pending signal set (the bitmap we mentioned) while the process is stopped. It’s only discarded if the process explicitly ignores it, or if the signal is set toSIG_IGNbefore delivery. SinceSIGHUP's default action is termination (unless handled), it stays pending until the process is resumed.Is this based on FIFO queues or FILO stacks?
For non-real-time signals likeSIGHUPandSIGCONT, the pending set is a bitmap—not a queue or stack. Delivery order is determined by signal priority, not arrival order.SIGCONThas higher priority thanSIGHUP, so once the process is awake,SIGCONTis processed first, followed bySIGHUP. If multiple pending signals of different priorities exist, the highest priority one is handled first.
In short: APUE’s "process SIGHUP then resume" describes the logical end result (the process handles SIGHUP before continuing normal execution), while the Stack Overflow answer covers the low-level delivery order (SIGCONT wakes the process first, then SIGHUP is handled). They’re not conflicting—just describing different layers of the process.
内容的提问来源于stack exchange,提问作者Tim

