The fork() and exec() are the system calls that are used for controlling processes. Both are used to create a new process where the process is the program in execution. The fork() system call when invoked by any process creates a new duplicate child process of the invoking process.
However, the exec() system call when invoked by any process replaces the invoking process along with all its threads by the process specified to it in its parameter.
In the section ahead we will discuss a few more differences between fork() and exec(). Along with that, we will also discuss both the terms separately in order to get a brief idea of both the system calls.
Content: fork() Vs exec() System Call
- Difference Chart
- What is the fork() System Call?
- What is exec() System Call?
- Key Takeaways
- Conclusion
Difference Chart
Factors for Differentiation | fork() | exec() |
---|---|---|
Invoking | fork() creates a new duplicate child process of the process that invoked fork() | exec() replaces a process that invokes it with a new process provided in its parameter. |
Process id | The child process and parent process have unique process id. | The new process and the replaced process have the same process id. |
Execution | The parent and child process start simultaneous execution from the instruction just after fork(). | The currently running process is terminated and the exec() start execution of the new process from its entry point. |
Arguments | No arguments are passed to fork() system call. | Basically, three or more arguments are passed to the exec() system call. |
Format | Pid=fork(); | exec(cont char *filename, char* const argv[], char* const envp[]) |
What is the fork() System Call?
The fork() system call is a process controlling system call. The fork() system call when invoked by any process creates a separate new process which is a duplicate of the process invoking the fork() system call. However, the process invoking the fork() system call is termed as the parent process and the new duplicate process created by the fork() is termed to as a child process.
After fork() creates a duplicate child process both the parent and child process resumes their execution from the next instruction after fork() simultaneously. Both the parent process and child process have their unique process id. we can illustrate this with the help of an example below:
As soon as the fork() is executed one child process is created that is duplicate of the parent process. Now, both parent and child process will execute the printf() statement simultaneously. Where once the parent process id will get printed and once the child process id will get printed.
If the fork has successfully created a child then the value its return would be 0 and if the fork() execution failed then it returns a negative value. For the parent process, fork() returns the process identifier of its child process.
To understand the working of fork() system call observe the code below:
main(){ fork(); printf(“\n hello world”); }
In the figure above you can see that as soon as the process P1 invokes the fork() instruction a child process CP1 is created and both child and parent process resume their execution from the next instruction after fork(). So we can see that the ‘printf()’ statement after fork() instruction is being executed two times once by the parent process and once by the child process.
Now what if we have fork() instruction is two times in the code above i.e.
main(){ fork(); fork(); printf(“\n hello world”); }
In this case, execution would take place in this manner.
So, seeing the number of fork() system calls in the code you can calculate the number of child processes that would be created by calculating 2n-1, where n is the number of fork() system call in the code.
What is exec() System Call?
Typically exec() system call is used to replace the currently executing process along with all its threads with the program provided to it in its parameter. The exec() system call is generally invoked after the fork() system call, either by parent or by the child process.
There are most of the cases where the child needs to execute the different program like that of the parent. So, by invoking the exec() system call a new program starts its execution in the context of the existing program.
Now, consider we have two program files prog1 and prog2. We will start executing prog1 and we will see how we can use exec() system call to replace the prog1 with prog2. Below you have the code for prog1.c.
The figure below shows the code for prog2.c.
On executing prog1.c we get the result as you can see below:
You can notice that the control of the program has not gone back to prog1.c after the execution of exec(). The only condition where the program control goes back to the invoking program is an error in the execution of exec().
The process invoking exec() must provide the environment for the new program. Like the name of the file or program to be executed, a pointer to the argument array, and finally a pointer to the environment array.
There are few versions of exec() system call including execl, execv, execle, and execve, that allow the parameters to be omitted or specified in various other ways.
Key Takeaways
- Both fork() and exec() being the system call to create a new process are different in a way that the fork() system call is used to create a new duplicate process of the process that invoked the fork() system call. However, the exec() replaces the memory space of the invoking process by a new process.
- In case of fork() system call both the invoking process i.e. parent process and the newly created process i.e. the child process has a different process identifier. Although in the case of exec() the invoking process and the newly created process that has replaced the invoking process shares the same process identifier as the newly created process is executed in the context of the invoking process.
- Once the fork() creates a child to a parent process both starts there execution simultaneously after the fork() instruction. On the other hand, once the exec() replaces the existing process by a new process, only the new process is in execution and control never goes back to the original process until there is an error in the execution of exec().
- No arguments are supplied to the fork() system call whereas the exec() system call has three basic parameters file name, point to argument array, a pointer to environment array.
Conclusion
So, we have discussed both fork() and exec() system call where both are used to create a new process in the system and we have also discussed how do they differ from each other in various ways. You can make use of exec() system call after the execution of fork() system call just to make parent and child process to go their separate way.
Leave a Reply