Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, … Formally,

ID: 3636599 • Letter: T

Question

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 either the Java or Pthreads
library. 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 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.

(its book: Operating system concepts/ Abraham Silberschatz, Peter B Halvin, Greg Gagne/ 8th edition )

Explanation / Answer

#include #include #include #include #include /* need to calculate which I will implement later */ void *fibr(void *n); void *fibr_1(void *k); signed long long int fibonacci(signed long long int); int main(){ clock_t begin, end; double time_spent; pthread_t tid,tid1; int result,result1; signed long long int n=6; signed long long int m=7; result=pthread_create(&tid,NULL,fibr,&n); if(result){ perror("pthread_create"); return 1; } result1=pthread_create(&tid1,NULL,fibr,&m); if(result1){ perror("pthread_create"); return 1; } if(pthread_join(tid,NULL)){ perror("pthread_join"); return 1; } if(pthread_join(tid1,NULL)){ perror("pthread_join"); return 1; } printf("Fib value=%lld ",n+m); pthread_exit(NULL); } void *fibr(void *n){ signed long long int *y=n; signed long long int x=*y; pthread_t tid2,tid3; signed long long int i,j; /* How do I assign values to i , j in order to achieve the level viz fib(n-2)....fib(n-4) */ if(pthread_create(&tid2,NULL,fibr_1,&i)) { perror("pthread_create"); } if(pthread_create(&tid3,NULL,fibr_1,&j)) { perror("pthread_create"); } if(pthread_join(tid2,NULL)) { perror("pthread_join"); } if(pthread_join(tid3,NULL)) { perror("pthread_join"); } /* How to return the values of i, j combined with *y . if I do *y+i+j, the result is not coming correctly */ *y=fibonacci(x); return NULL; } void *fibr_1(void *k){ long long int *a=k; long long int b=*a; *a=fibonacci(b); return NULL; } signed long long int fibonacci(signed long long int x){ if((x==0)||(x==1)) return x; return fibonacci(x-1)+fibonacci(x-2); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote