title: Segmentation fault for pthreads in a recursive call
body: <p>Given the code below, I get a segmentation fault if I run it with n>16. </p>
  
  <p>I think it has something to do with the stack, but I can't figure it out. Could anyone give me a hand? The code is not mine, and really not important. I would just like someone to give me a hand with what is happening. <a href="https://stackoverflow.com/questions/5086040/recursive-fib-with-threads-segmentation-fault">This SO question</a> is very similar, but there's not enough information (the person who posts the answer briefly talks about the problem, but then goes on to talk about a different language). Besides, notice that with two gigs and no recursion, I can (if I'm doing it right) successfully create more than 16000 threads (though the OS only creates about 500 and runs about 300). Anyway, where am I getting the seg fault here and why? Thanks.</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  
  static void* fibonacci_thread( void* arg ) {
  int n = (int)arg, fib;
  
  pthread_t th1, th2;
  
  void* pvalue; /*Holds the value*/
  
  switch (n) {
  case 0:  return (void*)0;
  case 1:  /* Fallthru, Fib(1)=Fib(2)=1 */
  case 2:  return (void*)1;
  default: break;
  }
  
  pthread_create(&amp;th1, NULL, fibonacci_thread, (void*)(n-1));
  
  pthread_create( &amp;th2, NULL, fibonacci_thread, (void*)(n-2));
  
  pthread_join(th1, &amp;pvalue);
  
  fib = (int)pvalue;
  
  pthread_join(th2, &amp;pvalue);
  
  fib += (int)pvalue;
  
  return (void*)fib;
  }
  
  int main(int argc, char *argv[])
  {
  int n=15;
  printf ("%d\n",(int)fibonacci_thread((void*)n));
  return 0;
  }
  </code></pre>