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
Difference Between fork() and vfork()
In this post, we will understand the difference between system calls fork() and vfork() −
Both fork() and vfork() are system calls used to create child processes in Unix-like operating systems. However, they differ significantly in how they handle memory management, execution order, and resource sharing between parent and child processes.
The fork() System Call
The fork() system call creates a new process by making a complete copy of the parent process. Here are its key characteristics:
The child and parent process have separate memory spaces.
The child and parent process are executed simultaneously.
This call uses the copy-on-write technique as an optimization.
Child process doesn't have the ability to suspend execution of the parent process.
The page of one process doesn't get affected by the page of other process.
It is more frequently used and considered safer.
Memory is allocated efficiently using copy-on-write.
If the child process alters the page in the address space, it is not visible to the parent process.
fork() Example
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("Child process executing<br>");
} else if (pid > 0) {
// Parent process
printf("Parent process executing<br>");
}
return 0;
}
The vfork() System Call
The vfork() system call was designed for scenarios where the child process immediately calls exec(). Its characteristics include:
The parent process and child process share the same address space.
Once the child process has been executed, the parent process begins execution.
This system call doesn't use copy-on-write technique.
The child process suspends the execution of the parent process.
The page of one process gets affected by the page of another process.
It is less frequently used and considered risky.
No additional address space is allocated initially.
If the child process alters the page in the address space, it can be seen by the parent process.
vfork() Example
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t pid = vfork();
if (pid == 0) {
// Child process - should call exec() or _exit()
printf("Child process<br>");
_exit(0); // Must use _exit(), not return
} else if (pid > 0) {
// Parent process - executes after child exits
printf("Parent process<br>");
}
return 0;
}
Comparison
| Aspect | fork() | vfork() |
|---|---|---|
| Memory Space | Separate address spaces | Shared address space |
| Execution Order | Concurrent execution | Child first, then parent |
| Copy-on-Write | Yes | No |
| Parent Suspension | No | Yes |
| Memory Isolation | Complete isolation | No isolation |
| Usage Frequency | Common | Rare |
| Safety | Safer | Risky |
Key Points
vfork()is primarily intended for cases whereexec()is called immediately after process creation.Modern systems often implement
vfork()asfork()due to copy-on-write optimizations.Using
returnin avfork()child can corrupt the parent's stack.fork()is generally preferred for its safety and predictable behavior.
Conclusion
While both fork() and vfork() create child processes, fork() provides memory isolation and concurrent execution, making it safer and more commonly used. vfork() shares memory space and suspends the parent, making it suitable only for specific scenarios where exec() follows immediately.
