Question

In: Computer Science

PART B: Computing Fibonacci Numbers With Threads This programming project is based on a problem given...

PART B: Computing Fibonacci Numbers With Threads

This programming project is based on a problem given in our textbook. 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 series using the pThreads library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program will generate. The program will then create a separate thread that will generate the Fibonacci numbers placing the sequence in data that is shared by the threads (an array or vector 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 discussed in the class

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C code with comments and screenshot for easy understanding :)

CODE-

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include<pthread.h>
#include<errno.h>
#include <stdlib.h>
#include <signal.h>

int fib0[100];
int fib1[100];
int fib2[100];

void *myThreadFun0(void *vargp)
{
    int *p = (int *)vargp;
    fib0[0] = 0;
    fib0[1] = 1;
    int l;
    //printf("%d\n",*p);
    for (l = 2; l<*p; l++){
       fib0[l] = fib0[l-1]+ fib0[l-2];
    }
    return NULL;
}


void *myThreadFun1(void *vargp)
{
    int *p = (int *)vargp;
    fib1[0] = 0;
    fib1[1] = 1;
    int l;
    //printf("%d\n",*p);
    for (l = 2; l<*p; l++){
       fib1[l] = fib1[l-1]+ fib1[l-2];
    }
    return NULL;
}


void *myThreadFun2(void *vargp)
{
    int *p = (int *)vargp;
    fib2[0] = 0;
    fib2[1] = 1;
    int l;
    //printf("%d\n",*p);
    for (l = 2; l<*p; l++){
       fib2[l] = fib2[l-1] + fib2[l-2];
    }
    return NULL;
}
int main(int argc, char *argv[])
{
    pthread_t *tid;
    tid = (pthread_t *)malloc(sizeof(pthread_t) * atoi(argv[1]));
    int *count = (int *) malloc(sizeof(int) * atoi(argv[1]));
    int i;
    int n = atoi(argv[1]);
    int j = n-1;
    int k = n+ 1;

    pthread_create(&tid[0], NULL, myThreadFun0, &n);
    pthread_create(&tid[1], NULL, myThreadFun1, &j);
    pthread_create(&tid[2], NULL, myThreadFun2, &k);


    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    pthread_join(tid[2], NULL);
    printf("Sequence by thread1:\n");
    for (i = 0; i<n; i++){
       printf("%d ", fib0[i]);
    }
    printf("\n");
    printf("Sequence by thread2:\n");
    for (i = 0; i<j; i++){
       printf("%d ", fib1[i]);
    }
    printf("\n");
    printf("Sequence by thread3:\n");
    for (i = 0; i<k; i++){
       printf("%d ", fib2[i]);
    }
    printf("\n");
    exit(0);
}


SCREENSHOT-


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem solved with recursion. However, this can also be done more efficiently using dynamic programming. Create a program that uses dynamic programming techniques to list the first 30 Fibonacci numbers.
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Part B - Calculate the molar solubility in NaOH Based on the given value of the...
Part B - Calculate the molar solubility in NaOH Based on the given value of the Ksp, what is the molar solubility of Mg(OH)2 in 0.180 M NaOH? Express your answer with the appropriate units. Hints
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and...
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period of time approximates the typical pid usage in which a pid is assigned to a new process, the process executes and then terminates, and the pid is released on the process's termination.) On UNIX and Linux systems, sleeping is accomplished through...
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits 0 to 9 occur in all numbers between a and b, inclusive. In order to make your code simpler, implement first the function intToList(n) that takes as input one integer n, and returns a list with the digits of n in the order they appear. For example, intToList(1964) should return [1,9,6,4]. Using the function intToList, implement the function digitsCount(a, b) that returns a list...
Problem 3.2 A more efficient version of the function for computing Tetranacci numbers can be defined...
Problem 3.2 A more efficient version of the function for computing Tetranacci numbers can be defined by following three steps: Implement a function which takes a list of integers and adds the sum of the top four elements to the head of the list (e.g., in F#, 1::1::1::1::[] should become 4::1::1::1::1::[]). Implement a recursive function which computes the nth Tetranacci number in linear time. This function may use a linear amount of space to compute its result, but the number...
JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A...
JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A Add to your project this class Position, which has x and y coordinates. Create an abstract class GameElt, which has a String name, an int health (keep it in the range 0 to 100) and a Position pos. For GameElt, include a default constructor that starts pos at (0, 0), and a parameterized constructor that takes x and y coordinates and a name. health...
Java Part 2 of 4 - Amusement Park Programming Project MUST BE COMPATIBLE WITH PART 1...
Java Part 2 of 4 - Amusement Park Programming Project MUST BE COMPATIBLE WITH PART 1 https://www.chegg.com/homework-help/questions-and-answers/java-part-1-4-amusement-park-programming-project-requirements-use-java-selection-construct-q40170145?trackid=ERIFssNL Requirements: Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Proper error handling. Class: Merchandise – models merchandise available in the gift shop such as t-shirts, sweatshirts, and stuffed animals. Instance Fields: id : long – to identify the specific merchandise item. category : String – to...
Single Lane Bridge Problem : Java Threads Given : //Use ReentrantLock for mutual exclusion import java.util.concurrent.locks.Lock;...
Single Lane Bridge Problem : Java Threads Given : //Use ReentrantLock for mutual exclusion import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class bridge { private final ReentrantLock myLock = new ReentrantLock(); public bridge(){ } public String cross(){ myLock.lock(); try { return "crossing the bridge"; } finally { myLock.unlock(); } } } public class lane extends Thread { int cars; bridge Bridge; public lane(int Cars1, bridge Bridge1) { cars = Cars1; Bridge = Bridge1; } public void run(){ for(int x=0;x //Important clue :...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT