The Fibonacci sequence is the series of numbers 011 2 3 5 8
The Fibonacci sequence is the series of numbers 0,1,1, 2, 3, 5, 8,.... Formally, it can be expressed as:
fib0 = 0
fib1 = 1
fibn = fibn-1 + fibn-2
Write a multithreaded program that generates the Fibonacci sequence using Pthreads C++. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in data that can be shared by the threads (an array is probably the most convenient data structure). When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, this will require having the parent thread wait for the child thread to finish, using the techniques described in Section 4.3 for the textbook.
The application must be submitted as a Microsoft Visual Studio project which includes the source codes of your program. or Microsoft Visual Studio project, use Microsoft Word to provide OLNY ONE assignment/user-documentation file. Documentation should include:
A report which clearly describes the design and description of your software components.
A logical flow diagram describes the logical flow structure of the application using any diagramming/graphic software such as Microsoft Visio.
A logical flow diagram for every method, functions, and procedures defined in the application using any diagramming/graphic software such as Microsoft Visio.
A description of any input files to test the application. (a file with valid data and a file with invalid data)
A detailed instruction of how the program can be compiled and executed.
Screenshots of execution sessions processing valid inputs and invalid inputs.
Solution
Here is the program,
#include<stdio.h>
#include<pthread.h>
#define SIZE 25
int fib_arr[SIZE]; //this array is shared by threads
void *fibonachi(void *n); // dymanically aloocate memory
int main(int argc,char *argcv[] ){
int num;
int x;
pthread_attr_t attr; //declare pthread_t type variable \'tid\'
if(argc !=2){
printf(\"Please insert one command line argument\ \");
return -1;
}
num=atoi(argcv[1]); //convert string value to integer and store to \'no\'
if(num<0){
printf(\"To Countinue please enter non-negative number\ \");
}
else{
pthread_attr_init(&attr);
pthread_t tid; //thread Identifier
pthread_create(&tid,&attr,fibonachi,num); //create a thread
pthread_join(tid,NULL);
printf(\" - Programs Out put - \ \");
printf(\"\ \");
printf(\"Set of Fibonachi numbers.........\ \");
for(x=0; x<num; x++){
printf(\"%d \",fib_arr[x]);
}
printf(\"\ \");
}
return 0;
}
void *fibonachi(void *n){
int x = 0;
int y = 1;
int fibn=0;
int i;
for(i=1; i<=n; i++){
fib_arr[i-1]=fibn;
fibn = x + y;
x = y;
y = fibn;
}
pthread_exit(0);
}

