1

My c progam is not able to create more than 8 threads. It returns the error code EAGAIN(11). Which is for lack of resources available. Before posting this question I googled for its solution but could not get much out of that. Here are the detail I have found for my program and unix system.

My thread creation functions is :-

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);
        }
}

Memory consumed by my program while execution is: pmap -x <pid> = 1111844

Unix Version: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

Thread Max value in 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

Please help how the maximum number of threads is calculated/ fixed by my system. I want to increase my threads to 32.

4

2 回答 2

2

ulimit -s 4000从终端设置。现在你可以运行比以前更多的线程,但是你会在某个阶段遇到分段错误。

线程数=总虚拟内存/(堆栈大小*1024*1024)

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

更多信息请参阅帖子清楚地解释。

于 2013-08-27T12:31:44.117 回答
1

毕竟,我为我的系统找到的所有上述研发就是Maximum number of threads for program = Memory Size(RAM) / Stack Size. 这个计算适用于我的系统。

尽管我将虚拟内存设置为无限制,但我的线程数不能超过上述计算。

在一个单独的问题中,我要求程序的最小堆栈大小,以便程序永远不会失败。这是链接:UNIX:UNIX 中的堆栈大小(ulimit -s)应该是多少?

于 2013-08-28T18:44:01.613 回答