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

2.2 Threads Write a multi-threaded Linux program named fibonacci.c which accepts

ID: 3879406 • Letter: 2

Question

2.2 Threads Write a multi-threaded Linux program named fibonacci.c which accepts a single integer parameter from the command line (N), and then prints out the first N Fibonacci numbers to the screen You must launch one thread which computes all N Fibonacci numbers, storing them into a data structure accessible by the main thread e o Hint: a dynamically allocated array is simple! Declare a global variable that is a pointer to ints. In main). dynamically allocate enough space to hold the N values. Have your thread function then use the global ariable (pointer to the ints) to store the values. o You could also have the thread function dynamically allocate the space and then return the pointer via pthread exit).See tutorial video posted to YouTube on dynamic memory allocation and threads for more After launching the calculating thread, the main thread must wait until the calculating thread completes. Once the calculating thread completes, the main thread writes the results to the screen The calculating thread must not write any text to the screen e As in the previous question, print the process ID and username using: printf(" ?ID is: %d Username : %s ", getpid(), get!ogin()); which will print the process ID and usemame. You need to include includecuniatd.h>

Explanation / Answer

Below is the C implementation of the problem with dynamically allocated array and multi-threaded environment.The main waits for the thread to calculate the next number in series and stores it into a global array. Also, the process ID and username is printed using the getpid() and getlogin()

/* Fibonacci Program*/

#include "pthread.h"

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>>

// this data is shared by the thread(s)

int sum;

int *s ;

void *runner(void *param); // the thread

int main()

{

int count, i;

pthread_attr_t attr;

printf("Enter the value of N: ");

scanf("%d", &count);

// Dynamic memory allocation for the shared variable.

s = (int*)malloc(count*sizeof(int*));

if (count < 1) {

fprintf(stderr,"%d must be>= 1 ", count);

return -1;

}

// intialize the thread

pthread_attr_init(&attr);

// Take the fibonacci numbers from thread and store them in sum .

for(i=1;i<=count;i++){

pthread_t thread;

pthread_create(&thread,&attr,runner,(void*)i);

pthread_join(thread,NULL);

s[i] = sum ;

}

for(i=1;i<=count;i++){

printf("%d ", s[i]);

}

printf(" PID is : %d Username :%s ", getpid(), getlogin()); // To print the current process ID and the user.

}

// To call the fibonacci series function.

void *runner(void *param)

{

sum = fibonacci((int)param);

pthread_exit(0);

}

// function to calculate the next fibonacci number in series.

int fibonacci (int x)

{

if (x <= 1) {

return 1;

}

return fibonacci(x-1) + fibonacci(x-2);

}