![]() |
VOOZH | about |
Fork is a system call in the Operating System used to create a copy of the process, the copied process created is known as the child process. The main process or the process from which the child process is created is known as the parent process. The parent and child processes are in separate address spaces in fork(). A Child process will have its Process ID different from the parent's ID.
Process: The task or job performed by the computer. Example: running a program.
We use fork() one time in a parent (P) then a child process is created (C1) and there is also a parent process (P) present. Similarly, if we write fork again, here C1 becomes the parent and creates a new child process C2 and there is a parent process (C1) available. P also creates a child process (C3) and P is also present (P).
Here n is the number of times forks called.
Remember that child processes return 0, while parent processes return a positive value (probably +1) at the fork call.
1. Simple Fork:
Output:
2. Parent and Child Processes Printing with PIDs:
Output:
3. Forking Multiple Child Processes:
Output:
fork() system call is the fundamental tool that allows processes to duplicate themselves. It makes a copy of process in a separate memory space, it provides both child and parents a different process ID (PIDs) for identification. The uses of fork are multitasking, parallel processing and complex process structure.