exec family of functions in C

10 May, 2022
The exec family of functions replaces the current running process with a new process. It can be used to run a C program by using another C program. It comes under the header file unistd.h. There are many members in the exec family which are shown below with examples. 
execvp : Using this command, the created child process does not have to run the same program as the parent process does. The exec type system calls allow a process to run any program files, which include a binary executable or a shell script . 
Syntax: 
int execvp (const char *file, char *const argv[]);
file: points to the file name associated with the file being executed. argv:  is a null terminated array of character pointers.Let us see a small example to show how to use execvp() function in C. We will have two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by calling execvp() function in execDemo.c .
CPP
//EXEC.c #include<stdio.h>#include<unistd.h> int main(){    int i;         printf("I am EXEC.c called by execvp() ");    printf("\n");         return 0;}
Now,create an executable file of EXEC.c using command 
gcc EXEC.c -o EXEC
CPP
//execDemo.c #include<stdio.h>#include<stdlib.h>#include<unistd.h>int main(){        //A null terminated array of character        //pointers        char *args[]={"./EXEC",NULL};        execvp(args[0],args);             /*All statements are ignored after execvp() call as this whole        process(execDemo.c) is replaced by another process (EXEC.c)        */        printf("Ending-----");         return 0;}
Now, create an executable file of execDemo.c using command 
gcc execDemo.c -o execDemo
After running the executable file of execDemo.cby using command ./excDemo, we get the following output: 
I AM EXEC.c called by execvp()
When the file execDemo.c is compiled, as soon as the statement execvp(args[0],args) is executed, this very program is replaced by the program EXEC.c. “Ending—–” is not printed because as soon as the execvp() function is called, this program is replaced by the program EXEC.c.
execv : This is very similar to execvp() function in terms of syntax as well. The syntax of execv() is as shown below:
Syntax: 
int execv(const char *path, char *const argv[]);
path: should point to the path of the file being executed. argv[]: is a null terminated array of character pointers.Let us see a small example to show how to use execv() function in C. This example is similar to the example shown above for execvp() . We will have two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by calling execv() function in execDemo.c . 
CPP
//EXEC.c #include<stdio.h>#include<unistd.h> int main(){    int i;         printf("I am EXEC.c called by execv() ");    printf("\n");    return 0;}
Now,create an executable file of EXEC.c using command 
gcc EXEC.c -o EXEC
CPP
//execDemo.c #include<stdio.h>#include<stdlib.h>#include<unistd.h>int main(){        //A null terminated array of character        //pointers        char *args[]={"./EXEC",NULL};        execv(args[0],args);             /*All statements are ignored after execvp() call as this whole        process(execDemo.c) is replaced by another process (EXEC.c)        */        printf("Ending-----");         return 0;}
Now, create an executable file of execDemo.c using command 
gcc execDemo.c -o execDemo
After running the executable file of execDemo.c by using command ./excDemo, we get the following output: 
I AM EXEC.c called by execv()
execlp and execl : These two also serve the same purpose but the syntax of them are a bit different which is as shown below:
Syntax: 
int execlp(const char *file, const char *arg,.../* (char  *) NULL */);
int execl(const char *path, const char *arg,.../* (char  *) NULL */);
file:  file name associated with the file being executed const char *arg and ellipses : describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.The same C programs shown above can be executed with execlp() or execl() functions and they will perform the same task i.e. replacing the current process the with a new process.
execvpe and execle : These two also serve the same purpose but the syntax of them are a bit different from all the above members of exec family. The syntaxes of both of them are shown below : Syntax: 
int execvpe(const char *file, char *const argv[],char *const envp[]);

Syntax:
int execle(const char *path, const char *arg, .../*, (char *) NULL, 
char * const envp[] */);
The syntaxes above shown has one different argument from all the above exec members, i.e. char * const envp[]: allow the caller to specify the environment of the executed program via the argument envp. envp:This argument is an array of pointers to null-terminated strings and must be terminated by a null pointer. The other functions take the environment for the new process image from the external variable environ in the calling process.
Reference: exec(3) man pageThis article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
AakashkumarGoryan
ShubhamMaurya3
simmytarika5
surinderdawra388
C-Library
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Function Pointer in C
Different Methods to Reverse a String in C++
std::string class in C++
Unordered Sets in C++ Standard Template Library
Enumeration (or enum) in C
Memory Layout of C Programs
C Language Introduction
Power Function in C/C++
Command line arguments in C/C++