Like Tree3Likes
  • 3 Post By AndiPersti

How do I implement recursive fibonacci into fork()?

This is a discussion on How do I implement recursive fibonacci into fork()? within the C Programming forums, part of the General Programming Boards category; 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; ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    7

    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.
    Last edited by Lavender; 03-03-2013 at 07:20 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Lavender View Post
    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

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    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?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Try it. I'm pretty sure your computer won't blow up.

    Bye, Andreas
    Dude22, Lavender and stahta01 like this.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    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?

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Lavender View Post
    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.

    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
    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

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    Sorry but what is man pages?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,591

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    when I type "man 3 clock" in the commands, it says command not found.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,703
    Quote Originally Posted by Lavender View Post
    when I type "man 3 clock" in the commands, it says command not found.
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    Quote Originally Posted by stahta01 View Post
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Fibonacci function
    By 843 in forum C++ Programming
    Replies: 8
    Last Post: 03-18-2011, 01:58 PM
  2. double fork for implement background
    By yen in forum C Programming
    Replies: 2
    Last Post: 11-24-2010, 11:24 PM
  3. Replies: 9
    Last Post: 11-12-2007, 02:29 PM
  4. Recursive Fibonacci, some help needed
    By cwafavre in forum C Programming
    Replies: 8
    Last Post: 11-04-2007, 01:20 PM
  5. Fibonacci series using a recursive function
    By Dargoth in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2002, 11:54 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21