Write a multithreaded program that generates the Fibonacci sequence using Win32
ID: 3536365 • Letter: W
Question
Write a multithreaded program that generates the Fibonacci sequence using Win32 thread library. This program should work as follows: The program will then create three separate threads each of which will generate separate sets of Fibonacci numbers. One thread will generate the number of Fibonacci numbers given by the command line, the second thread will generate the number of Fibonacci numbers given by the command line minus one, and the third thread will generate the number of Fibonacci numbers given by the command line plus one. Each thread will place its sequence in a data structure that can be accessed by the parent thread (arrays are probably the most convenient data structures). When the threads finish execution, the main thread will output the sequence generated by the child threads. Because the main thread cannot begin outputting the Fibonacci sequence until the child threads finishes, this will require having the main thread wait for the child threads to finish, using the WaitForMultipleObjects() system call.
I need help with this one. Im not to familiar with Win32.
Explanation / Answer
include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int a=0, b=1, n, i, user_input;
pid_t pid;
printf("Enter the number of a Fibonacci Sequence: ");
scanf("%d", &user_input);
if (user_input < 0)
printf("Please enter a non-negative integer! ");
else
{
pid = fork();
if(pid < 0){
printf("fork failed");
}
else if (pid == 0)
{
printf("Child is producing the Fibonacci Sequence... ");
printf("%d %d ",a,b);
for (i=0;i<user_input;i++)
{
n=a+b;
printf("%d ", n);
a=b;
b=n;
}
printf("Child complete ");
}
else
{
printf("Parent is waiting for child to complete... ");
wait(NULL);
printf("Parent ends ");
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.