What is the execvp() function in C?
The execvp() function belongs to the exec family of functions. It is provided by the unistd.h library, which we need to include in our C programs to use the function. The execvp() function is used when we want to replace the current
Let's take the example of a case where we are making a custom command shell for our computer system. In the shell, there should be some way to execute different commands without the actual shell program getting terminated. Here, we can use the execvp() function to execute external commands in our custom shell by creating a child process via the fork() system call and replace its image with the external command via the execvp() function.
Syntax
Below, we can see the syntax for the function:
int execvp (const char *File, char *const ArgumentVector[])
The function takes in two arguments which are discussed below.
File: Here, we specify the binary file/program path we want to execute. The path can either be the absolute path or the relative path.ArgumentVector: This is an array of strings representing the arguments we want to pass to the replacement program. The arguments can be additional parameters, such as-lfor the commandps -l.
Note: The last string of the
ArgumentVectorparameter should beNULLeg.{"ps", "-l", "NULL"}
Return value
When the execvp() function executes successfully, it doesn't return a value. This is so because the process image of the current process that called execvp() is replaced by the one specified in the function. So any lines of code written after the execvp() function would not be executed after successful execution.
However, if there is an error while running the execvp() function, it returns the value of -1, which can be used in error handling.
Code example
Now that we have gone through the syntax for the function, let's look at the code example below that shows us how to use the execvp() function in C programs.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char* argv[]){
int cpid = fork();
if(cpid == 0){
printf("\nChild: I have the PID: %d, I am going to call exec!\n", getpid());
char* argumentVector[]= {"ps", "-l", NULL};
int flag = execvp(argumentVector[0], argumentVector);
printf("\nError!\n");
exit(-1);
}
else{
printf("\nParent: I have the PID: %d, I am going to wait for child!\n", getpid());
wait(NULL);
printf("\nParent: Child exitted!\n");
exit(0);
}
}When we run the program, we see that a child process is created via the fork() system call, and the child process's image is replaced with the program ps -l, which we have specified in the execvp() function.
Code explanation
Line 7: We create a child process via the
fork()system call.Lines 9–14: Here, we write the code for the child process whose image we want to replace with the one in the
execvp()function.Line 11: We create a 2D char array and initialize it with the strings
{"ps", "-l", NULL}which specifies that we want to replace the current process with the processps -l.Line 12: We call the
execvp()function, and pass the pathpsand the argument vector we made in line 11 as its parameters.Lines 13–14: We check for errors during the function call. If there was an error, these lines get executed, and the child process exits with a status of
-1via theexit()system call.Lines 16–21: This portion represents the code for the parent process in which we wait for the child to exit via the
wait()system call.
Practice quiz
Now that we have gone through the syntax and understood how the function works via a C program, we can test what we learned by solving the quiz below.
What does the execvp() function return on a successful execution?
0
1
-1
None of the above!
Free Resources