我的c程序无法创建超过8 threads
的代码。它返回错误代码EAGAIN(11)
。这是因为缺乏可用资源。在发布这个问题之前,我用谷歌搜索了它的解决方案,但并没有从中获得太多帮助。这是我为程序和UNIX系统找到的详细信息。
我的线程创建函数是:
thread_initialise(File *CFG_FILE)
{
int total_pthreads; //reads number of threads I want for the program from configuration file.
int rc =0 ;
for (i = 0; i < total_pthreads; i++)
{
rc = pthread_create (&pthread_list[i], NULL, (fp)(begin_worker_pthread), NULL);
if (rc !=0) printf("Thread creation Error Code: %d",rc);
}
}
我的程序在执行时消耗的内存为:
pmap -x <pid> = 1111844
Unix版本:
uname -a = Linux 2.6.18-308.24.1.el5 #1 SMP Wed Nov 21 11:42:14 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
Unix
cat /proc/sys/kernel/threads-max = 81920
中的线程最大值ulimit -u max user processes (-u) 16000
ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 40960
max locked memory (kbytes, -l) 3000000
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 5857280
real-time priority (-r) 0
stack size (kbytes, -s) 512000
cpu time (seconds, -t) unlimited
max user processes (-u) 16000
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
请帮助我的系统如何计算/固定最大线程数。我想将线程数增加到
32
。 最佳答案
从终端设置ulimit -s 4000
。现在您可以运行比以前更多的线程,但是在某些阶段您会遇到段错误。
线程数=总虚拟内存/(堆栈大小* 1024 * 1024)
更多信息,请参见this明确解释的内容。
关于c - UNIX:程序中的最大线程数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18464041/