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.
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 aNULL
pointer to indicate the end of the argument list. The first entry inargv
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.
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.
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.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups