Understanding the fork() statement and its process tree

偶尔善良 提交于 2019-12-13 02:36:47

问题


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?


回答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.




回答2:


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.




回答3:


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:

  • http://linux.die.net/man/2/fork

  • http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html



来源:https://stackoverflow.com/questions/18840638/understanding-the-fork-statement-and-its-process-tree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!