exec family of functions in C
Last Updated :
21 May, 2025
The exec family of functions is defined in the unistd.h header file. The exec functions are used to replace the current running process with a new process. It can be used to run a C program by using another C program.
Exec functions in C
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:
C
int execvp (const char *file, char *const argv[]);
where,
- file: points to the file name associated with the file being executed.
- argv: is a null terminated array of character pointers.
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
C
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
C
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:
C
int execv(const char *path, char *const argv[]);
Where,
- 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
C
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
C
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:
C
int execlp(const char *file, const char *arg,...
(char *) NULL);
int execl(const char *path, const char *arg,...
(char *) NULL);
Where,
- file: file name associated with the file being executed
- const char *argand 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.
Difference between execlp and execl:
- execl() requires the full path of the program to be specified.
- execlp() only requires the program name and will search for it in the directories listed in the PATH environment variable which can be useful if the exact location of the program is unknown.
execvpe and execle
These are also part of exec family of functions and they 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:
C
int execvpe(const char *file, char *const argv[],char *const envp[]);
int execle(const char *path, const char *arg, ..., (char *) NULL,
char * const envp[] );
Where,
- 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.
Difference between execvpe()
and execle()
- execvpe() searches for the program in directories listed in the PATH environment variable where as in execle() you must specify the full path to the program.
- execvpe() takes an array of arguments (argv[]) while execle() accepts a variable list of arguments, which is terminated by NULL.
Similar Reads
fabs() Function in C
fabs() function of math.h header file in C programming is used to get the absolute value of a floating point number. This function returns the absolute value in double. Syntax: double fabs (double a); Parameter: It will take a single parameter which is to be converted to an absolute value. Return Va
2 min read
expm1, expm1f, expm1l Functions in C
expm1(), expm1f(), expm1l() were introduced with C99, and are available under the <math.h> header file. These are used to calculate the Euler's Number, e (2.7182818) raised to a power equal to the provided parameter minus 1.0, i.e ex - 1. expm1(x), expm1f(x), exmp1l(x) = ex - 1 Example: double
2 min read
clock() function in C
The clock() function in C returns the approximate processor time that is consumed by the program which is the number of clock ticks used by the program since the program started. The clock() time depends upon how the operating system allocates resources to the process that's why clock() time may be
2 min read
exp2() function in C++ STL
The exp2() is a builtin function in C++ STL that computes the base-2 exponential function of a given number. It can also be written as 2num. Syntax: exp2(data_type num) Parameter: The function accepts a single mandatory parameter num which specifies the value of the exponent. It can be positive, neg
2 min read
User-Defined Function in C
A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no hea
6 min read
Types of User Defined Functions in C
A user-defined function is one that is defined by the user when writing any program, as we do not have library functions that have predefined definitions. To meet the specific requirements of the user, the user has to develop his or her own functions. Such functions must be defined properly by the u
4 min read
Functions in C++
A function is a building block of C++ programs that contains a set of statements which are executed when the functions is called. It can take some input data, performs the given task, and return some result. A function can be called from anywhere in the program and any number of times increasing the
9 min read
fclose() Function in C
In C language, fclose() is a standard library function used to close a file that was previously opened using fopen(). This function is the itegral part of the file handling in C which allows the users to free the memory occupied by the file once all the operations are done on it.In this article, we
2 min read
__builtin_inf() functions of GCC compiler
These functions do not need to include any header file to use them. So it provides faster usage, as these are Built-in functions of GCC compiler which is most used compiler, even in competitive programming. Note: If the compiler shows warning of overflow, use casting before __builtin_inf() function
4 min read
ldexp() function in C/C++
The ldexp() is a built-in function in C/C++ gives the product of any variable x and 2 raised to the power of exp by taking two arguments x and exp i.e., x * 2^exp Syntax: float ldexp (float x, int exp) double ldexp (double x, int exp) long double ldexp (long double x, int exp) double ldexp (T x, int
2 min read