【发布时间】:2012-03-05 19:26:09
【问题描述】:
我正在尝试查找 UNIX 机器上每个进程的最大线程数,并编写了以下代码以使用 sysconf:
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
errno = 0;
long maxThreads = sysconf(_SC_THREAD_THREADS_MAX);
if (maxThreads == -1 && errno == 0)
{
printf("the variable corresponding to _SC_THREAD_THREADS_MAX "
"is associated with functionality that is not "
"supported by the system\n");
exit(1);
}
if (maxThreads == -1)
{
printf("errno: %d\n", errno);
exit(1);
}
printf ("max num threads per process: %ld\n", maxThreads);
exit(0);
}
不幸的是 sysconf() 返回 -1 而没有改变 errno!有谁知道如何解决这个问题,最终每个进程的最大 Pthread 数是多少? 谢谢
附:我在 Solaris 和 Linux 上进行了尝试,得到了相同的结果。但是 HPUX 确实返回了 8000!
【问题讨论】:
标签: c multithreading pthreads posix