c - fork 一个长时间运行的进程并避免调用 waitpid 来清理僵尸?

标签 c linux fork waitpid

我有一个长时间运行的进程 (node.js),它调用 fork(作为 C++ 模块的一部分)。这会将新进程创建为 node.js 进程的子进程。但是,没有任何东西会为这个子进程等待/waitpid,所以它在终止后仍然是僵尸。

是否有可能在当前进程不是其父进程的情况下 fork() 一个进程,以便在终止时它不会保持僵尸状态而是被清理?

如果不是,我能否以某种方式表明我不会对 child 调用 waitpid 并且不关心它何时终止?

如果这一切都失败了,我可以编写/找到一个可以执行 waitpid 的 native 模块,但我需要确定它会:

  1. 不阻塞父进程(node.js)
  2. 调用模块函数后不留下任何僵尸

谢谢!

最佳答案

这是我用来创建守护进程的代码。注释描述了为什么完成每个步骤。

Status daemon_init(void)
{
  pid_t pid;
  int fh;

  /*-----------------------------------------------------------------------
  ; From the Unix Programming FAQ (corraborated by Stevens):
  ;
  ; 1. 'fork()' so the parent can exit, this returns control to the command
  ; line or shell invoking your program. This step is required so that
  ; the new process is guaranteed not to be a process group leader. The
  ; next step, 'setsid()', fails if you're a process group leader.
  ;---------------------------------------------------------------------*/

  pid = fork();
  if (pid == (pid_t)-1)
    return retstatus(false,errno,"fork()");
  else if (pid != 0)    /* parent goes bye bye */
    _exit(EXIT_SUCCESS);

  /*-------------------------------------------------------------------------
  ; 2. 'setsid()' to become a process group and session group leader. Since
  ; a controlling terminal is associated with a session, and this new
  ; session has not yet acquired a controlling terminal our process now
  ; has no controlling terminal, which is a Good Thing for daemons.
  ;
  ; _Advanced Programming in the Unix Environment_, 2nd Edition, also
  ; ignores SIGHUP. So adding that here as well.
  ;-----------------------------------------------------------------------*/

  setsid();
  set_signal_handler(SIGHUP,SIG_IGN);   /* ignore this signal for now */

  /*-------------------------------------------------------------------------
  ; 3. 'fork()' again so the parent, (the session group leader), can exit.
  ; This means that we, as a non-session group leader, can never regain a
  ; controlling terminal.
  ;------------------------------------------------------------------------*/

  pid = fork();
  if (pid == (pid_t)-1)
    return retstatus(false,errno,"fork(2)");
  else if (pid != 0)    /* parent goes bye bye */
    _exit(EXIT_SUCCESS);

  /*-------------------------------------------------------------------------
  ; 4. 'chdir("/")' to ensure that our process doesn't keep any directory in
  ; use. Failure to do this could make it so that an administrator
  ; couldn't unmount a filesystem, because it was our current directory.
  ;
  ; [Equivalently, we could change to any directory containing files
  ; important to the daemon's operation.]
  ;
  ; I just made sure the name of the script we are using contains the full
  ; path.
  ;-------------------------------------------------------------------------*/

  chdir("/");

  /*-----------------------------------------------------------------------
  ; 5. 'umask(022)' so that we have complete control over the permissions of
  ; anything we write. We don't know what umask we may have inherited.
  ;-----------------------------------------------------------------------*/

  umask(022);

  /*-----------------------------------------------------------------------
  ; 6. 'close()' fds 0, 1, and 2. This releases the standard in, out, and 
  ; error we inherited from our parent process. We have no way of knowing
  ; where these fds might have been redirected to. Note that many daemons
  ; use 'sysconf()' to determine the limit '_SC_OPEN_MAX'.
  ; '_SC_OPEN_MAX' tells you the maximun open files/process. Then in a
  ; loop, the daemon can close all possible file descriptors. You have to
  ; decide if you need to do this or not. If you think that there might
  ; be file-descriptors open you should close them, since there's a limit
  ; on number of concurrent file descriptors.
  ;
  ; 7. Establish new open descriptors for stdin, stdout and stderr. Even if
  ; you don't plan to use them, it is still a good idea to have them
  ; open. The precise handling of these is a matter of taste; if you
  ; have a logfile, for example, you might wish to open it as stdout or
  ; stderr, and open '/dev/null' as stdin; alternatively, you could open
  ; '/dev/console' as stderr and/or stdout, and '/dev/null' as stdin, or
  ; any other combination that makes sense for your particular daemon.
  ;
  ; We do this here via dup2(), which combines steps 6 & 7.
  ;------------------------------------------------------------------------*/

  fh = open(DEV_NULL,O_RDWR);
  if (fh == -1)
    return retstatus(false,errno,"open(" DEV_NULL ")");

  assert(fh > 2);

  dup2(fh,STDIN_FILENO);
  dup2(fh,STDOUT_FILENO);
  dup2(fh,STDERR_FILENO);

  close(fh);

  return c_okay;
}

如果您想在上下文中查看此函数,可以在此处查看:https://github.com/spc476/syslogintr/blob/master/syslogintr.c

关于c - fork 一个长时间运行的进程并避免调用 waitpid 来清理僵尸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14971314/

相关文章:

c - sizeof-operator 反向行为

c - C 中的 Readline 函数输出奇怪的结果

linux - pkill 通过远程 ssh 结合另一个命令返回 255

linux - 您如何在此行上使用 'grep'? Linux

C 读取套接字偶尔发生变化

C:一个 malloced block 用于多个不同类型的结构

c - 为什么根节点值无效?

c++ - 如何使用cmake将cuda文件正确链接到cpp文件

c - 当两个进程(父进程和子进程)在同一代码上工作时,使用 fork() 系统调用有什么意义?

multithreading - Perl:从并行线程派生