- “fork()” is used in Unix-like operating systems to create a new process. The is called the “parent” process, and the new process created is called the “child” process.
- After a successful fork(), two processes are running the same code: the parent and the child.
- The fork() function returns a value that helps to identify whether the code is running in the parent or the child process.
Below are different values returned by fork().
- Negative Value: The creation of a child process was unsuccessful.
- Zero: Returned to the newly created child process.
- Positive value: Returned to parent or caller. The value contains the process ID of the newly created child process.
Table of Content
Fork() Method in C
Basic Example of fork()
Explanation:
printf("L0\n");: The program starts by printingL0to the screen. This happens only once.fork();: The firstfork()is called. This creates a new process. Now, there are two processes:- The original parent process.
- The new child process created by
fork().
Both the parent and the child process will continue to execute the code after the
fork().printf("L1\n");: Both the parent and child processes printL1. So,L1is printed twice.fork();: The secondfork()is called. This creates another new process from each of the two existing processes (the parent and the first child). Now, there are four processes in total:- The original parent process.
- The first child process.
- Two new processes created by the second
fork().
printf("Bye\n");: All four processes printBye. So,Byeis printed four times.
Creating a New Process Using fork()
How Many Times Will The Below Code Prints Out?
The number of times “hello” is printed equals the number of processes created. The total number of processes is 2n, where n is the number of fork() system calls.
So, if n=3, the total is 23 = 8 processes.
Printing Process ID For Parent and Child Processes
Example 1:
When fork() is called, it creates two processes: the parent and the child. Both processes will run the entire code from that point onward. The getpid() function retrieves the current process’s ID, so it will print the process ID for both the parent and the child.
Example 2:
Explanation:
- A variable
xis initialized to 1. - The
fork()call creates two processes: the parent and the child.- The child process increments
xand prints its value. - The parent process decrements
xand prints its value.
- The child process increments
As a result, the output shows how the value of x differs in the parent and child processes due to the separate memory spaces they occupy. The child will print x = 2, while the parent will print x = 0.
exit(0): Indicates success.exit(1)orexit(2), etc.: Indicates failure, with different values sometimes used to represent different types of errors.
Creating Multiple Child Processes
Explanation:
- Each process is independent after
fork(): Once a child process is created, it runs independently of the parent.return 0;in the child process only exits that child, not the parent or any other child processes. - The parent process continues to the next iteration of the loop regardless of what happens in the child process.
- Parent Process:
- The parent calls
fork(). fork()creates a child process.- In the parent process,
fork()returns a positive number (the PID of the child), so theif (fork() == 0)condition is false. - The parent process does not enter the
ifblock and continues to the next iteration of the loop.
- The parent calls
- Child Process:
- In the child process,
fork()returns0, so theif (fork() == 0)condition is true. - The child process executes the code inside the
ifblock, prints its message, and then hitsreturn 0;. return 0;causes the child process to exit, but this does not affect the parent process, which continues running
- In the child process,
- The parent process creates another child (Child 2) with
fork(). - The parent again does not enter the
ifblock and moves to the next iteration. - The second child process prints its message and exits with
return 0;.
- The parent creates the third child (Child 3) with
fork(). - The parent finishes the loop after this iteration.
- The third child process prints its message and exits with
return 0;.
Recent Posts
- Abundența_norocului_și_fortuna_de_a_construi_un_viitor_prosper_și_împlinit
- Šikovná_strategie_fortuna_nabízí_pestré_sázky_a_bonusy_pro_každého_hrá
- Absorbția_riscurilor_financiare_și_fortuna_acumulată_prin_investiții_intelig
- Analiza_skuteczności_strategii_finansowych_z_uwzględnieniem_fortuna_i_długote
- LuckyLouis Casino mobilapp: spil nemt og hurtigt på farten
Recent Comments
Archives
- June 2026
- May 2026
- October 2025
- October 2024
- September 2024
- August 2024
- June 2024
- January 2024
- November 2023
- October 2023
- September 2023
- August 2023
- June 2023
- May 2023
- March 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
Categories
- 21
- aks 1 en
- aks 1 pl
- aks 18
- aks 28
- aks 31
- Angular
- AWS
- Backend Development
- Big Data
- Blog
- Cloud
- crypto 01
- crypto 05
- crypto 15
- crypto 19 en
- crypto 20
- crypto 20.05
- crypto 21.05
- crypto 29
- crypto 5
- Database
- Deployment
- DevOps
- Docker
- fi
- Frontend Development
- GitHub
- Google Cloud Platform
- hu
- Installations
- it
- Java
- JavaScript
- Linux
- MySQL
- Networking
- News
- nl
- no
- NodeJS
- Online casino
- Operating System
- Post
- public
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- sushicasino 13
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
- Στοιχηματικές
Search
Recent Post
Abundența_norocului_și_fortuna_de_a_construi_un_viitor_prosper_și_împlinit
- 27 June, 2026
- 9 min read
Šikovná_strategie_fortuna_nabízí_pestré_sázky_a_bonusy_pro_každého_hrá
- 27 June, 2026
- 10 min read
Absorbția_riscurilor_financiare_și_fortuna_acumulată_prin_investiții_intelig
- 27 June, 2026
- 8 min read