【问题标题】:Default behavior of a Unix-based shell with respect to child processes on exit?基于 Unix 的 shell 关于退出时的子进程的默认行为?
【发布时间】:2014-12-22 07:27:10
【问题描述】:

我正在实现一个基本的基于 Unix 的 shell。 shell 维护着一个在后台运行的子进程和已停止进程的列表。

如果退出 shell:

  • shell 是否会杀死所有在后台运行/停止的子进程?

或者

  • shell 是否会简单地忽略存在于后台或处于停止状态的子进程,仅稍后被 init 采用?

请注意,我需要知道 Unix shell 的确切行为。我找到了答案here,但它仍然没有谈论 Unix shell 的确切实现。

【问题讨论】:

    标签: c linux shell unix signals


    【解决方案1】:

    阅读有关SIGHUP 信号的更多信息。每当控制终端退出时,此信号通常会发送到进程组中的所有进程。在 shell 的情况下,这通常发生在终端窗口关闭时。信号的默认行为是终止进程。这就是为什么存在诸如nohup 这样的程序的原因。

    为了编写一个真正的 shell,你真的必须了解整个 TTY 子系统以及信号之间的关系。推荐文章是"The TTY Demystified"

    【讨论】:

    • 我想你会发现内核将 SIGHUP 进程发送到进程组中以终端作为其控制终端的进程组中的所有进程,或者附近。这是一个棘手的领域,需要掌握很多术语。
    【解决方案2】:

    其实SIGHUP(信号号为1,(验证kill -l))是信号,用于关闭所有在终端下运行的进程(可以说是子进程),通过发送该终端向所有进程发出此信号。

    The SIGHUP signal is sent to a process when its controlling terminal is closed. It was 
    originally designed to notify the process of a serial line drop (a hangup). In modern   
    systems, this signal usually means that the controlling pseudo or virtual terminal has 
    been closed.Many daemons will reload their configuration files and reopen their 
    logfiles instead of exiting when receiving this signal.nohup is a command to make a 
    command ignore the signal.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 2011-10-01
      • 2014-11-01
      • 1970-01-01
      • 2010-11-30
      相关资源
      最近更新 更多