Difference Between fork() and exec() in Tabular Form

In this article, we’ll be discussing meaning of fork() and exec() in C and differences between them.

fork() and exec() both are system calls that are used to create and start a new processes.

The main difference between fork and exec is, fork() creates a new process by producing a duplicate of the current calling process, whereas, exec() replace the entire current calling process with a new program altogether.

Difference between fork() and exec() in Tabular form

ForkExec
The fork system call generates a duplicate copy of the calling processThe exec system call is used to replace the entire current calling process with a new program altogether
The new process created by a fork system call is identical to the parent process in almost all respectThe new program which replaces the current process is loaded and is run from the entry point
After the fork system call, both the child and parent processes will be executed simultaneouslyAfter the exec system call the control never get transferred to the original program until and unless an exec() error does not happen
Example, int process_id=fork();Example, int execvp (const char *file_name, char *const argv[]);

fork() System Call

fork() is a system call used which creates duplicate of the calling process and the new process is known as the child process. This child process runs concurrently along with the calling process which often referred to as the Parent process.

In other words, it creates an exact replica of existing process with a unique id. Suppose a process is running and one wants to create a exact copy of that particular process then fork() is used.

Negative Value: Unsuccessful child process creation.
Zero: Will be returned to the newly created child process.
Positive value: Will be returned to the parent or the caller process and the value will contain the process ID of the newly created child process.

Syntax:

int process_id=fork();

Program to show the working of fork() in C

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main() {
fork();
printf("\nProcess running with id : %d ",getpid());

return 0;
}

Output:

Process running with id : 21729 
Process running with id : 21730

exec() System Call

The exec family of system calls always replaces the executed program with a new process. At the point when a procedure calls exec, all codes (content) and information in the process are lost and exchanged with the executable of the new program.

In other words, it replaces an existing process entirely with new program specified in the parameter of the exec().

Suppose a process is running, when that particular process invokes exec() system call containing a program as a parameter, then it is going to completely replace the calling process with new process. This newly created process would have the same id.

Syntax:

int execvp (const char *file_name, char *const argv[]);

Program to show the working of execv() in C

We will be using execv() from the exec() family.

  • Create two files one.c and two.c.
  • one.c would be the replacing process
  • two.c will be calling  execv()
  • As a result,”one” will be going to replace “two”

two.c

#include<stdio.h> 
#include<unistd.h> 
#include<sys/types.h> 
int main() { 
    char *args[]={"./one",NULL}; 
printf("\n Current Process running with id : %d \n",getpid());
        execv(args[0],args); 
       printf("Calling ");
 return 0; 
}

one.c

#include<stdio.h> 
#include<unistd.h> 
#include<sys/types.h> 
int main() { 
printf("\n New Process running with id : %d \n",getpid());
 return 0; 
}

Commands:

 gcc one.c -o one
 gcc two.c -o two
./two


Output:

Current Process running with id : 26515

New Process running with id : 26515

Leave a Reply