Javatpoint Logo
Javatpoint Logo

Difference between fork() and vfork()

Both fork() and vfork() are system calls that make a new process which is similar to the process called fork() or vfork(). The use of fork() allows the execution of both processes at the same. On the other side, the vfork() system calls to suspend the parent process's execution until the child process accomplishes its execution process.

In this article, you will learn about the difference between the fork() and vfork() functions. But before discussing the differences, you must know about the fork() and vfork() functions.

What is a fork()?

The fork() system call is mainly utilized to make a new process. The child process is the new process generated by the fork() system call and the parent process is the process triggered by the fork() system call. The child process code is similar to the code of its parent process. When a child process is formed, both the parent and child processes begin execution from the next statement following fork(), and both processes are executed concurrently.

There is a different address space for the parent and child processes. As a result, whenever one of the processes alters a statement or variable in the code, the other process's codes would not reflect this. For example, if a child's process changes the code, it would not affect the parent's process.

Some child processes after their creation may immediately call the exec() function. The system function exec() replaces the present process with the program defined in its parameter. After that, the child process's different address space is useless and contains a copy-on-write function. The copy-on-write function enables both the parent and child processes to transmit the same address space. If each process writes to the address space's pages, a duplicate of the address space is created to enable both processes to operate independently.

What is vfork()?

The vfork() is another system call that is utilized to make a new process. The child process is the new process formed by the vfork() system call, while the parent process is the process that uses the vfork() system call. The child process's code is comparable to that of its parent process. Because the child and parent processes share the same address space, the child process halts the parent process's execution until the child process completes its execution.

The child and parent both processes transmit the same address space. If one of the processes changes the code, it is visual to the other process transferring the same pages. Assume that the parent process modifies the code; it will be reflected in the child process code.

The vfork() system call doesn't create separate address spaces for both parent and child processes. As a result, it should be applied where the child process applies the exec() function immediately after its creation. So, no address space will be wasted, which is the most efficient method to create a process. The vfork() system call does not support copy-on-write.

Key differences between fork() and vfork()

Difference between fork() and vfork()

There are various key differences between the fork() and vfork(). Some main differences between fork() and vfork() are as follows:

  1. The memory space of the child process formed by the fork differs from the parent process. In contrast, the child process formed by the vfork() system call uses the same address space as its parent process.
  2. As the memory space of the parent and child processes is independent, any modification made by one of the processes doesn't affect the pages of the other processes. On the other hand, as the parent and child processes both share the same memory address, any alteration made by any process is reflected in the address space.
  3. When a child process modifies a page in the address space, it is not visible to the parent process because the address spaces are different. In contrast, when a child process changes a page in the address space, the parent process is visible because they use the same address space.
  4. The child process created by the fork() system call runs concurrently with the parent process. In contrast, a child process formed by the vfork() system calls suspends the parent process's execution until it is completed.
  5. As an alternative, the fork() system call employs copy-on-write, allowing the child and parent to share the same address space until one of them updates the pages. In contrast, the vfork() system call doesn't employ copy-on-write.
  6. In the fork() system call, the child process does not suspend the parent process's execution. In contrast, the vfork() system call suspends the parent process execution.
  7. There is address space waste in the fork() system call. In contrast, there is no address space waste in the vfork() system call.
  8. The fork() system call is highly used in the processes. In contrast, the vfork() system call is less used.

Head-to-head comparison between fork() and vfork System Call

Here, you will learn about the head-to-head comparison between the fork() and vfork() system calls. Some main differences between the fork() and vfork() system call are as follows:

Features fork() vfork()
Basic In fork() system call, both processes have separate address spaces. In the vfork() system call, both child and parent processes use the same address space.
Execution In fork(), both child and parent processes execute simultaneously. In the vfork(), the parent process remains halted until the child process accomplishes its execution.
Copy-on-write As an alternative, the fork() system call employs copy-on-write, allowing the child and parent to share the same address space until one of them updates the pages. The vfork() system call does not employ copy-on-write.
Modification When a child's process alters a page in the address space, it is not visual to the parent process because the address spaces are different. When a child's process alters a page in the address space, the parent process is visual because they use the same address space.
Wastage There is address space waste in the fork() system call. There is no address space waste in the vfork() system call.
Used The fork() system call is highly used in the processes. The vfork() is less used.
Suspends In the fork() system call,s the child process does not suspend the parent process's execution. The vfork() system call stops the parent process execution.

Conclusion

The fork() and vfork() functions both are the system calls. Both systems formed a new process similar to the process called fork() or vfork(). When a child process is formed, both the parent and child processes begin execution from the next statement following fork(), and both processes are executed concurrently. In contrast, in the vfork() system call, the child process pauses the parent process execution until the child process accomplishes its execution because both child and parent processes use the same address space.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA