Linux中每个进程的最大线程数

Linux中每个进程的最大线程数

问题描述:

我写了一个简单的程序来计算一个进程在linux(Centos 5)中可以拥有的最大线程数.这是代码:

I wrote a simple program to calculate the maximum number of threads that a process can have in linux (Centos 5). here is the code:

int main()
{
    pthread_t thrd[400];
    for(int i=0;i<400;i++)
    {
        int err=pthread_create(&thrd[i],NULL,thread,(void*)i);
        if(err!=0)
            cout << "thread creation failed: " << i <<" error code: " << err << endl;
    }
    return 0;
}

void * thread(void* i)
{
    sleep(100);//make the thread still alive
    return 0;
}

我发现线程的最大数量只有300个!?如果我还需要更多呢? 我不得不提到pthread_create返回12作为错误代码.

I figured out that max number for threads is only 300!? What if i need more than that? I have to mention that pthread_create returns 12 as error code.

谢谢

对Linux有线程限制,可以通过将所需的限制写入/proc/sys/kernel/threads-max来修改运行时.默认值是从可用的系统内存中计算得出的.除了该限制之外,还有另一个限制:/proc/sys/vm/max_map_count限制最大的映射段,并且至少最近的内核将对每个线程映射内存.如果您将其提高很多,应该是安全的.

There is a thread limit for linux and it can be modified runtime by writing desired limit to /proc/sys/kernel/threads-max. The default value is computed from the available system memory. In addition to that limit, there's also another limit: /proc/sys/vm/max_map_count which limits the maximum mmapped segments and at least recent kernels will mmap memory per thread. It should be safe to increase that limit a lot if you hit it.

在32位操作系统中,您遇到的限制是缺少虚拟内存.如果您的硬件支持,请安装64位linux,这样就可以了.我可以轻松启动30000个线程,堆栈大小为8MB.该系统具有一个Core 2 Duo + 8 GB系统内存(我同时在使用5 GB的内存),并且正在运行带有内核2.6.32的64位Ubuntu.请注意,必须允许内存过量使用(/proc/sys/vm/overcommit_memory),因为否则系统将至少需要240 GB的可提交内存(实际内存和交换空间的总和).

The limit you're hitting is lack of virtual memory in 32bit operating system. Install a 64 bit linux if your hardware supports it and you'll be fine. I can easily start 30000 threads with a stack size of 8MB. The system has a single Core 2 Duo + 8 GB of system memory (I'm using 5 GB for other stuff in the same time) and it's running 64 bit Ubuntu with kernel 2.6.32. Note that memory overcommit (/proc/sys/vm/overcommit_memory) must be allowed because otherwise system would need at least 240 GB of committable memory (sum of real memory and swap space).

如果您需要大量线程并且不能使用64位系统,则唯一的选择是最小化每个线程的内存使用量以节省虚拟内存.从请求尽可能少的堆栈开始.

If you need lots of threads and cannot use 64 bit system your only choice is to minimize the memory usage per thread to conserve virtual memory. Start with requesting as little stack as you can live with.