LWN.net Logo

Solving the process ID allocation problem

Ingo Molnar, in his project to give Linux "world-class threading support," has set his sights on another Linux performance problem: the allocation of process ID (PID) numbers for new processes. This does not seem like it should be a difficult problem, but the current kernel get_pid() shows quadratic behavior when the number of processes gets large. Essentially, the algorithm looks like this:

    for each possible PID
        for each task in the system
	    if task_pid == pid
	        keep_trying

The above is an oversimplification, since the get_pid() code tries to find a range of usable PIDs, not just one. Look here for the real get_pid() implementation. The point is that, with very large numbers of processes (i.e. on the order of 100,000), process ID allocation can lock up the system for long periods of time.

Ingo's solution starts with some work done by William Lee Irwin. William's "idtag" infrastructure adds hash tables for managing things with numeric ID tags; it is used in this patch to manage PID-related things like process groups and session IDs. The idtags help to eliminate many iterations over the whole process space done in the kernel, but do not solve the PID allocation problem.

Ingo handles PID allocation through a new allocator that he wrote from scratch. This allocator maintains an array of pages (allocated as needed) which are used as PID bitmaps; allocating a new PID becomes a matter of finding a page with a free PID available, then finding and clearing the first set bit. It all happens with no locking required. Ingo claims:

Ie. even in the most hopeless situation, if there are 999,999 PIDs allocated already, it takes less than 10 usecs to find and allocate the remaining one PID. The common fastpath is a couple of instructions only.

So it's fast - though a few extra features have been requested. But this patch has stirred up a bit of a debate. Rather than put in a complicated new PID allocator, it is asked, why not just make the maximum PID be very large? Then, in theory, the quadratic part of get_pid() will never run so the performance problems go away, and the code stays simpler. Linus prefers this approach, as do a number of other developers; he has put a simple patch along these lines into his pre-2.5.37 BitKeeper tree.

Ingo disagrees, pointing out that any reasonable maximum PID size can be exceeded eventually. He would rather fix the problem than try to hid it behind a large process ID space. In the absence of real-world examples that show people being bitten by get_pid()'s behavior in a larger PID space, though, Linus appears unlikely to accept any more complicated fix.


(Log in to post comments)

Solving the process ID allocation problem

Posted Sep 19, 2002 16:58 UTC (Thu) by cpeterso (subscriber, #305) [Link]

Why is this so complicated? For each running process, there is probably a struct allocated for each process. Why not just use the memory address of the process struct as a pid? Separate processes would be guarenteed to have unique addresses and thus unique pids without any locking or scanning any big tables. Casting the process struct address to an int is super fast! :-)


(btw, the post a reply for this particular article seems to be broken. I have tried logging in with IE6, Mozilla 1.1, Mozilla 1.0, and Netscape 6.2. When I to reply to this article, LWN says I need to login first. That doesn't seem happen when replying to the other articles on this page.)

Solving the process ID allocation problem

Posted Sep 21, 2002 7:13 UTC (Sat) by steveha (guest, #3876) [Link]

Why not just use the memory address of the process struct as a pid?

Short answer: because Linus Torvalds would rather type "kill 1234" than "kill 0xFE3A07C1".

Slightly longer answer: there are a number of times when you might care about the process number. As things are, the number won't be too long most of the time. With addresses for PIDs, you will have longer and uglier numbers to keep track of or type in. (And it could get very ugly on a 64-bit architecture!) And I for one like the fact that two processes started at about the same time will have PID numbers very close to each other.

Solving the process ID allocation problem

Posted Sep 25, 2002 13:51 UTC (Wed) by oneukum (subscriber, #3970) [Link]

The PID is an argument of some system calls. The kernel cannot for reasons of security,
accept a pointer into kernel memory which user spaces feeds it.
PIDs must be in some kind of table, so that they can be verfied.

Solving the process ID allocation problem

Posted Sep 29, 2002 17:40 UTC (Sun) by GreyWizard (subscriber, #1026) [Link]

The kernel cannot for reasons of security, accept a pointer into kernel memory which user spaces feeds it.

Well, sure. But that's not what was suggested. Just because a valid process identifier is identical to the memory address of a process struct doesn't mean the kernel can't look it up values passed in from user space in a hash table before acting on them. The code that creates processes would cast the pointer and the rest of the system would remain unchanged.

(I agree this makes a PID hard to type, especially on 64 bit systems, but things like job control and terminal cut-and-paste mitigate this somewhat.)

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