Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of threads per process - sysconf(_SC_THREAD_THREADS_MAX) failing

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!

like image 855
Reza Toghraee Avatar asked Sep 14 '25 06:09

Reza Toghraee


1 Answers

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

like image 77
Jon Ander Ortiz Durántez Avatar answered Sep 15 '25 22:09

Jon Ander Ortiz Durántez