#include <unistd.h> int execve(const char *file, char *const argv[], char *const envp[]);
exec
functions,
execve
replaces the calling process image with a new process
image. This has the effect of running a new program with the process
ID of the calling process. Note that a new process is not started;
the new process image simply overlays the original process image.
The execve
function is most commonly used to overlay a
process image that has been created by a call to the fork
function.
file
argv
NULL
pointer is used to mark the end of the
array. Each character string pointed to by the array
is used to pass an argument to the new process image. The
first argument, argv[0]
,
is required and must contain the name of the executable
file for the new process image.
envp
NULL
pointer is used to mark the end of the
array. Each character string pointed to by the array
is used to pass an environment
variable to the new process image.
execve
does not have a return value
because the new process image overlays the calling process
image. However,
a -1
is returned if the call to execve
is unsuccessful.
execve
to execute the ls
shell command. Notice that the
STEPLIB
environment variable is set for the new process.
#include <sys/types.h> #include <unistd.h> #include <stdio.h> main() { pid_t pid; char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL}; char *const envParms[2] = {"STEPLIB=SASC.V6.LINKLIB", NULL}; if ((pid = fork()) ==-1) perror("fork error"); else if (pid == 0) { execve("/u/userid/bin/newShell", parmList, envParms); printf("Return not expected. Must be an execve error.n"); } }
execv
, execvp
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.