title: UNIX: Maximum number of threads in a program
body: <p>My c progam is not able to create more than <code>8 threads</code>. It returns the error code <code>EAGAIN(11)</code>. Which is for lack of resources available. Before posting this question I googled for its solution but could not get much out of that. Here are the detail I have found for my program and unix system.</p>
  
  <p>My thread creation functions is :-</p>
  
  <pre><code>thread_initialise(File *CFG_FILE)
  {
          int total_pthreads; //reads number of threads I want for the program from configuration file.
          int rc =0 ;
          for (i = 0; i &lt; total_pthreads; i++) 
          {
              rc = pthread_create (&amp;pthread_list[i], NULL, (fp)(begin_worker_pthread), NULL);
              if (rc !=0) printf("Thread creation Error Code: %d",rc);
          }
  }
  </code></pre>
  
  <p>Memory consumed by my program while execution is: <code>pmap -x &lt;pid&gt; = 1111844</code></p>
  
  <p>Unix Version:<code>uname -a = Linux 2.6.18-308.24.1.el5 #1 SMP Wed Nov 21 11:42:14 EST 2012 x86_64 x86_64 x86_64 GNU/Linux</code></p>
  
  <p>Thread Max value in unix <code>cat /proc/sys/kernel/threads-max = 81920</code></p>
  
  <p><code>ulimit -u max user processes              (-u) 16000</code></p>
  
  <pre><code>ulimit -a
  core file size          (blocks, -c) unlimited
  data seg size           (kbytes, -d) unlimited
  scheduling priority             (-e) 0
  file size               (blocks, -f) unlimited
  pending signals                 (-i) 40960
  max locked memory       (kbytes, -l) 3000000
  max memory size         (kbytes, -m) unlimited
  open files                      (-n) 1024
  pipe size            (512 bytes, -p) 8
  POSIX message queues     (bytes, -q) 5857280
  real-time priority              (-r) 0
  stack size              (kbytes, -s) 512000
  cpu time               (seconds, -t) unlimited
  max user processes              (-u) 16000
  virtual memory          (kbytes, -v) unlimited
  file locks                      (-x) unlimited
  </code></pre>
  
  <p>Please help how the maximum number of threads is calculated/ fixed by my system. I want to increase my threads to <code>32</code>.</p>