【问题标题】:How does the idle process and init process schedule?idle进程和init进程如何调度?
【发布时间】:2015-08-12 08:12:09
【问题描述】:

这段代码来自Linux内核:

kernel/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 cpu平台的话)?两个进程是如何调度的?

  2. 如果它们在同一个cpu的一个线程列表中,当0个进程调用schedule_preempt_disabledfunction()时,表示停止调度。那么0进程在空闲时间进入cpu_startup_entry(),哪个进程会设置need_resched标志让idle(0)进程调度呢?我的意思是进程1不会再次运行?

  3. 或者你可以告诉我详细的0和1过程此时如何调度。

【问题讨论】:

    标签: linux-kernel


    【解决方案1】:

    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;
    

    【讨论】:

    • 我明白你的意思,看来这段代码必须在非单cpu平台上运行?对吧?
    • 你为什么这么说?它可以在signle cpu或SMP上运行。
    猜你喜欢
    • 1970-01-01
    • 2012-04-23
    • 2012-01-17
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多