Difference between fork() and exec()

The computer system has two modes: kernel mode and user mode. In kernel mode, a program may directly access memory and hardware resources. In user mode, programs may not directly access hardware and memory resources. As a result, kernel mode is more privileged than user mode. UNIX systems use a variety of system calls. Fork and exec are two of them. In this article, you will learn the difference between the fork() and exec(). But before discussing the differences, you must know about the fork and exec().

What is fork()?

The fork () system calls aids in the creation of processes. When a process uses the fork() system call, it creates a replicate of itself. The parent process is the existing process, and the child process is the new process. Although, the child process is equivalent to the parent process. When creating the child process, the parent state like open files, address space, and variables are copied to the child process. In other words, the child and parent processes are located in separate physical address spaces. As a result, the modification in the parent process doesn't appear in the child process.

What is exec()?

The exec() system call is used to make the processes. When the exec() function is used, the currently running process is terminated and replaced with the newly formed process. In other words, only the new process persists after calling exec(). The parent process is shut down. This system call also substitutes the parent process's text segment, address space, and data segment with the child process.

Main differences between the fork() and exec()

Difference between fork() and exec()

Here, you will learn the main differences between the fork() and exec(). Various differences between the fork() and exec() are as follows:

  1. In a UNIX operating system, the fork is a command that allows a process to copy itself. However, in a UNIX operating system, exec is a command that creates a new process by replacing the existing one.
  2. The fork() makes a child's process equal to the parent's process. On the other hand, the exec() makes a child process and replaces it with the parent process.
  3. After invoking the fork(), there is a child process and a parent process. In contrast, there is only a child process after invoking the exec().
  4. The parent and child processes are in separate address spaces in the fork(). On the other hand, the child address space replaces the parent address space in the exec().

Head-to-head comparison between the fork() and exec()

Here, you will learn the head-to-head comparison between the fork() and exec(). Various head-to-head comparisons between the fork() and exec() are as follows:

FeaturesFork()Exec()
DefinitionIt is a command that allows a process to copy itself.It is a command that makes a new process by replacing the existing process.
Address SpaceThe parent and child processes are in separate address spaces in the fork().The child address space replaces the parent address space in the exec().
Parent ProcessThere is a child process and a parent process after calling the fork() function.There is only a child process after calling the exec().
ResultThe fork() makes a child's process equal to the parent's process.The exec() makes a child process and replaces it with the parent process.