What are the Fork and Exec system call? / What is fork () Vfork () and exec ()?

Hello Friends, In this blog post we are going to discuss Fork and Exec system call. In UNIX, each process is identified by its process identifier, which is a unique integer. A new process is created by the fork system call. The new process consists of a copy of the address space of the original process.

What is fork () vfork () and exec ()?

A fork system call is used for creating a new process. This new process becomes the child process. And once this new child process is created then both the process parent and child process execute the next instruction as per the fork() system call.

Vfork is differ from the fork system call in the context where vfork creates the new child process and then the parent process gets suspended temporarily. And this child process also takes or borrow the address space of the parent process.

Exec() system call replaces the current process with a new executable process or program.

This mechanism allows the parent process to communicate easily with its child process. Both processes continue execution at the instruction after the fork system call, with one difference ——-

The return code for the fork system call is zero for the new (child) process, whereas the (nonzero) process identifier of the child is returned to the parent.

Typically the exec system call is used after a fork system call by one of the two processes to replace the process memory space with a new program.

In the multithreaded program, the semantics of the fork and exec system call change. If one thread in a program calls fork, does the new process duplicate all threads or is the new process single-threaded?

Some Unix system has chosen to have two versions of the fork, one that duplicates all threads and another that duplicates only the thread that invoked the fork system call.

If a thread invokes the system call, the program specified in the parameter to exec will replace the entire process- including all threads and LWPs.

The usage of the two versions of fork depends upon the application. If exec is called immediately after forking, then duplicating all threads is unnecessary, as the program specified in the parameters to exec will replace the process.

In this instance, duplicating only the calling thread is appropriate. if however, the separate process does not call exec after forking, the separate process should duplicate all threads.

In the case of any queries, you can write to us at [email protected] we will get back to you ASAP.

Hope! you would have enjoyed this post about the Fork() and Exec() system call.

Please feel free to give your important feedbacks in the comment section below.

Have a great time! Sayonara!

Anurag

I am a blogger by passion, a software engineer by profession, a singer by consideration and rest of things that I do is for my destination.