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!
According to my manual for
sysconfon my Mac OSX 10.7.X:Under linux it is different:
So it looks like that
_SC_THREAD_THREADS_MAXis not a valid sysconfig variable on that architecture or maybe there is no limit. Or maybe there is another definition for-1on other architectures. You’ll have to check the manual on each architecture you are trying to get to work.If
_SC_THREAD_THREADS_MAXis not valid then you might want to try processes instead of threads. Maybe there is a max processes setting which also means max threads.