空闲进程和init进程如何安排?

时间:2015-08-12 08:12:09

标签: linux-kernel

此代码来自Linux内核:

内核/ INIT / main.c中

static noinline void __init_refok rest_init(void)
{
    int pid;

    rcu_scheduler_starting();
    /*
     * 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 | CLONE_SIGHAND);
    numa_default_policy();
    pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
    rcu_read_lock();
    kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
    rcu_read_unlock();
    complete(&kthreadd_done);

    /*
     * The boot idle thread must execute schedule()
     * at least once to get things moving:
     */
    init_idle_bootup_task(current);
    schedule_preempt_disabled();
    /* Call into cpu_idle with preempt disabled */
    cpu_startup_entry(CPUHP_ONLINE);
}

我知道从内核启动时,有一个0进程会在内核启动时初始化所有内容,直到这个时候,它运行函数:rest_init

这里:它将创建我们称之为1进程的init进程。

kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);

运行该函数后,应该有两个进程现在为0和1。

问题:

  1. 0和1进程都在同一个cpu(如果有4或8个cpus平台)的同一个线程列表中?如何调度这两个进程?

  2. 如果它们位于同一个cpu中的线程列表中,当0进程调用schedule_preempt_disabled function()时,则表示停止计划。然后0进程在空闲时间输入cpu_startup_entry(),哪个进程将设置need_resched标志以使空闲(0)进程进行调度?我的意思是流程1不会再次运行?

  3. 或者您可以告诉我详细的0和1流程如何在此时安排。

1 个答案:

答案 0 :(得分:0)

process 0致电schedule_preempt_disabled来执行以下操作:

1, sched_preempt_enable_no_resched();   //enable preempt

2, schedule();    //schedule to other process(1-init or 2-kthreadd_task)

3, preempt_disable();  //when all processes give up cpu, 
     //scheduler pick the 0-idle to run again; 
     //0-idle disable preemt and run into cpu_idle_loop;