title: The Fibonacci sequence using threads in C
body: <p>I wrote a program in C to generate a Fibonacci sequence with n numbers where each  Fibonacci number created by a separate thread, the parent thread outputs whole produced Fibonacci sequence yet I got wrong sequence for n>2 it some how rewrite the value of the last element  in the Fibonacci sequence array to 0 if n>2 .How i can fix it? please find the code below.</p>
  
  <pre><code>/*============================================================================
     Description :The Fibonacci sequence
    ============================================================================ */
  #include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int n;                          // size of fibonacci sequence.
  int *fibseq;                    // arry holds the value of each fibonacci term.
  int i;                          // counter for the threads.
  
  void *runn(void *arg);
  
  int main(int argc, char *argv[])
  {
      if (argc != 2)
      {
          printf("format is:./a.out &lt;intgervalue&gt;\n");
          return -1;
      }                           // valdiate num of args.
  
      if (atoi(argv[1]) &lt; 0)
      {
          printf("%d must be&gt;=0\n", atoi(argv[1]));
          return -1;
      }                           // valdiate value of arg1.
  
      n = atoi(argv[1]);
      fibseq = (int *)malloc(n * sizeof(int));
      pthread_t *threads = (pthread_t *) malloc(n * sizeof(pthread_t));
      pthread_attr_t attr;        // set of thread attribute
  
      pthread_attr_init(&amp;attr);
  
      for (i = 0; i &lt; n; i++)
      {
          pthread_create(&amp;threads[i], &amp;attr, runn, NULL);
      }                           // End of creating threads.
  
      int j;
  
      for (j = 0; j &lt; n; j++)
      {
          pthread_join(threads[j], NULL);
      }                           // End of wating the threads to exit.
  
      // printing fibseq.
      printf("The Fibonacci sequence.:");
      int k;
  
      for (k = 0; k &lt; n; k++)
      {
          printf("%d,", fibseq[k]);
      }                           // End of printing fibseq.
      return 0;
  }                               // End of main.
  
  void *runn(void *arg)
  {
      if (i == 0)
      {
          fibseq[i] = 0;
          pthread_exit(0);
      }                           // first fib term
  
      if (i == 1)
      {
          fibseq[i] = 1;
          pthread_exit(0);
      }                           // seconed fib term
      else
      {
          fibseq[0] = 0;
          fibseq[1] = 1;
  
          int p, pp, fibp, fibpp;
  
          p = (i - 1);
          pp = (i - 2);
          fibp = fibseq[p];
          // printf("fibseq[%d]%d\n",p,fibp);
          fibpp = fibseq[pp];
          // printf("fibseq[%d]%d\n",pp,fibpp);
          fibseq[i] = fibseq[i - 1] + fibseq[i - 2];
          // printf("fibseq[%d]%d,\n",i,fibseq[i]);
          pthread_exit(0);        // thread exit.
      }                           // End of else
  }                               // End of run.
  </code></pre>