8

I'm attempting to write a function that recursively computes the resulting fibonacci number from a given int n using forks in C.

Here is the function specification: If print is true, print it. Otherwise, provide it to the parent process. The solution should be recursive and it must fork a new child for each call. Each process should call doFib() exactly once. The method signature cannot be changed. Helper functions cannot be used.

Here is what I've written thus far based on my understanding of fork. I'm attempting to fork twice so I can spawn two child processes. One to do fib(n-1) and one to do fib(n-2). This way I can grab both of the results and combine them.

static void doFib(int n, int doPrint)
{
    pid_t pid1;
    pid_t retpid1;
    int status1;

    pid_t pid2;
    pid_t retpid2;
    int status2;

    pid = fork();
    if (pid == 0) // Child Process 1
    {
        exit(100); // sends 100 to the parent
    } 
    else if (pid > 0) // Parent Process 1
    {
        pid2 = fork();
        if (pid2 == 0) // Child Process 2
        {
            exit(200); // sends 200 to the parent
        }
        else if (pid2 > 0) // Parent Process 1
        {

        }

        retpid = waitpid(pid,&status,0);
        if (pid != retpid)
        {
            printf("waitpid error\n");
        }
        printf("I got this value from my child process 1: %d\n", WEXITSTATUS(status));
    } 
}

My questions:

1. How do I grab both of the exit values from the two child processes? I know how to grab one (see code), but how do I grab them both?

2. Since doFib does not return a value, how do I get the value of my doFib call in either of my child processes so I can combine them?

3. Am I doing my forking correctly? I was confident with one fork, two is making my head hurt.

This is a practice midterm problem of a series that I'm currently working through to prepare for an upcoming exam.

5
  • Is this homework? Please tag it as such then. Commented Feb 6, 2012 at 7:25
  • 1
    Actually, it's not homework. This is a problem in a series of problems I'm working through voluntarily to prepare for a midterm. They aren't due or turned in in any way. Commented Feb 6, 2012 at 7:31
  • 2
    +1 to offset the downvote. Legitimate question and this is not homework. Commented Feb 6, 2012 at 7:33
  • @Zeychin: Thanks very much! Didn't want to be downvoted for an illegitimate reason. :/ Commented Feb 6, 2012 at 7:35
  • 1
    It is a fun exercise. Here's an implementation in Python. Commented Feb 6, 2012 at 9:37

1 Answer 1

6

1) Call waitpid twice.

2) The waitpid call will put it in status.

3) Two things: First, to terminate a forked process, use _exit, not exit. The exit function can mess up file descriptors that the parent is still using. It doesn't matter in your case, but why learn bad habits? Second, you don't need the else if (pid2 > 0) clause. That's all that's left.

Sign up to request clarification or add additional context in comments.

5 Comments

I thought it was more than likely just calling waitpid twice, thanks for clarifying that. I do understand the waitpid call putting whatever exit value I give _exit() into status, but how do I get the value of doFib(n-1) (for example if it's called in the first child), if the function doesn't return a value? What do I give to _exit()?
You give the value to _exit. You calculate it and then call _exit right there as soon as you have the value. You never need to return it anywhere because all this work takes place inside doFib.
Ah. I think I understand now. So, basically return the value (n-1) and (n-2) in the child processes and add those values together at the end and print. The function will take care of the rest. Is that correct?
@CODe It sounds correct to me, though the last part scares me. All of that logic takes place in the function.
It scares me too, heh. I'll play with it and I'm sure I'll get it to work relatively quickly thanks to you. Thanks very much. The rest of the problem series is formatted like this and I was scratching my head for a while wondering how these worked without a function return value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.