Linux守护进程

基础概念

进程组

  • 每个进程除了有一个进程ID之外,还属于一个进程组。
  • 进程组是一个或多个进程的集合,同一进程组中的各进程接收来自同一终端的各种信号。
  • 每个进程组有一个组长进程。组长进程的进程组ID等于其进程ID。

会话

会话(session)是一个或多个进程组的集合,进程调用 setsid 函数(原型:pid_t setsid(void) )建立一个会话。进程调用setsid函数建立一个新会话,如果调用此函数的进程不是一个进程组的组长,则此函数创建一个新会话。具体会发生以下3件事:

  • 该进程变成新会话的会话首进程(sessionleader,会话首进程是创建该会话的进程)。此时,该进程是新会话的唯一进程。
  • 该进程成为一个新进程组的组长进程。新进程组ID是该调用进程的进程ID。
  • 该进程没有控制终端。如果调用setsid之前该进程有一个控制终端,那么这种联系也被切断。

如果该调用进程已经是一个进程组的组长,则此函数返回出错。为了保证不处于这种情况,通常先调用fork,然后使其父进程终止,而子进程则继续。因为子进程继承了父进程的进程组ID,而其进程ID是重新分配的,两者不可能相等,这就保证了子进程不是一个进程组的组长。

C 复制代码
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);
	}
}
相关推荐
Hard but lovely4 分钟前
linux:----进程守护化(Daemon)&&会话的原理
linux·运维·服务器
捷米研发三部23 分钟前
Profinet转ModbusTCP网关:实现西门子1200PLC与打标卡稳定通讯
网络
芝麻馅汤圆儿33 分钟前
sockperf 工具
linux·服务器·数据库
YJlio35 分钟前
桌面工具学习笔记(11.1):BgInfo——给服务器桌面“刻”上关键信息
服务器·笔记·学习
IDC02_FEIYA38 分钟前
Windows 服务器任务管理器用户客户端名怎么修改?Windows 10修改电脑设备名称
运维·服务器·windows
Trouvaille ~40 分钟前
【Linux】虚拟内存揭秘:地址空间的魔法
linux·运维·服务器·系统·入门·虚拟内存·进程地址空间
木子欢儿1 小时前
Debian 13安装rime中文输入法
linux·运维·服务器·debian
Trouvaille ~1 小时前
【Linux】进程等待与资源回收:父进程的责任
linux·运维·服务器·进程等待·进程退出·非阻塞与阻塞·资源回收
木子欢儿1 小时前
Ubuntu24.04 安装rime中文输入法
linux·运维·服务器
gf13211111 小时前
python_基于主视频删减片段并插入镜头视频
linux·python·音视频