# How do I implement recursive fibonacci into fork()?

• 03-03-2013
Lavender
How do I implement recursive fibonacci into fork()?
This is what I have so far.

Code:

```#include<stdio.h> #include<time.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> int main() {   int pid;   int n;   float x,y,z,a,b,c;   printf("input the fib number you want: ");   scanf("%d", &n);   x = clock(); /*starts clock on program*/   pid = fork();    /* child process created */   if (pid == 0)    /*begin the child*/     {           a = clock(); /*start clock on child*/           ........../*recursive fib here*/           printf( "The fib number is %d\n ", fib(n));           b = clock(); /*ends clock on child*/           c = (b - a)/CLOCKS_PER_SEC;           printf("\n\nThe execution time was: %.15f" , c );       _exit(0); /*child process ends*/     }       y = clock(); /*ends clock on program*/       z = (y - x)/CLOCKS_PER_SEC;       printf("\n\nThe execution time was: %.15f" , z );   /* Now back to the parent code */   if (pid < 0)     { fprintf(stderr, "\tfork failed\n\n");       exit(1);     }  wait(); /* wait for the child process to end */   return; }```
I want to use this as recursive Fibonacci function but don't know how to code it in.

Code:

```unsigned int fib(unsigned int n) {         if (n == 1 || n == 2)         {               return 1;         }         else         return (fib(n-2)+fib(n-1)); }```
This was the instructions that were given to me to how the program should be structured: In the child process implement the recursive Fibonacci algorithm. Use the clock( ) function to compute the time it takes to run the program. Start the clock at the beginning of the program and end it at the end of the program, then compute the execution time. Then begin the clock inside (at the beginning) the child process and end it at the end of the child process. Compute the execution time of the child process.
• 03-04-2013
AndiPersti
Quote:

Originally Posted by Lavender
I want to use this as recursive Fibonacci function but don't know how to code it in.

Just put it where you would put it if you would just use one process and call it from the child process. The child process inherits the whole program from its parent, thus all functions are also visible for the child.

You should also look at the man pages for clock() (what type does it return?) and wait() (what are its parameters?).

Bye, Andreas
• 03-04-2013
Lavender
I know where to put it but I don't know how to code it in. I doubt I can copy and paste it in and it'll work.. does it?
• 03-04-2013
AndiPersti
Try it. I'm pretty sure your computer won't blow up.

Bye, Andreas
• 03-04-2013
Lavender
lol thanks! :) the Fibonacci works. Now I have another problem.. It wont print out the "The execution time was:... ". It only prints out the Fibonacci then ends the program. Is the clock() supposed to be double? The wait() is NULL.. or do I have to change it to something else?
• 03-04-2013
AndiPersti
Quote:

Originally Posted by Lavender
Now I have another problem.. It wont print out the "The execution time was:... ". It only prints out the Fibonacci then ends the program.

Code:

```printf("\n\nThe execution time was: %.15f" , c ); _exit(0); /*child process ends*/```
"_exit(0)" terminates the process immediately and whether it flushes the output stream is implementation-dependent. You don't finish your printf()-call with a newline and that's why your output is lost.

Quote:

Is the clock() supposed to be double?
Don't you know that you can read the manpages by using the "man" program?
Code:

`man 3 clock`
Quote:

The wait() is NULL.. or do I have to change it to something else?
Again, reading the man page should tell you how wait() works.

One problem is that clock returns an integer type and thus dividing by CLOCKS_PER_SEC will use integer division, i.e. the result will be truncated to an integer.

You also calculate the working time for the parent before your wait()-call. Is that on purpose? Suppose you calculate the 40th Fibonacci number. This should take some time but the processing time for the parent is calculated immediately after the fork and probably zero.

Bye, Andreas
• 03-04-2013
Lavender
Sorry but what is man pages?
• 03-04-2013
Matticus
• 03-04-2013
Lavender
when I type "man 3 clock" in the commands, it says command not found.
• 03-04-2013
stahta01
Quote:

Originally Posted by Lavender
when I type "man 3 clock" in the commands, it says command not found.

Quote:

A man page (short for manual page) is a form of online software documentation usually found on a Unix or Unix-like operating system.
Your operating system (OS) is name and version?

Tim S.
• 03-04-2013
Lavender
Quote:

Originally Posted by stahta01
Your operating system (OS) is name and version?

Tim S.

Im using Linux through putty.exe version 0.62.0.0. But anyways I got the problem fixed. :) The problem was the _exit(0) where I had to take the _ away so it supposed to be exit(0). Thanks to everyone who helped. Assignment all done and working perfectly!