_nosay 发表于 2016-03-21 20:58

get_pid() bug ?

问题:当0x8000-300个用户pid都被占用时,再调用get_pid()函数是不是会死循环?

内核版本:linux-2.4.0
源文件:kernel/fork.c
代码:static int get_pid(unsigned long flags)
{
        static int next_safe = PID_MAX;
        struct task_struct *p;

        if (flags & CLONE_PID)
                return current->pid;

        spin_lock(&lastpid_lock);
        if((++last_pid) & 0xffff8000) {
                last_pid = 300;                /* Skip daemons etc. */
                goto inside;
        }
        if(last_pid >= next_safe) {
inside:
                next_safe = PID_MAX;
                read_lock(&tasklist_lock);
        repeat:
                for_each_task(p) {
                        if(p->pid == last_pid        ||
                           p->pgrp == last_pid        ||
                           p->session == last_pid) {
                                if(++last_pid >= next_safe) {
                                        if(last_pid & 0xffff8000)
                                                last_pid = 300;
                                        next_safe = PID_MAX;
                                }
                                goto repeat;
                        }
                        if(p->pid > last_pid && next_safe > p->pid)
                                next_safe = p->pid;
                        if(p->pgrp > last_pid && next_safe > p->pgrp)
                                next_safe = p->pgrp;
                        if(p->session > last_pid && next_safe > p->session)
                                next_safe = p->session;
                }
                read_unlock(&tasklist_lock);
        }
        spin_unlock(&lastpid_lock);

        return last_pid;
}

nswcfd 发表于 2016-03-23 11:08

既然没有特殊的返回值标志错误,那就应该会一直循环吧?否则不就pid冲突了?

_nosay 发表于 2016-03-23 11:45

回复 2# nswcfd

那就会一直在这里循环是吧?        repeat:
                for_each_task(p) {
                        if(p->pid == last_pid        ||
                           p->pgrp == last_pid        ||
                           p->session == last_pid) {
                                if(++last_pid >= next_safe) {
                                        if(last_pid & 0xffff8000)
                                                last_pid = 300;
                                        next_safe = PID_MAX;
                                }
                                goto repeat;
                        }在内核态只能靠自己主动调度,那这样是不是会死机?
页: [1]
查看完整版本: get_pid() bug ?