execvp, is a POSIX specified function from the exec* family, which replaces the current process with one specified. Questions using this tag should be related to the use and semantics of this function.
execvp is a POSIX function, from the exec*
family, which replaces the current process with one specified.
It is available on most every UNIX-like platform, including Linux and Mac OS X, where a unistd.h
file exists, and many languates, such as Python, emulate this function on platforms, such as Windows, where this function does not normally exist. Many languages, including C, C++, and Python, have interfaces to this function.
If it succeeds, it will not return and the current process will be replaced. If it does not succeed, then it will return a -1.
POSIX specifies that this function has the signature:
int execvp(const char *path, char *const argv[]);
.
Its arguments are interpreted as follows.:
path: This is a NULL terminated string which holds the name of an executable. It is specified that execvp will search the PATH environment variable for this name if it is not a path.
If it is a path, then execvp will look for the executable where specified, using an absolute path if this argument begins with a
/
, or a relative one if this argument doesn't.
Example: ping
will resolve to /bin/ping
.
argv: This is an array of NULL terminated strings, which make up the
argv
of the process in thepath
argument.This array should end with a NULL, and should start with the name of the executable passed in the
path
argument.
Example: {"ping", "-c", "5", "www.google.com", NULL}
POSIX includes the following complete example for reference:
The following example illustrates the use of execvp to execute the
ls
shell command:#include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> main() { pid_t pid; char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL}; if ((pid = fork()) == -1) perror("fork() error"); else if (pid == 0) { execvp("ls", parmList); printf("Return not expected. Must be an execvp() error.\n"); } }