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.