do_fork()在哪里为新分配的task_struct定义“prio”字段?

时间:2013-05-01 18:25:46

标签: linux linux-kernel fork scheduling

包括static_prio和policy等其他字段。我知道根据定义,子进程从父进程继承它们,但它在do_fork()的代码中发生了什么?

1 个答案:

答案 0 :(得分:0)

新分配的task_struct的“prio”字段在sched_fork()函数中定义。 sched_fork()函数的调用流程是fork() - > sys_fork() - > do_fork() - > copy_process() - > sched_fork()。

这里的参考是下面的sched_fork()函数。 (内核版本3.7.5)

void sched_fork(struct task_struct *p)
{
    unsigned long flags;
    int cpu = get_cpu();

    __sched_fork(p);
    /*
     * We mark the process as running here. This guarantees that
     * nobody will actually run it, and a signal or other external
     * event cannot wake it up and insert it on the runqueue either.
     */
    p->state = TASK_RUNNING;

    /*
     * Make sure we do not leak PI boosting priority to the child.
     */
    p->prio = current->normal_prio;

    /*
     * Revert to default priority/policy on fork if requested.
     */
    if (unlikely(p->sched_reset_on_fork)) {
            if (task_has_rt_policy(p)) {
                    p->policy = SCHED_NORMAL;
                    p->static_prio = NICE_TO_PRIO(0);
                    p->rt_priority = 0;
            } else if (PRIO_TO_NICE(p->static_prio) < 0)
                    p->static_prio = NICE_TO_PRIO(0);

            p->prio = p->normal_prio = __normal_prio(p);
            set_load_weight(p);

            /*
             * We don't need the reset flag anymore after the fork. It has
             * fulfilled its duty:
             */
            p->sched_reset_on_fork = 0;
    }

    .....
    ......
}