Linux下一个进程可以创建的最大线程数是多少?
如何(如果可能的话)修改这个值?
Linux下一个进程可以创建的最大线程数是多少?
如何(如果可能的话)修改这个值?
当前回答
对于现在看到这个的人来说,在systemd系统上(在我的情况下,特别是Ubuntu 16.04),还有另一个由cgroup pid强制执行的限制。max参数。
默认设置为12288,可以在/etc/systemd/logind.conf中覆盖
其他建议仍然适用,包括pids_max、threads-max、max_maps_count、ulimits等。
其他回答
说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/
使用nbio 非阻塞i / o 库之类的,如果你需要更多的线程来做I/O调用那个块
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.
使用ulimit检查每个线程的堆栈大小,在我的情况下Redhat Linux 2.6:
ulimit -a
...
stack size (kbytes, -s) 10240
你的每个线程将得到这个数量的内存(10MB)分配给它的堆栈。一个32位的程序,最大地址空间为4GB,最大只有4096MB / 10MB = 409个线程!!减去程序代码,减去堆空间可能会导致观察到的最大值。300个线程。
您应该能够通过在64bit上编译和运行或设置ulimit -s 8192甚至ulimit -s 4096来引发这个问题。但如果这是明智的是另一个讨论…
对于现在看到这个的人来说,在systemd系统上(在我的情况下,特别是Ubuntu 16.04),还有另一个由cgroup pid强制执行的限制。max参数。
默认设置为12288,可以在/etc/systemd/logind.conf中覆盖
其他建议仍然适用,包括pids_max、threads-max、max_maps_count、ulimits等。