You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

进程信号处理顺序、未递送信号存储结构及SIGCONT/SIGHUP处理疑问

Unix Signal Handling: Order, Pending Storage, and the SIGHUP/SIGCONT Orphan Process Group Puzzle

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 SIGKILL and SIGSTOP always 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).
  • Process state: If the process is stopped, only SIGCONT and SIGKILL trigger 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

  1. How does SIGCONT skip SIGHUP for delivery?
    The kernel treats SIGCONT (and SIGKILL) as exceptions to the "stopped processes block signal delivery" rule. When SIGCONT is sent to a stopped process, the kernel first resumes the process, then delivers SIGCONT (its default action is already in motion). After that, the process checks its pending signals and handles SIGHUP.

  2. Why isn't SIGHUP discarded?
    SIGHUP is 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 to SIG_IGN before delivery. Since SIGHUP's default action is termination (unless handled), it stays pending until the process is resumed.

  3. Is this based on FIFO queues or FILO stacks?
    For non-real-time signals like SIGHUP and SIGCONT, the pending set is a bitmap—not a queue or stack. Delivery order is determined by signal priority, not arrival order. SIGCONT has higher priority than SIGHUP, so once the process is awake, SIGCONT is processed first, followed by SIGHUP. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:55:21