为什么kthread有pid 2而systemd有PID 1?

时间:2015-09-03 13:41:00

标签: linux kernel init systemd

在systemd环境中,当我执行 ps -auxf 时,我看到kthread的PID为2,而systemd已分配PID 1。

那么,谁将PID 2分配给kthread,为什么当kthread被称为 systemd 时它会获得PID 2?

1 个答案:

答案 0 :(得分:6)

我不认为kthreadd正在开始init(在您的情况下符号链接到systemd)。

init由内核初始化启动。 kthreadd刚刚开始。请参阅此kernel threads wikipage,对于Linux 4.2,其文件init/main.c,函数rest_init,在第397行附近,您可以看到:

/*
 * We need to spawn init first so that it obtains pid 1, however
 * the init task will end up wanting to create kthreads, which, if
 * we schedule it before we create kthreadd, will OOPS.
 */
kernel_thread(kernel_init, NULL, CLONE_FS);
numa_default_policy();
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);

所以kthreadd init开始,但两者都是在调度之前在内核中启动的,所以在开始执行之前。 < / p>

静态kernel_init函数(init/main.c的930 - 975行)显着:

if (execute_command) {
    ret = run_init_process(execute_command);
    if (!ret)
        return 0;
    panic("Requested init %s failed (error %d).",
          execute_command, ret);
}
if (!try_to_run_init_process("/sbin/init") ||
    !try_to_run_init_process("/etc/init") ||
    !try_to_run_init_process("/bin/init") ||
    !try_to_run_init_process("/bin/sh"))
    return 0;

所以设置 init 进程(àlaexecve(2) ....)并且已经硬连线/sbin/init等......

并且init process已经有pid 1多年了(自1970年代的原始Unix以及没有内核线程的旧Linux 1.x内核)。这是一个强大的Unix惯例(可能很多软件都依赖于它)。您可以使用systemd作为 init 流程,但也可以使用sysvinit或仅使用bash(将init=/bin/bash传递给$filter有时很有用内核通过GRUB用于修复目的)或其他东西(例如runit