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(¤t->files->count);
}
//就成为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(¤t->files->count);
}