首页 > Web开发 > 详细

守护进程 ppid 1 https://unix.stackexchange.com/questions/240646/why-we-use-setsid-while-daemonizing-a-process nohup 修改ppid 为1

时间:2020-01-11 19:40:04      阅读:97      评论:0      收藏:0      [点我收藏+]

守护进程


ppid 1


https://unix.stackexchange.com/questions/240646/why-we-use-setsid-while-daemonizing-a-process

nohup 修改ppid 为1

 

https://pubs.opengroup.org/onlinepubs/7908799/xsh/setsid.html

 

 

https://github.com/karelzak/util-linux/blob/master/sys-utils/setsid.c

 

 

https://unix.stackexchange.com/questions/316186/how-does-nohup-work

 

 

 

The act of using nohup simply changes the PPID of the spawned process to 1 (init) so that when the shell exits, it is no longer a child process of that shell and so therefor doesn‘t receive a HUP.

EDIT: Should think more before I post sometimes. Leaving it here to remind me of my shame :-(

 

 

https://zh.wikipedia.org/wiki/孤儿进程

在操作系统领域中,孤儿进程(Orphan Process)指的是在其父进程执行完成或被终止后仍继续运行的一类进程

 

解决办法

“收养”

类UNIX操作系统中,为避免孤儿进程退出时无法释放所占用的资源而僵死,任何孤儿进程产生时都会立即为系统进程initsystemd自动接收为子进程,这一过程也被称为“收养”(英语:re-parenting)[1]。在此需注意,虽然事实上该进程已有init作为其父进程,但由于创建该进程的进程已不存在,所以仍应称之为“孤儿进程”。

进程组

因为父进程终止或崩溃都会导致对应子进程成为孤儿进程,所以也无法预料一个子进程执行期间是否会被“遗弃”。有鉴于此,多数类UNIX系统都引入了进程组以防止产生孤儿进程:在父进程终止后,用户的Shell会将父进程所在进程组标为“孤儿进程组”,并向终止的进程下属所有子进程发出SIGHUP信号,以试图结束其运行,如此避免子进程继续以“孤儿进程”的身份运行[2]

远程调用的情况

RPC过程中也会产生孤儿进程。例如,若客户端进程在发起请求后突然崩溃,且对应的服务器端进程仍在运行,则该服务器端进程就会成为孤儿进程。这样的孤儿进程会浪费服务器的资源,甚至有耗尽资源的潜在危险,但也有对应的解决办法[3]

  1. 终止机制:强制杀死孤儿进程(最常用的手段);
  2. 再生机制:服务器在指定时间内查找调用的客户端,若找不到则直接杀死孤儿进程;
  3. 超时机制:给每个进程指定一个确定的运行时间,若超时仍未完成则强制终止之。若有需要,亦可让进程在指定时间耗尽之前申请延时。

“孤儿进程”的应用

除此之外,用户也可能会刻意使进程成为孤儿进程,以使之与用户会话脱钩,并转至后台运行。这一做法常应用于启动需要长时间运行的进程,也即守护进程[4]。另外,UNIX命令nohup也可以完成这一操作[5]

 

 

https://linux.die.net/man/3/execvp

The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2). (See the manual page for execve(2) for further details about the replacement of the current process image.)

The initial argument for these functions is the name of a file that is to be executed.

守护进程 ppid 1 https://unix.stackexchange.com/questions/240646/why-we-use-setsid-while-daemonizing-a-process nohup 修改ppid 为1

原文:https://www.cnblogs.com/yuanjiangw/p/12180665.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!