Linux下一个进程可以创建的最大线程数是多少?

如何(如果可能的话)修改这个值?


当前回答

linux上合适的100k线程:

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

来自@Thomas的2018年更新,关于systemd系统:

/etc/systemd/logind.conf: UserTasksMax=100000

其他回答

要检索它:

cat /proc/sys/kernel/threads-max

设置方法:

echo 123456789 | sudo tee -a /proc/sys/kernel/threads-max

123456789 = #线程

Linux对每个进程没有单独的线程限制,只是对系统上进程总数的限制(线程本质上只是在Linux上具有共享地址空间的进程),您可以像这样查看:

cat /proc/sys/kernel/threads-max

默认为内存页数/4。你可以这样增加:

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

对于单个用户可以创建的进程(以及线程)的数量也有限制,有关这些限制的详细信息,请参阅ulimit/getrlimit。

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.

我们可以看到linux中下面文件中定义的最大线程数

猫/proc/sys/kernel/threads-max

(OR)

Sysctl -a | grep threads-max

说LINUX没有每个进程单独的线程限制是错误的。

Linux间接实现了每个进程最大线程数!!

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

因此,可以通过增加总虚拟内存或减小堆栈大小来增加每个进程的线程数。但是,当最大虚拟内存等于交换内存时,过多地减小堆栈大小会导致代码失败,因为堆栈溢出。

检查你的机器:

总虚拟内存:ulimit -v(默认值是无限的,因此您需要增加交换内存来增加它)

总堆栈大小:ulimit -s(默认为8Mb)

命令来增加这些值。

ulimit -s newvalue

ulimit -v newvalue

*将新值替换为您想要放置的限制值。

引用:

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