What is the Difference Between fork and exec

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

1. What is fork
     – Definition, Functionality
2. What is exec
     – Definition, Functionality
3. Difference Between fork and exec
     – Comparison of Key Differences

Key Terms

exec, fork, Kernel mode, System Call, UNIX

Difference Between fork and exec - Comparison Summary

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.

Difference Between fork and exec

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

Definition

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.

Parent process

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.

Result

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.

Address Space

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.

Conclusion

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.

References:

1.“Introduction to System Calls.” Studytonight, Available here.
2.“Fork (System Call).” Wikipedia, Wikimedia Foundation, 16 Mar. 2019, Available here.

Image Courtesy:

1.”Version 7 Unix” By Huihermit – Own work (CC0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply