fork function process graph, valid and invalid out

2019-09-20 15:47发布

printf("L1 \n");
  if (fork() != 0) {
    printf("L2 \n");
    if (fork() != 0) {
      printf("L3 \n");
      fork();
    }
  }
printf("End \n");

I am having a hard time understanding this code. Could someone explain/ display what the process graph would look like? Also, a valid and invalid sequence.

My thought of the process graph was something like this:

L1 will hold two L2 processes then each L2 processes will hold 2 L3 processes and each L3 processes will hold an end processes. Is this correct?

A valid sequence for me was L1, L2, L3, End

An invalid one would be L2, L1, End this is because L1 must come before L2, L2 is dependent on L1.

标签: c process fork
1条回答
Anthone
2楼-- · 2019-09-20 16:07

Ok, here's the code properly indented and with numbered lines.

1.  printf("L1 \n");
2.  if(fork() != 0) {
3.    printf("L2 \n");
4.    if(fork() != 0) {
5.      printf("L3 \n");
6.      fork();
7.    }
8.  }
9.  printf("End \n");

Let's assume that when you run it for the first time the father's PID is 1000 (it doesn't matter what number is it, anyway it depends on OS, but we're making this assumption to make it clear on the process tree).

Line1:

Process PID==1000 prints L1. So far we have:

Process Tree:

           1000

Output:

L1

Line2:

Process PID==1000 forks, creating a child process (assume it's PID==1001). According to man 2 fork only process PID==1000 is entering the if block, since fork() will return 0 for the child process.

Process PID==1000 continues at Line 3 where it will print L2 and then continue to Line 5, whereas process PID==1001 jumps to Line 9, after the if block, so it will print End. So far, we have:

Process Tree:

  -------------- 1000
  |
  |
 1001

Output:

L1
L2
End

Line 4:

Process PID==1000 forks again, creating child process PID==1002.

Again, process PID==1002 jumps to Line 9, printing End because fork() returns 0 for it, whereas father process PID==1000 continues at Line 5, printing L3, and after that it's going to Line 6. So far we have:

Process Tree:

   ------------ 1000
   |              |
   |              |
  1001          1002

Output:

L1
L2
End
L3
End

Line 6:

Father process PID==1000 forks again. creating child process PID==1003. After that, both father and child processes go to Line 9, so they both print End. So finally, we have:

Process Tree:

   ------------- 1000 --------------
   |               |               |
   |               |               |
  1001           1002            1003

Output:

L1
L2
End
L3
End
End
End

To see that by yourself, you could change printf() in your current code using getpid(2) to see exactly what will be printed by each process. Code will be like this:

1.  printf("(pid %d)\tL1\n", (int) getpid());
2.  if (fork() != 0) {
3.      printf("(pid %d)\tL2\n", (int) getpid());
4.      if (fork() != 0) {
5.          printf("(pid %d)\tL3\n", (int) getpid());
6.          fork();
7.      }
8.  }
9.  printf("(pid %d)\tEnd\n", (int) getpid());
查看更多
登录 后发表回答