Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Creating multiple process using fork() in C
In this section we will see how to use the fork() system call to create child processes in C. The fork() function creates a new process by duplicating the calling process, allowing both parent and child processes to execute different tasks.
When fork() is called, it returns different values to distinguish between processes: a positive value (child's PID) to the parent process, 0 to the child process, and -1 if the fork failed.
Syntax
#include <unistd.h> pid_t fork(void);
Note: This program requires a Unix-like operating system (Linux, macOS) to compile and run. The
fork()system call is not available on Windows.
Return Values
- Greater than 0: Returns the child's process ID (PID) to the parent process
- 0: Returned to the child process
- -1: Fork failed (error occurred)
Example: Basic Fork Implementation
This example demonstrates how to create a child process and execute different code in parent and child processes −
#include <stdio.h>
#include <unistd.h>
int main() {
int n = fork(); // Create child process
if (n > 0) {
// Parent process
printf("Parent process (PID: %d)<br>", getpid());
printf("Child PID: %d<br>", n);
} else if (n == 0) {
// Child process
printf("Child process (PID: %d)<br>", getpid());
printf("Parent PID: %d<br>", getppid());
} else {
// Fork failed
printf("Fork failed!<br>");
return 1;
}
return 0;
}
Parent process (PID: 1234) Child PID: 1235 Child process (PID: 1235) Parent PID: 1234
Example: Creating Multiple Processes
This example shows how to create multiple child processes using fork() in a loop −
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int i;
pid_t pid;
printf("Main process PID: %d<br>", getpid());
for (i = 0; i < 3; i++) {
pid = fork();
if (pid == 0) {
// Child process
printf("Child %d: PID = %d, Parent = %d<br>",
i+1, getpid(), getppid());
return 0; // Child exits
} else if (pid < 0) {
printf("Fork failed for child %d<br>", i+1);
return 1;
}
}
// Parent waits for all children
for (i = 0; i < 3; i++) {
wait(NULL);
}
printf("All children completed. Parent exiting.<br>");
return 0;
}
Main process PID: 1234 Child 1: PID = 1235, Parent = 1234 Child 2: PID = 1236, Parent = 1234 Child 3: PID = 1237, Parent = 1234 All children completed. Parent exiting.
Key Points
- The
fork()system call creates an exact copy of the calling process - Both processes continue execution from the point where
fork()was called - Use
wait()in the parent process to synchronize with child processes - Always check the return value of
fork()for error handling
Conclusion
The fork() system call is essential for creating multiple processes in Unix-like systems. It allows parallel execution and is the foundation for process management in operating systems.
