猿问

创建守护进程时执行双叉的原因是什么?

创建守护进程时执行双叉的原因是什么?

我试图在python中创建一个守护进程。我找到了以下问题,它有一些好的资源,我现在正在跟踪,但我很好奇为什么需要一个双叉。我在谷歌(Google)周旋,发现了大量的资源,宣称这是必要的,但不是为什么。

有人提到,这是为了防止守护进程获取控制终端。如果没有第二个叉子,它怎么能做到这一点?后果是什么?


qq_花开花谢_0
浏览 370回答 3
3回答

慕沐林林

看看问题中引用的代码,理由是:# Fork a second child and exit immediately to prevent zombies.  This # causes the second child process to be orphaned, making the init # process responsible for its cleanup.  And, since the first child is # a session leader without a controlling terminal, it's possible for # it to acquire one by opening a terminal in the future (System V-# based systems).  This second fork guarantees that the child is no # longer a session leader, preventing the daemon from ever acquiring# a controlling terminal.因此,这是为了确保守护进程被重新授予init(以防启动守护进程的进程长寿),并消除守护进程重新获取控制TTY的任何机会。因此,如果这两种情况都不适用,那么一个叉子就足够了。“Unix网络编程-Stevens“这方面有一个很好的章节。
随时随地看视频慕课网APP

相关分类

Python
我要回答