Maximum number of threads per process in Linux?

前端 未结 16 2822
轮回少年
轮回少年 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:41

    To set permanently,

    vim /etc/sysctl.conf
    

    and add

    kernel.threads-max = "value"
    
    0 讨论(0)
  • 2020-11-22 02:43

    proper 100k threads on linux:

    ulimit -s  256
    ulimit -i  120000
    echo 120000 > /proc/sys/kernel/threads-max
    echo 600000 > /proc/sys/vm/max_map_count
    echo 200000 > /proc/sys/kernel/pid_max 
    
     ./100k-pthread-create-app
    

    2018 update from @Thomas, on systemd systems:

    /etc/systemd/logind.conf: UserTasksMax=100000
    
    0 讨论(0)
  • 2020-11-22 02:43

    You can see the current value by the following command- cat /proc/sys/kernel/threads-max

    You can also set the value like

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

    The value you set would be checked against the available RAM pages. If the thread structures occupies more than 1/8th) of the available RAM pages, thread-max would be reduced accordingly.

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

    For anyone looking at this now, on systemd systems (in my case, specifically Ubuntu 16.04) there is another limit enforced by the cgroup pids.max parameter.

    This is set to 12,288 by default, and can be overriden in /etc/systemd/logind.conf

    Other advice still applies including pids_max, threads-max, max_maps_count, ulimits, etc.

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

    Yes, to increase the threads number you need to increase the virtual memory or decrease the stack size. In Raspberry Pi I didn’t find a way to increase the virtual memory, if a decrease the stack size from default 8MB to 1MB It is possibly get more than 1000 threads per process but decrease the stack size with the “ulimit -s” command make this for all threads. So, my solution was use “pthread_t” instance “thread class” because the pthread_t let me set the stack size per each thread. Finally, I am available to archive more than 1000 threads per process in Raspberry Pi each one with 1MB of stack.

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

    This is WRONG to say that LINUX doesn't have a separate threads per process limit.

    Linux implements max number of threads per process indirectly!!

    number of threads = total virtual memory / (stack size*1024*1024)
    

    Thus, the number of threads per process can be increased by increasing total virtual memory or by decreasing stack size. But, decreasing stack size too much can lead to code failure due to stack overflow while max virtual memory is equals to the swap memory.

    Check you machine:

    Total Virtual Memory: ulimit -v (default is unlimited, thus you need to increase swap memory to increase this)

    Total Stack Size: ulimit -s (default is 8Mb)

    Command to increase these values:

    ulimit -s newvalue
    
    ulimit -v newvalue
    

    *Replace new value with the value you want to put as limit.

    References:

    http://dustycodes.wordpress.com/2012/02/09/increasing-number-of-threads-per-process/

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