c - 每个进程的最大线程数-sysconf(_SC_THREAD_THREADS_MAX)失败

标签 c multithreading pthreads posix

我试图找到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/

相关文章:

c - malloc和realloc的关系,当内存中没有所需的空间时如何处理

c++ - 指向 POD 结构的 C/C++ 指针也指向第一个结构成员

java - 随叫随到的 Swing 重新喷漆

c++ - 线程作为类,如何保证子类的数据被销毁?

C 程序没有给出正确的输出

c++ - 头文件编译成目标文件?

c - 如何将参数从 Makefile 传递给程序?

c# - 双重检查字典 "ContainsKey"上的锁定

php - 为 drupal 并行化 php 脚本

c - 线程函数 sum_array() 无法被 main 函数调用