The main difference between fork and exec is that fork creates a new process while preserving the parent process, but exec creates a new process without preserving the parent process.
A computer operates in two modes: the kernel mode and user mode. In kernel mode, a program can access memory or hardware resources directly. In user mode, the programs cannot directly access memory or hardware resources. Therefore, kernel mode is a privileged mode than user mode. When a program needs to access memory or a resource, it sends a request to the kernel via a system call. Then the mode changes from user mode to kernel mode. After completing the task, the mode changes back to the user mode. UNIX systems have various system calls. Two of them are fork and exec.
Key Areas Covered
exec, fork, Kernel mode, System Call, UNIX
What is fork
A process is a program in execution. The fork () is a system call that helps to create processes. When a process makes fork() call, a copy of the process is created. The existing process is the parent process, while the new process is the child process. However, this child process is similar to the parent process.
When creating the child process, the state of the parent such as variables, open files and address space is copied to the child process. In other words, the parent and child processes are in different physical address spaces. Therefore, the changes made in the parent process are not reflected in the child process and vice versa.
What is exec
The exec() system call is also used to create processes. When exec() is called, the currently running process terminates and is replaced with the newly created process. In other words, after making an exec() call, only the new process exists. The parent process is terminated. Furthermore, this system call replaces the address space, text segment and data segment of the parent process with the child process.
Difference Between fork and exec
fork is an operation in a UNIX operating system that allows a process to create a copy of itself. But, exec is an operation in a UNIX operating system that creates a process by replacing the previous process. Thus, this explains the main difference between fork and exec.
After calling fork(), there is parent process and child process. On the other hand, after calling exec(), there is only a child process, and there is no parent process. Hence, this is another difference between fork and exec.
Moreover, fork creates a child process which is similar to the parent process, while exec creates a child process and replace it with the parent process.
Furthermore, one other difference between fork and exec is in their address space. In fork(), the parent and the child processes are in different address spaces whereas, in exec(), the child address space replaces the parent address space.
In brief, there are various system calls available in the UNIX operating system, and two of them are fork and exec. The main difference between fork and exec is that fork creates a new process while preserving the parent process while exec creates a new process without preserving the parent process.
1.”Version 7 Unix” By Huihermit – Own work (CC0) via Commons Wikimedia