I am learning the process management in Linux and I tried implementing the following C program, the output of which printed 15 PIDs(4 unique PIDs). I tried to figure out the process family tree but it really didnt help me understand why the PIDs were printed so many times. I went through few links including http://u.cs.biu.ac.il/~linraz/os/OS2.pdf, http://www.ibm.com/developerworks/aix/library/au-unixprocess.html , Who executes first after fork(): parent or the child? . But, I couldn't find the solution. It would be of great help if somebody helps me to understand this problem.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
printf ( "Parent:%d Child: %d\n",getppid(),getpid()); // To print the PIDs of the parent process and the child process
fork(); //System call to spawn a child process
printf ( "Parent:%d Child: %d\n",getppid(),getpid());
fork();
printf ( "Parent:%d Child: %d\n",getppid(),getpid());
fork();
printf ( "Parent:%d Child: %d\n",getppid(),getpid());
return 0;
}
Each time you call fork you're duplicating the current process. So the first printf is executed once- there's only one process. You then call
fork()
so the second printf is executed twice- once on the parent process and once on the new child process. Then the parent and child each fork creating 4 process total, etc...fork()
creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent.When ever if you made call to fork() the next instructions will be executed by child process and parent parent process
Your program look like this
consider all
fork()
created child process successfully.When your program is launched, the kernel assigns it a PID. The PPID, is the PID of the process that launched the program, usually your shell. For the purposes of this answer, we'll assign your shell the PID of
123
, and your program, at start, will be assigned the pid of130
.The first line of output will simply display the PPID and the PID.
The next thing the program does is split itself in two. The original program's PID and PPID are unchanged, the child's PPID is the original program's PID, and the child is assigned a new PID. As both programs are still tied to the same output channel, either one's output could be displayed first. For simplicity, we'll pretend output is ordered.
Now you're forking a second time... both process duplicate themselves. You now have four processes that want to print output to the same channel.
Now, with your third fork, all four of these processes duplicate themselves again.
Any questions?
When you
fork
, a new process is created, but the parent continues its execution too. So yourmain
will execute all 4 prints, your first fork will execute the subsequent 3 prints and so on. To have control on what code each child and parent will execute, you will have to use thefork
return value, not just fork.