【Linux学习笔记35】教你写精灵进程(附精灵进程模板)

本文详细解析了守护进程(Daemon)的创建过程,包括如何通过代码实现进程的后台运行,防止因终端关闭或用户切换导致进程终止。介绍了通过fork、setsid、setpgrp等系统调用实现守护进程的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言

精灵进程又称守护进程。进程负责程序的基本服务,但是我们也希望这种服务能够在后台持续的进程,不会因为用户的切换或者登录终端的关闭而停止。阅读本篇笔记应该补充的预备知识: 【Linux学习笔记35(补充)】精灵进程的预备知识

精灵进程代码的步骤解剖(完整代码在最下面)

在这里插入图片描述

  1. 忽略信号,防止进程因为终端关闭而被控制终端杀死:signal(SIGHUP, SIG_IGH);
  2. 产生一个子进程,然后父进程立刻退出:
a = fork();
if (a >0) exit(0);
  1. 创建一个新会话,使得子进程在没有控制终端的新会话中运行:
setsid();
  1. 继续创建一个新的子进程,以确保守护进程无法被控制终端打开
a = fork();
if (a >0) exit(0);
  1. 将守护进程从原始进程组中分离,防止接收任何信号
setpgrp();
  1. 关闭任何文件描述符以释放资源
max_fd = sysconf(_SC_OPEN_MAX);
	for(i=0; i<max_fd; i++)
		close(i);
  1. 将文件权限掩码清除为零
	umask(0);
  1. 修改当前进程的工作路径,确保不被卸载,这里设置为根。必须为根目录!(因为如果为其他路径,工作路径一旦卸载,则进程也会消失)
chdir("/");
  1. 来到这一步,该进程就是精灵进程了,以下添加自己需要的代码。

精灵进程的完整代码:

#include "daemon.h"

int main(void)
{
	pid_t a;
	int max_fd, i;

	/*********************************************
	1. ignore the signal SIGHUP, prevent the
	   process from being killed by the shutdown
	   of the present controlling termination
	**********************************************/
	signal(SIGHUP, SIG_IGN);

	/***************************************
	2. generate a child process, to ensure
	   successfully calling setsid()
	****************************************/
	a = fork();
	if(a > 0)
		exit(0);

	/******************************************************
	3. call setsid(), let the first child process running
	   in a new session without a controlling termination
	*******************************************************/
	setsid();

	/*************************************************
	4. generate the second child process, to ensure
	   that the daemon cannot open a terminal file
	   to become its controlling termination
	**************************************************/
	a = fork();
	if(a > 0)
		exit(0);

	/*********************************************************
	5. detach the daemon from its original process group, to
	   prevent any signal sent to it from being delivered
	**********************************************************/
	setpgrp();

	/*************************************************
	6. close any file descriptor to release resource
	**************************************************/
	max_fd = sysconf(_SC_OPEN_MAX);
	for(i=0; i<max_fd; i++)
		close(i);

	/******************************************
	7. clear the file permission mask to zero
	*******************************************/
	umask(0);

	/****************************************
	8. change the process's work directory,
	   to ensure it won't be uninstalled
	*****************************************/
	chdir("/");


	// Congratulations! Now, this process is a DAEMON!
	pause();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值