With execvp, a program can initiate system commands, such as starting ap­plic­a­tions 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 re­start­ing 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, pipelin­ing and re­dir­ec­tions. It sig­ni­fic­antly enhances the flex­ib­il­ity of C programs.

What is the syntax of execvp?

The execvp function needs two para­met­ers: 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 con­tain­ing 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 pro­gram­ming that contains defin­i­tions and de­clar­a­tions of functions for in­ter­act­ing 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 suc­cess­ful, the current process will be replaced by the external program, and the sub­sequent 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
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included
Go to Main Menu