我试图找到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!有谁知道如何解决这个问题,以及每个进程的最大Pthreads数量是多少?
谢谢
P.S.我在Solaris和Linux上进行了尝试,并得到了相同的结果。但是,HPUX确实返回了8000!
最佳答案
根据我在Mac OSX 10.7.X上的sysconf
手册:
If the call to sysconf() is not successful, -1 is returned and errno is set appropriately. Otherwise, if the variable is associated with functionality that is not supported, -1 is returned and errno is not modified. Otherwise, the current variable value is returned.
在linux下是不同的:
If name is invalid, -1 is returned, and errno is set to EINVAL. Otherwise, the value returned is the value of the system resource and errno is not changed. In the case of options, a positive value is returned if a queried option is available, and -1 if it is not. In the case of limits, -1 means that there is no definite limit.
因此,似乎
_SC_THREAD_THREADS_MAX
在该体系结构上不是有效的sysconfig变量,或者可能没有限制。也许在其他架构上还有-1
的另一种定义。您必须查看要尝试使用的每种体系结构的手册。如果
_SC_THREAD_THREADS_MAX
无效,那么您可能要尝试使用进程而不是线程。也许有一个最大进程设置,这也意味着最大线程数。
关于c - 每个进程的最大线程数-sysconf(_SC_THREAD_THREADS_MAX)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9572647/