This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Linux/EVMK2H: Max thread limit per process

Part Number: EVMK2H

Tool/software: Linux

Hello,

We have a several EVMK2H REV4.0 boards and have set them up using the Processor SDK Linux for K2H/K2K v03.02.00.05, with 8GB memory DIMM.

We are booting up the board using the tftpserver/NFS Server approach, and the host is a Ubuntu 16.04 PC.

Our application that will run on the ARM processor is expected to run under a single root linux thread that may be required to create up to 1000 threads.  During some initial test we have discovered that we are not able to create 1000 threads.  Initially we were able to create 380 threads and were able to determine this limit was due to the default stack size per thread of 8MB.  But even after changing the default stack size to a very small amount, 16k, the maximum number of threads we can create is 509.

Does anyone know if there is another resource that is preventing creation of more than 509 threads.

Any suggestions will be appreciated.

Regards,

Bob


-- Below is the source of the test program we are using followed by sample output.


//=====================================
//============= Test program threadTest
//=====================================
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <errno.h>

#define NUM_THREADS 1000
void * aThread(void* parm) {
  sleep(5);
  return NULL;
}

int main4() {
  int i, err;
  int nCreated=0;
  size_t stacksize;
  pthread_attr_t attr;
 
  pthread_t thrd[NUM_THREADS];
 
  printf("Trying to create %d threads\n", NUM_THREADS);

  for(i=0;i<NUM_THREADS;i++) {
    err = pthread_create(&thrd[i], NULL, aThread, NULL);
    if (err) {
      printf("  pthread_create() failed creating thread %d, error %d\n", i+1, err);
      errno = err;
      perror("  Error code is ");
      break;
    } else {
      if (nCreated == 0) {
        if (pthread_getattr_np(thrd[i], &attr) == 0) {
          pthread_attr_getstacksize(&attr, &stacksize);
          printf("Thread stack size = %d bytes \n", stacksize);
        }
      }
      nCreated++;
    }
  }
 
  printf("Joining %d threads...\n", i);
  for (i=1; i<nCreated; i++) {
    pthread_join(thrd[i], NULL);
  }
 
  return 0;
}

//=====================================
//======= Sample output - default stacksize of 8MB
//=====================================

root@k2hk-evm:~/testing# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 60560
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 60560
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

root@k2hk-evm:~/testing# ./threadTest

Trying to create 1000 threads
Thread stack size = 8388608 bytes
  pthread_create() failed creating thread 381, error 11
  Error code is : Resource temporarily unavailable
Joining 380 threads...


//================================================
//======= Sample output - stacksize changed to 16K
//================================================

root@k2hk-evm:~/testing# ulimit -s 16

root@k2hk-evm:~/testing# ./threadTest
 
Trying to create 1000 threads
Thread stack size = 16384 bytes
  pthread_create() failed creating thread 510, error 11
  Error code is : Resource temporarily unavailable
Joining 509 threads...

//=======================
//==== Memory info ======
//=======================

root@k2hk-evm:~/testing# grep MemTotal /proc/meminfo
MemTotal:  7784860 kB

root@k2hk-evm:~/testing# free -m
         total     used      free     shared     buff/cache     available
Mem:      7602       34      7502         17             65          7506
Swap:        0        0         0