views:

211

answers:

4

How many threads a single process can handle in Linux(RHEL-5)? Once threads are created how much stack an each thread can get ?

A: 

Thread stack size is configurable, using pthread_attr_setstack method. Amount of thread is imho limited only by resources you have, more than 2K threads work in an application i know.

Drakosha
+2  A: 

Maximum number of threads: http://stackoverflow.com/questions/344203/maximum-number-of-threads-per-process-in-linux

Stack size:

Even if pthread_attr_setstacksize() and pthread_attr_setstackaddr() are now provided, we still recommend that you do not use them unless you really have strong reasons for doing so. The default stack allocation strategy for LinuxThreads is nearly optimal: stacks start small (4k) and automatically grow on demand to a fairly large limit (2M). Moreover, there is no portable way to estimate the stack requirements of a thread, so setting the stack size yourself makes your program less reliable and non-portable.

(from http://pauillac.inria.fr/~xleroy/linuxthreads/faq.html)

Aaron Digulla
LinuxThreads hasn't shipped with any Linux distribution for almost 7 years now. In particular, RHEL hasn't shipped LinuxThreads since RHEL3.
Jörg W Mittag
@Jörg. Agreed. I couldn't find any better docs. Do you have a link?
Aaron Digulla
+1  A: 

There is not a maximum number of threads by process.

There is however limit of the total active thread. This value can be retrieved by typing :

cat /proc/sys/kernel/threads-max

you can also change this value :

echo 99999 > /proc/sys/kernel/threads-max

Hope this helps.

Snowangelic
+1  A: 

If you're on a 32-bit machine, then the thread stacks will consume the address space eventually, depending on the size, probably at < 10,000 threads.

10k threads is certainly feasible and some people do run production servers with that many, but you really want to be sure that's the best way of doing what you're doing.

If you're thinking of having 10k threads, you probably have 64-bit machines anyway, and lots and lots of ram.

MarkR
Default thread stack size on x86 is 4KiBytes. With a 4G/4G split kernel configuration, you could theoretically start 2**20 threads (more than 1 million). When NPTL was first announced, the authors actually ran that test, and they maxed out just below 900000. You can easily start 100000 threads (takes about 2 seconds) and the system will still be usable (unlike with LinuxThreads, where starting 100000 threads takes more than 15 minutes and brings the system to a screeching halt).
Jörg W Mittag
I was working on the assumption that most applications would need a lot more than 4k stack.
MarkR
That's incorrect, Jörg. On i686, glibc will reserve 10M per stack of address space for every thread. Obviously none of that memory is allocated, but it consumes a big chunk of the available mapping space and you will exhaust that after only a few hundred threads. Starting 100k threads requires tuning the stack size parameter. I suspect you were thinking about the kernel only, not the glibc implementation of pthreads.
Andy Ross
I would imagine you'd need a minimum of 2 pages per thread, one for the stack and one "reserved" page to catch stack overflows. If there is, say, 2G of address space available for stacks and 4k pages, then you're limited to about 256k threads (which is a lot). I'm assuming your app can run with a 4k stack, which is pretty tight.
MarkR
If you're not doing special handling for stack overflows, you'll just crash anyway. So a correct program that can run in 4k of stack doesn't "need" 8k, really. Designing programs to run in 4k of stack, though, is non-trivial. Be prepared to spend several lifetimes auditing your auxilliary libraries...
Andy Ross