I am learning fork() in Linux, and here is my program:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 int main(void){
5 int pid;
6 pid = fork();
7 if(pid < 0){
8 exit(1);
9 }
10 if(pid == 0){
11 fork();
12 fork();
13 printf("pid:%d ppid:%d\n",getpid(),getppid());
14 exit(0);
15 }
16 else{
17 printf("parent pid:%d ppid:%d\n",getpid(),getppid());
18 exit(0);
19 }
20
21 }
Sometimes it worked fine, with result like this:
./test1.out
parent pid:27596 ppid:21425
pid:27599 ppid:27597
pid:27597 ppid:27596
pid:27598 ppid:27597
pid:27600 ppid:27598
But the result was not consistent, more often than not it worked like this:
parent pid:27566 ppid:21425
pid:27567 ppid:27566
pid:27568 ppid:27567
pid:27569 ppid:1599
pid:27570 ppid:1599
Which makes no sense to me, so I typed $ps aux to find out what process 1599 is:(with some columns deleted)
USER PID VSZ RSS STAT START COMMAND
linux 1599 63236 6316 Ss 09:03 /lib/systemd/systemd --user
Can anybody help me straighted things up?
The "inconsistency" you observed is because sometimes, the parent process(es) exited before their child process(es) terminated. So, these child processes become "orphans" as their parent processes are not waiting for them. As a result, they are "re-parented" to the init process. While traditionally the process id of the "init" process is 1, it's not always true. POSIX leaves it as implementation-defined:
Thus you see a particular PID as the parent (1599 in your example), which happens to be "init" process equivalent on your Linux. The systemd is an init variant used in Debian Linux distributions - which follows a slightly more complicated implementation.
In essense, what you observed is pretty normal. Ideally, you should reap all the processes in order to avoid zombie processes.
I suppose that, sometimes, a race condition happens, and the parent dies before the child. Hence, the child becomes children from init process. In your case, that must be systemd.
Anyway, be advised that running
fork(); fork();
will produce 4 processes, which is (probably) not what you intend. Use control structure as you did with the first one to have fine control on the behaviour of your program.It is because the parent process terminates first. In Linux there are no ophan processes. They are assigned to the
init
process.If you want to control your processes so that child process terminates first, make parent process wait. Using
wait()
sys_call.