There is a primary difference between fork and exec. The fork, on the one hand, generates new processes while simultaneously preserving its parent process. The exec, on the other hand, creates new processes but doesn’t preserve the parent process simultaneously.
Use of fork and exec
The computers operate in two major modes: the user mode and the kernel mode. In the case of kernel mode, any program can directly access the memory or the hardware resources. On the other hand, in the case of user mode, a program won’t be able to directly access the memory or the hardware resources.
Thus, kernel mode is comparatively more privileged. Whenever a program tries to access a resource or the memory, it simply uses a system call to send requests to the kernel. This way, the mode ultimately changes from user to kernel. But after the task is complete, then the mode will change back to its user mode. There are various system calls present in the UNIX system. Fork and exec are two of these. In this article, we will look into the difference between fork and exec. But before we do that, let us look at each of these individually.
What is fork()?
The term process refers to a program that is in its execution mode. Here, fork() acts as a system call that helps in creating processes. Thus, whenever a process makes a fork() call, then a copy of this process gets generated. The existing process is known as the parent process, and it creates new processes known as the child processes. However, note that a child process is the same as the parent process.
During the creation of a child process, the state of this parent process, like open files, address space, and the variables, get copied to the child process. Thus, any changes made to the parent process will not get reflected in its child process (it will also happen vice versa).
What is exec()?
The exec() also refers to a system call that helps in creating processes. Termination occurs in the currently running process once we make an exec() call. Then the newly created process replaces this parent process. In simpler words, only the new process will exist after one makes an exec() call; the parent process will ultimately be terminated. Also, this system call will lead the child process to replace the text segment, the address space, and the data segment of its parent process.
Difference between fork() and exec()
Let us talk about the differences between fork() and exec().
Parameters | fork() | exec() |
Basics | It is an operation used in the UNIX OS that lets a process create its copies that do not replace the original process. | It is also an operation used in the UNIX OS, but it creates child processes that replace the parent process or the previous process. |
Types of Processes | Both parent and child processes exist in the system after making this function call. | After calling the exec(), only the child process exists. No parent process is present since the child process replaces it. |
Result and Similarity | The child process created via the fork() system call is always similar to its parent process. They both coexist. | The child process created via the exec() system call replaces its parent process. |
Address Space | Since the parent and child process coexist after the fork() system call, they reside in different address spaces. | Since the child process replaces the parent process, it also replaces its address space. Thus, they both have the same address space in the system. |
Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Application Form, GATE Syllabus, GATE Cut off, GATE Previous Year Question Paper, and more.
Comments