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 process's imageA process image is the executable file of the program that is currently loaded into the main memory for execution. with another process's image that we have specified in the function.

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[])
execvp() function syntax

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 -l for the command ps -l.

Note: The last string of the ArgumentVector parameter should be NULL eg. {"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);
    }
}
execvp() C code example

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 process ps -l.

  • Line 12: We call the execvp() function, and pass the path ps and 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 -1 via the exit() 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.

1

What does the execvp() function return on a successful execution?

A)

0

B)

1

C)

-1

D)

None of the above!

Question 1 of 40 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved