glibc: daemon

这篇博客详细介绍了Linux中的daemon函数,该函数用于将进程后台化并脱离session和shell。文章提到了调用daemon后,直接使用exit_group(0)可能会导致线程状态判断错误,以及setsid如何使进程创建独立的会话和进程组,确保守护进程不受控制终端影响。还提及了/etc/init.d/functions文件中包含的另一个daemon函数。

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

参考

https://www.man7.org/linux/man-pages/man7/daemon.7.html
https://www.man7.org/linux/man-pages/man3/daemon.3.html

说明

将当前进程运行到后台,脱离当前session、shell。
#include <unistd.h>
int daemon(int nochdir, int noclose);

需要注意的问题

调用者直接退出exit_group(0)

从 daemon-7里可以看到,这个方法会将之前的线程直接exit_group(0) 。这样在判断返回值时其实不是真正的线程的状态。
1035338 08:40:51.880893 exit_group(0) = ?

  1. Call exit() in the first child, so that only the second child (the actual daemon process) stays around. This ensures that the daemon process is re-parented to init/PID 1, as all daemons should be.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mzhan017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值