, .
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");
, PID 1000 (, , , , ).
Line1:
PID==1000 L1. :
:
1000
:
L1
Line2:
Process PID==1000 forks, (, PID==1001). man 2 fork PID==1000 if, fork() 0 .
PID==1000 3, L2, 5, PID==1001 9 if, End. :
:
-------------- 1000
|
|
1001
:
L1
L2
End
4:
PID==1000 , PID==1002.
, PID==1002 9, End, fork() 0 , PID==1000 5, L3, 6. :
:
------------ 1000
| |
| |
1001 1002
:
L1
L2
End
L3
End
6:
PID==1000 . PID==1003. 9, End. , , :
:
------------- 1000 --------------
| | |
| | |
1001 1002 1003
:
L1
L2
End
L3
End
End
End
, printf() , getpid(2), , . :
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());