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

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


当前回答

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.

其他回答

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.

线程数限制:

$ cat /proc/sys/kernel/threads-max 

计算方法:

max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);

和: x86_64页大小(PAGE_SIZE)为4K; 像所有其他体系结构一样,x86_64为每个活动线程都有一个内核堆栈。这些线程栈是THREAD_SIZE (2*PAGE_SIZE)大;

备忘录:

cat /proc/zoneinfo | grep spanned | awk '{totalpages=totalpages+$2} END {print totalpages}';

因此,实际上这个数字与线程内存堆栈大小的限制(ulimit -s)无关。

注:线程内存堆栈限制是10M在我的rhel虚拟机,1.5G内存,这个虚拟机只能提供150个线程?

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

cat /proc/sys/kernel/threads-max

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

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

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

如果使用Suse操作系统,则需要选择其中一种方法

https://www.suse.com/support/kb/doc/?id=000015901

全球性的,

/etc/systemd/system.conf
    DefaultTasksMax=Value

对于特定的ssh服务

/etc/systemd/system/sshd.service.d/override.conf
    TasksMax=Value

要检索它:

cat /proc/sys/kernel/threads-max

设置方法:

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

123456789 = #线程