• 11-03-2007
funkyjohny
The Fibonacci sequence problem: 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
fib2 = fibn-1 + fibn-2

Write a C program using the fork() system call that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in the Fibonacci sequence will be output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke the wait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is passed on the command line.
• 11-03-2007
Salem
OK, so can you say solve this without using fork() ?
• 11-03-2007
funkyjohny
I want to use fork().
• 11-03-2007
Salem
So you said.

But do we have to explain the basic problem of displaying the Fibonacci series first, or can we get to the point by you showing us what you've managed to far and then we can help you with the next step of making use of fork().
• 11-04-2007
maklolm
hi
that shouldn't be too hard

Code:

```#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() {   int n;   printf("please enter the number n\n");   scanf("%d",&n);   if (n<=0)     {       printf("moron!\n");       return 1;     }   pid_t pid = fork();   if (pid == 0)     {       /*child process*/       /*here we print out the fibonacci numbers, which i would do like this*/       printf("fibonacci-numbers up to %d:\n",n);       int *fib_array = (int*)malloc(sizeof(int)*n);       int i;       for (i=0;i<n;i++)         {           if (i<=1)             fib_array[i] = 1;           else             fib_array[i] = fib_array[i-1]+fib_array[i-2];           printf("%d\n",fib_array[i]);         }       free(fib_array); /*not really neccessary, but anyway*/       exit(0);     }   if (pid < 0)     {       printf("error forking child\n");       return 1;     }   if (pid > 0)     {       /*parent process*/       int status;       wait(&status);       printf("now the child process has exited with the status %d\n",status);       return 0;     } }```
cheers
maklolm
• 11-04-2007
Salem
It's not a question of being easy, it's a matter of the OP learning to do the homework for themselves and not being spoon-fed a complete answer right off the bat.

Not that it matters, it seems the OP has already gotten lucky elsewhere as well.
• 11-07-2007
funkyjohny
Thank you maklolm!!!