daemonize

本文详细解析了Linux 2.4.20内核中daemonize函数的实现原理,该函数负责将进程转变为内核级守护进程。通过释放用户空间资源、设置进程属性为init会话的一部分等方式,确保守护进程稳定运行。

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

linux-2.4.20/kernel/sched.c start from line 1283:

/*
* Put all the gunge required to become a kernel thread without
* attached user resources in one place where it belongs.
*/

void daemonize(void)
{
struct fs_struct *fs;


/*
    * If we were started as result of loading a module, close all of the
    * user space pages.   We don't need them, and if we didn't close them
    * they would be locked into memory.
    */
exit_mm(current); //因为是内核级daemon,所以不会再需要用户级的内存页面,释放之。这些页面是因为加载模块而申请到的。

        current->session = 1;
current->pgrp = 1;
current->tty = NULL;

 

 

//因为是内核级daemon,所以不再需要终端控制台,所属会话为init会话,parent group为1(init),这样
//就成为init任务的一部分了。下面的代码则是释放文件系统相关资源,并且转为指向init任务的文件资源。
        /* Become as one with the init task */

exit_fs(current); /* current->fs->count--; */
fs = init_task.fs;
current->fs = fs;
atomic_inc(&fs->count);
exit_files(current);
current->files = init_task.files;
atomic_inc(&current->files->count);
}      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值