The execvp Function in C
-
Use
execvp
Function to Replace a Process Image in C -
Properly Handle the
execvp
Function Call Error Scenarios and Output Corresponding Messages -
Use
execvp
With thefork
Function to Create a Child Process and Execute Different Program in C
This article will demonstrate multiple methods about how to use the execvp
function in C.
Use execvp
Function to Replace a Process Image in C
In Unix-based systems, there are two separate system calls to create a new process and to load a new program code into a running process. The latter is done using the exec
family of library functions that are just different interfaces to the execve
system call. There are 6 different function prototypes: execlp
, execle
, execv
, execvp
and execvpe
. These functions take a file name or a pathname of a new program file to load and execute as the first argument. execvp
also takes an array of program arguments as the second argument.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
const char *args[] = {"vim", "/home/ben/tmp3.txt", NULL};
execvp("vim", args);
exit(EXIT_SUCCESS);
}
Properly Handle the execvp
Function Call Error Scenarios and Output Corresponding Messages
Note that, exec
family functions only return if an error occurs, so it’s important to implement error checking routines and handle the corresponding code paths as needed.
execvp
among them returns -1
on failure, and it also sets the errno
variable. Mind though, that errno
should be explicitly set to 0 before the function call and only check the value once the given call returns. The execvp
function can take filename without slashes, which implies that the file is searched in directories as specified by the PATH
environment variable.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
const char *args[] = {"vim", "/home/ben/tmp3.txt", NULL};
errno = 0;
if (execvp("vim", args) == -1) {
if (errno == EACCES)
printf("[ERROR] permission is denied for a file\n");
else
perror("execvp");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
Use execvp
With the fork
Function to Create a Child Process and Execute Different Program in C
Alternatively, suppose the user needs to create a new process and execute the given program code. In that case, we can utilize the fork
function call combined with execvp
. fork
duplicates the calling process and creates a new one called - the child process. In the following example, we implemented a custom function wrapper to create a new process and load/execute the given program code. Notice that once a child process is created, it executes a different code and the parent process waits until the child exits.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
pid_t spawnChild(const char* program, char** arg_list) {
pid_t ch_pid = fork();
if (ch_pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (ch_pid > 0) {
printf("spawn child with pid - %d\n", ch_pid);
return ch_pid;
} else {
execvp(program, arg_list);
perror("execve");
exit(EXIT_FAILURE);
}
}
int main(void) {
const char* args[] = {"vim", "/home/ben/tmp3.txt", NULL};
pid_t child;
int wstatus;
child = spawnChild("vim", args);
if (waitpid(child, &wstatus, WUNTRACED | WCONTINUED) == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook