The fork system call
In this section we shall see how the fork() system call works
The fork() function creates a new process that runs parallel to the main thread. The new process can execute the same code as the main thread or some other code based on certain criteria that is specified in the code itself.
Example:
Notice: The parent process is being executed before the child process in this case. The fork() method returns an integer which is the process id (PID) of the child process and is always 0 in case of a child process.
Visualizing the fork tree
Observe the following code snippets and predict many times will Hello World be printed.
Answer: 2³ = 8. Since the fork() function is called 3 times.
Explanation: The following diagram shows how the fork() system calls get executed
Last updated