1

I am trying to figure out how many processes are created with the following C code:

int main () { fork(); if (fork()) { fork(); } fork(); return 0; } 

There are a couple of things I am confused about:

Each time fork() is called, does the child start from the start of the code, or does it start where the current fork() created it from? For instance, if line 3's first fork is called, will I start the child at line 4, or line 1? I believe this is a stupid question, b/c it would create an infinite loop if each child started from the beginning, but I want to be safe about this assumption.

Next, when fork is called, does the current process split into two NEW processes, one being the parent, and the other the child, or is the current process automatically the parent, so really, just one extra process is created.

Lastly, with the if statement, I am pretty certain fork returns the value of the parent id when it is actually the parent, and always 0 when it is the child. So, am I correct to assume that the if statement will be false for every child spawned?

With all my assumptions above, this is the process tree I came up with, and would greatly appreciate if someone sees a fault that is throwing it off. The number of the children in the tree represent the line of code that the fork is currently happening:

 main | | | | 3 4 5 7 // the main/initial process calls fork 4 times | | | | | 4 5 7 7 7 // the first fork will see 3 additional forks since it was called | | // at line 3 and I assume resumes at line 4. 7 7 // The second and third original children would each only callthe // very last fork(). The last fork has no other forks following. 

Therefore, there are 10 total processes created (including main). Did I do it wrong?

3 Answers 3

1

(1) The line directly after the fork.

(2) One new process. The child is a copy of the parent.

(3) fork returns the child's pid in the parent and 0 in the child. The if statement will be false for every child. It should come out to 12 processes. I'm terrible at drawing graphs but you should be able to figure it out.

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

2 Comments

Thank you for clarifying that. I have one more new confusion now that if you have a spare minute, maybe you can clear up also. There's a fork() in the if statement. So, if the child starts the process at the very next line, will it ignore the fork() inside of the if statement block? This is what I must assume since in that specific child process, the if statement was never satisfied.
Your if statement is a fork. That fork will produce a child. In the child the fork return zero so that if statement will evaluate to false and not be executed by the child. However the parent will receive the child's pid from the fork within the if statement. The pid will never be zero so the if statement will evaluate to true for the parent and he will execute if code.
1

Just to be clear here, it's not that the child "starts" at any particular line. (Lines don't actually exist at run-time anyway.)

The child becomes a precise copy of the parent at the point of the fork (except for its kernel process record). So fork() is called once but returns twice, once in the parent and once in the child. That's why it's called fork. The difference starts at the moment of the return, since the return value of fork is different in the two branches. But it's just an ordinary function return in both cases.

2 Comments

Thank you. This cleared up my misconception about it going to the next line of code. The current statement is actually judged by both processes with the current values. So, does my process tree look at all correct? I updated it by adding a few processes I missed. Also, the node numbers are based on the line in which that current fork occurs.
@TheRationalist: it has the right number of nodes. I find the presentation a bit confusing, though.
0

Q: Each time fork() is called, does the child start from the start of the code, or does it start where the current fork() created it from?

A: From the statement after the "fork()".

Q: When fork is called, does the current process split (resulting in a) NEW process?

A: Yes.

Q: Does fork returns the value of the parent id when it is actually the parent, and always 0 when it is the child

A: Yes. Unless there's an error creating the process, in which case it returns -1.

Look here for more info:

3 Comments

Only 1 new process is created; the child's pid is returned to the parent not its own pid.
Quibble quibble quibble :(. The point is 1) Yes, the current process "splits", and 2) the end result is two processes.
Actually, Duck understood what I meant in the question. My confusion was not whether two processes resulted, but rather whether the original process that called fork is considered one of the two resulting processes. Now that I look back, I realize I didn't make this as clear as I should have. Thank you for your response though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.