LWN.net Logo

The get_pid() function

This is the source of get_pid() from fork.c in the 2.5.36-bk kernel:
static int get_pid(unsigned long flags)
{
	struct task_struct *g, *p;
	int pid;

	if (flags & CLONE_IDLETASK)
		return 0;

	spin_lock(&lastpid_lock);
	if (++last_pid > pid_max) {
		last_pid = 300;		/* Skip daemons etc. */
		goto inside;
	}

	if (last_pid >= next_safe) {
inside:
		next_safe = pid_max;
		read_lock(&tasklist_lock);
	repeat:
		do_each_thread(g, p) {
			if (p->pid == last_pid	||
			   p->pgrp == last_pid	||
			   p->session == last_pid) {
				if (++last_pid >= next_safe) {
					if (last_pid >= pid_max)
						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;
		} while_each_thread(g, p);

		read_unlock(&tasklist_lock);
	}
	pid = last_pid;
	spin_unlock(&lastpid_lock);

	return pid;
}

(Log in to post comments)

Copyright © 2002, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds