什么是守护进程:生存期长的一种进程,没有控制终端。它们常常在系统引导装入时启动,仅在系统关闭时才终止。
进程组 :
会话:会话(session)是一个或多个进程组的集合,进程调用 setsid 函数(原型:pid_t setsid(void) )建立一个会话。
进程调用 setsid 函数建立一个新会话,如果调用此函数的进程不是一个进程组的组长,则此函数创建一个新会话。具体会发生以下3件事:
如果该调用进程已经是一个进程组的组长,则此函数返回出错。为了保证不处于这种情况,通常先调用fork,然后使其父进程终止,而子进程则继续。因为子进程继承了父进程的进程组ID,而其进程ID是重新分配的,两者不可能相等,这就保证了子进程不是一个进程组的组长

- 作者:Zyoung
- 链接:https://www.zhihu.com/question/38609004/answer/529315259
- 来源:知乎
- 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
-
- void
- daemonize(const char *cmd)
- {
- int i, fd0, fd1, fd2;
- pid_t pid;
- struct rlimit rl;
- struct sigaction sa;
-
- /*
- * Clear file creation mask.
- */
- umask(0);
-
- /*
- * Get maximum number of file descriptors.
- */
- if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
- err_quit("%s: can't get file limit", cmd);
-
- /*
- * Become a session leader to lose controlling TTY.
- */
- if ((pid = fork()) < 0)
- err_quit("%s: can't fork", cmd);
- else if (pid != 0) /* parent */
- exit(0);
- setsid();
-
- /*
- * Ensure future opens won't allocate controlling TTYs.
- */
- sa.sa_handler = SIG_IGN;
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = 0;
- if (sigaction(SIGHUP, &sa, NULL) < 0)
- err_quit("%s: can't ignore SIGHUP", cmd);
- if ((pid = fork()) < 0)
- err_quit("%s: can't fork", cmd);
- else if (pid != 0) /* parent */
- exit(0);
-
- /*
- * Change the current working directory to the root so
- * we won't prevent file systems from being unmounted.
- */
- if (chdir("/") < 0)
- err_quit("%s: can't change directory to /", cmd);
-
- /*
- * Close all open file descriptors.
- */
- if (rl.rlim_max == RLIM_INFINITY)
- rl.rlim_max = 1024;
- for (i = 0; i < rl.rlim_max; i++)
- close(i);
-
- /*
- * Attach file descriptors 0, 1, and 2 to /dev/null.
- */
- fd0 = open("/dev/null", O_RDWR);
- fd1 = dup(0);
- fd2 = dup(0);
-
- /*
- * Initialize the log file.
- */
- openlog(cmd, LOG_CONS, LOG_DAEMON);
- if (fd0 != 0 || fd1 != 1 || fd2 != 2) {
- syslog(LOG_ERR, "unexpected file descriptors %d %d %d",
- fd0, fd1, fd2);
- exit(1);
- }
- }
关于sighup是何时产生的:
SIGHUP会在以下3种情况下被发送给相应的进程:
1、终端关闭时,该信号被发送到session首进程以及作为job提交的进程(即用 & 符号提交的进程);
2、session首进程退出时,该信号被发送到该session中的前台进程组中的每一个进程;
3、若父进程退出导致进程组成为孤儿进程组,且该进程组中有进程处于停止状态(收到SIGSTOP或SIGTSTP信号),该信号会被发送到该进程组中的每一个进程