0

这包括其他字段,如 static_prio 和策略。我知道根据定义,子进程从父亲那里继承它们,但是它在 do_fork() 的代码中发生在哪里?

4

1 回答 1

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;
    }

    .....
    ......
}
于 2013-05-31T11:00:11.543 回答