How to use execvp() in C and C++

With execvp, a program can initiate system commands, such as starting applications or executing system services. When combined with the fork() function, code can also be called after execvp.

How does execvp work?

The main purpose of execvp is to allow one program to control another without restarting the entire process. This enables seamless switching between different functions or the execution of external commands with variable arguments. execvp acts like an invisible director, changing the scenery and jumping back and forth between different storylines.

Dynamic process execution allows you to adjust the program path and its arguments during runtime. The execvp() function is used in system calls that require complex tasks such as script execution, system commands, pipelining and redirections. It significantly enhances the flexibility of C programs.

What is the syntax of execvp?

The execvp function needs two parameters: the file path or name of the program you want to execute and an array of strings with the arguments for that program.

#include <unistd.h>
int execvp(const char *command, char* argv[]);
c
  • const char *command: This is the file path or the name of the program you want to execute. It can be an absolute path or a relative path. If a relative path is used, execvp searches for the file in the system PATH.
  • char *argv[]: An array of strings containing the arguments for the program to be executed. The array has to end with a NULL pointer to indicate the end of the argument list. The first entry in argv is usually the name of the program itself, followed by the arguments.

The execvp function and other functions from the exec family are specific to Unix-based operating systems. The #include <unistd.h> statement is a header file in C programming that contains definitions and declarations of functions for interacting with a Unix-based operating system and process control. You will encounter this file a lot when learning to write code in C.

Example of how to use execvp

In this example below, we are going to use the execvp() function from the unistd.h header file to start the external program ls with the arguments -l and /usr/bin. The array args stands for the arguments of the program. If the execvp() function is successful, the current process will be replaced by the external program, and the subsequent lines will be ignored. In the event of an error, an error message will be displayed via perror and the program will return the status code 1.

#include <unistd.h>
int main() {
    char *args[] = {"ls", "-l", "/usr/bin", NULL};
    execvp("ls", args);
    perror("execvp");
    return 1;
}
c

Using fork(), you can create a new process. In this child process, you can run another program using execvp. This allows the parent process to continue to execute its own code, while the new process starts the external program.

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
    char* command = "ls";
    char *args[] = {"ls", "-l", "/usr/bin", NULL};
    printf("Before calling execvp()\n");
    pid_t child_pid = fork();
    if (child_pid == -1) {
        // Error creating the process
        perror("fork");
        return 1;
    }
    if (child_pid == 0) {
        // Code executed in the child process
        // Call execvp in the child process to execute "ls" with the specified arguments
        int status_code = execvp(command, args);
        // This line is reached if execvp encounters an error
        perror("execvp");
        // Print statement after execvp
        printf("ls -l /usr/bin has taken control of this child process. If this is printed, execvp encountered
       an error.\n");
        // Error handling in the child process
        return 1;
    } else {
        // Code executed in the parent process
        // Wait for the completion of the child process
        waitpid(child_pid, NULL, 0);
        printf("The child process has completed its execution.\n");
    }
    return 0;
}
c

In the example above, we created a new process using fork().Using the execvp() function, ls takes over the child process with its arguments. With waitpid, the parent process waits for the child process to complete and then outputs the message.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top