3

I'm trying to find the maximum number of threads per process on a UNIX machine and wrote the code below to use 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);
}

Unfortunately the sysconf() returns -1 without changing the errno! Does anyone know how to get around this problem and eventually what is the maximum number of Pthreads per process? Thanks

P.S. I tried it on Solaris and Linux and got the same result. However HPUX did return 8000!

Reza Toghraee
  • 1,603
  • 1
  • 14
  • 21

2 Answers2

2

According to my manual for sysconf on my Mac OSX 10.7.X:

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.

Under linux it is different:

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.

So it looks like that _SC_THREAD_THREADS_MAX is not a valid sysconfig variable on that architecture or maybe there is no limit. Or maybe there is another definition for -1 on other architectures. You'll have to check the manual on each architecture you are trying to get to work.

If _SC_THREAD_THREADS_MAX is not valid then you might want to try processes instead of threads. Maybe there is a max processes setting which also means max threads.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • well I actually did try this on my mac os x 10.6 as well. I do see that there is no such a macro as PTHREAD_THREAD_MAX, but the _SC_THREAD_THREADS_MAX is defined in unistd.h. Moreover, should there be a hard limit on the total number of threads+processes that the kernel could handle? Thanks for the response though – Reza Toghraee Mar 05 '12 at 23:34
  • I was just trying to show you that without knowing which OS you were on, there were a couple of different reasons why you'd be getting -1. Thanks for the accept. – Gray Mar 05 '12 at 23:36
  • Should there be a hard limit? Sounds like an OS dependent answer. I would think that there would be but I've been surprised before about how dynamic modern operating systems are. – Gray Mar 05 '12 at 23:37
  • One more comment @Reza. Just because it is listed in the `unistd.h` file does not mean that the local OS supports it. Some of the settings may be a standard to have defined for portability but not supported by the local kernel variant. – Gray Mar 07 '12 at 13:29
2

If there is no thread limit, the function returns -1.

The POSIX standard refers to PTHREAD_THREAD_MAX as the system-imposed limit on the total number of threads in a process. On RHEL4 and SLES9, this macro is actually defined for the LinuxThreads. Starting from SLES 10, LinuxThreads is no longer supported. The new implementation of pthread on Linux, Native POSIX Thread Library (NPTL), does not have a limit on the number threads that can be created except for actual resource availability. Therefore, the PTHREAD_THREAD_MAX macro is no longer defined in the SLES 10 system header files.

info +info