Both fork() and vfork() are the system calls that creates a new process that is identical to the process that invoked fork() or vfork(). Using fork() allows the execution of parent and child process simultaneously. The other way, vfork() suspends the execution of parent process until child process completes its execution. The primary difference between the fork() and vfork() system call is that the child process created using fork has separate address space as that of the parent process. On the other hand, child process created using vfork has to share the address space of its parent process.
Let us find some differences between fork() and vfork() with the help of comparison chart shown below.
Content: fork() Vs vfrok()
|Basis for Comparison||fork()||vfork()|
|Basic||Child process and parent process has separate address spaces.||Child process and parent process shares the same address space.|
|Execution||Parent and child process execute simultaneously.||Parent process remains suspended till child process completes its execution.
|Modification||If the child process alters any page in the address space, it is invisible to the parent process as the address space are separate.||If child process alters any page in the address space, it is visible to the parent process as they share the same address space.
|Copy-on-write||fork() uses copy-on-write as an alternative where the parent and child shares same pages until any one of them modifies the shared page.||vfork() does not use copy-on-write.|
Definition of fork()
The fork() is a system call use to create a new process. The new process created by the fork() call is the child process, of the process that invoked the fork() system call. The code of child process is identical to the code of its parent process. After the creation of child process, both process i.e. parent and child process start their execution from the next statement after fork() and both the processes get executed simultaneously.
The parent process and child process do have separate address space. Hence, when any of the processes modifies any statement or variable in the code. It would not be reflected in other process codes. Let’s suppose if child process modifies the code it would not affect the parent process.
Some child process after their creation immediately calls exec(). The exec() system call replaces the process with the program specified in its parameter. Then the separate address space of child process is of no use. The one alternative here is copy-on-write.
The copy-on-write let the parent and child process to share same address space. If the any of the processes writes on the pages in address space the copy of address space is created to let both the process work independently.
Definition of vfork()
The modified version of fork() is vfork(). The vfork() system call is also used to create a new process. Similar to the fork(), here also the new process created is the child process, of the process that invoked vfork(). The child process code is also identical to the parent process code. Here,the child process suspends the execution of parent process till it completes its execution as both the process share the same address space to use.
As the child and parent process shares the same address space. If any of the processes modifies the code, it is visible to the other process sharing the same pages. Let us suppose if the parent process alters the code; it will reflect in the code of child process.
As using vfork() does not create separate address spaces for child and parent processes. Hence, it must be implemented where the child process calls exec() immediately after its creation. So, there will be no wastage of address space, and it is the efficient way to create a process. vfork does not use copy-on-write.
Key Differences Between fork() and vfork()
- The primary difference between fork and vfork is that the child process created by the fork has a separate memory space from the parent process. However, the child process created by the vfork system call shares the same address space of its parent process.
- The child process created using fork execute simultaneously with the parent process. On the other hand, child process created using vfork suspend the execution of parent process till its execution is completed.
- As the memory space of parent and child process is separate modification done by any of the processes does not affect other’s pages. However, as the parent and child process shares the same memory address modification done by any process reflects in the address space.
- The system call fork() uses copy-on-write as an alternative, which let child and parent process share the same address space until any one of them modifies the pages. On the other hand, the vfork does not use copy-on-write.
The vfork() system call has to be implemented when child process call exec() immediately after its creation using fork(). As separate address space for child and parent process will be of no use here.