Question

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

The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... It is defined by

the following mathematical expression, with X0 & X1 being 0 and 1, respectively:

Xn = Xn-1 + Xn-2

Write a C program using the fork() system call that generates and prints the Fibonacci

sequence in the child process. The number of members in the sequence will be

determined by a user provided as a user prompted input. Make the parent process wait

for the child process to complete before exiting. Make the program print both the

parent and child process IDs. The program should perform the necessary error checking

to only accept positive integers from the user. A failure of fork() should be caught and

printed as an error message using err_sys call.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void err_sys(const char* x)
{
   perror(x);
   exit(1);
}

int main(){

int n, f, c, i;
int *arr;
printf("Enter number of terms: ");
scanf("%d", &n);
arr = (int*) malloc(n* sizeof(int));

arr[0] = 0;
arr[1] = 1;

f= fork();

if(f < 0)
{

err_sys("Fork failed");
}

else if(f ==0){
//child process

for(i=0; i<n; i++)
{
    if(i != 0 && i !=1)
       arr[i] = arr[i-1] + arr[i-2];

    printf("%d ",arr[i]);
}
}

else{
    //parent process

    c = wait(NULL);
    printf("\n Child process ID %d \n", c);
    printf(" Parent process ID %d\n", getpid());
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .......
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formal...

    Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formally, it can be expressed as: fib0-0 fibl-1 fibn-fibn-1+fibn-2 Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter 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...

  • Check the words in bold. The Fibonacci sequence is the series of numbers 0,1,1, 2, 3,...

    Check the words in bold. 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 the Win32 thread library(not pthreads because it does not work on windows). This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program is...

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • fibonacci sequence in C

    The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 i. Write a C program using the fork() system call that that generates the Fibonacci sequence by the child process. ii. If the user input as 5 then the child process has to output up to the 5 that is output must be 0,1,1,2,3,5. iii. Parent has to print the child process id and child has to print the parent process id. iv. Parent has to finish only after the child terminates. v. Is there any process synchronization problem here? Justify your answer. vi. Modify the above program to create a zombie process. How do y ou identify the zombie process? vii. Modify the above program to create an orphan process. viii. Compare and contrast the process and threads.

  • The Collatz conjecture concerns what happens when we take any positive integer n and apply the...

    The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: if n is even 3 xn+1, if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n- 35, the sequence 1S 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4,2,'1 Write a C program using the fork) system call that generates this sequence in the...

  • Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...

    Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorthm: n={n / 2 , if n is even 3 * n + 1 , if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 35, the sequence is:  35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2,...

  • Please I need java code for this question with output: 4.19) The Fibonacci sequence is the...

    Please I need java code for this question with output: 4.19) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 Write a multithreaded program that generates the Fibonacci sequence using either the Java, Pthreads, or Win32 thread library. This program should work as follows: The user will enter on the command...

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • pls give answers for this

    The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 i. Write a C program using the fork() system call that that generates the Fibonacci sequence by the child process. ii. If the user input as 5 then the child process has to output up to the 5 that is output must be 0,1,1,2,3,5. iii. Parent has to print the child process id and child has to print the parent process id. iv. Parent has to finish only after the child terminates. v. Is there any process synchronization problem here? Justify your answer. vi. Modify the above program to create a zombie process. How do y ou identify the zombie process? vii. Modify the above program to create an orphan process. viii. Compare and contrast the process and threads.

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT