Maximum number of threads per process in Linux?

前端 未结 16 2802
轮回少年
轮回少年 2020-11-22 02:16

What is the maximum number of threads that can be created by a process under Linux?

How (if possible) can this value be modified?

相关标签:
16条回答
  • 2020-11-22 02:49

    It probably shouldn't matter. You are going to get much better performance designing your algorithm to use a fixed number of threads (eg, 4 or 8 if you have 4 or 8 processors). You can do this with work queues, asynchronous IO, or something like libevent.

    0 讨论(0)
  • 2020-11-22 02:53

    Linux doesn't have a separate threads per process limit, just a limit on the total number of processes on the system (threads are essentially just processes with a shared address space on Linux) which you can view like this:

    cat /proc/sys/kernel/threads-max
    

    The default is the number of memory pages/4. You can increase this like:

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

    There is also a limit on the number of processes (and hence threads) that a single user may create, see ulimit/getrlimit for details regarding these limits.

    0 讨论(0)
  • 2020-11-22 02:54

    Depends on your system, just write a sample program [ by creating processes in a loop ] and check using ps axo pid,ppid,rss,vsz,nlwp,cmd. When it can no more create threads check nlwp count [ nlwp is the number threads ] voila you got your fool proof answer instead of going thru books

    0 讨论(0)
  • 2020-11-22 02:56

    check the stack size per thread with ulimit, in my case Redhat Linux 2.6:

        ulimit -a
    ...
        stack size              (kbytes, -s) 10240
    

    Each of your threads will get this amount of memory (10MB) assigned for it's stack. With a 32bit program and a maximum address space of 4GB, that is a maximum of only 4096MB / 10MB = 409 threads !!! Minus program code, minus heap-space will probably lead to an observed max. of 300 threads.

    You should be able to raise this by compiling and running on 64bit or setting ulimit -s 8192 or even ulimit -s 4096. But if this is advisable is another discussion...

    0 讨论(0)
提交回复
热议问题