Check out example codes for "Fibonacci program c pthread". It will help you in understanding the concepts better.

Code Example 1

``````/*============================================================================
Description :The Fibonacci sequence
============================================================================ */
#include <stdio.h>
#include <stdlib.h>

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 <intgervalue>\n");
return -1;
}                           // valdiate num of args.

if (atoi(argv[1]) < 0)
{
printf("%d must be>=0\n", atoi(argv[1]));
return -1;
}                           // valdiate value of arg1.

n = atoi(argv[1]);
fibseq = (int *)malloc(n * sizeof(int));

for (i = 0; i < n; i++)
{
}                           // End of creating threads.

int j;

for (j = 0; j < n; j++)
{
}                           // End of wating the threads to exit.

// printing fibseq.
printf("The Fibonacci sequence.:");
int k;

for (k = 0; k < 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;
}                           // first fib term

if (i == 1)
{
fibseq[i] = 1;