Open In App

exec family of functions in C

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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
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 

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
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 

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.

Next Article
Article Tags :

Similar Reads