/>
exec: Used to start execution of new program from another program, e.g., execl, execv, execls, execlp and execvp
Successful exec call will overlay second program over first, leaving only second program in memory No return from successful exec call ^^^^^^^^^ Concept of parent-child doesn’t hold here Each exec call is followed by test to ensure successful completion![]()
exec - Invoke subprocess(es) execl execle execlp execv execve execvp - Each of the functions in the exec family replaces the current process image with a new process image. - The new image is constructed from a regular, executable file called the new process image file. - There is no return from a successful call to one of these functions because the calling process image is overlaid by the new process image. execl... pass the command line args in an explicit list use if you know what the arguments will be when coding execv... pass the command line args in an argument array
jupiter|/export/home/wyatt/private/code/Opsys/Exec$ cat execHello.cpp #include <unistd.h> using namespace std; int main (void) { execl("hello", NULL); return 0; } ===================================================== jupiter|/export/home/wyatt/private/code/Opsys/Exec$ cat hello.cpp #include <iostream> using namespace std; int main (void) { cout << "Hello World" << endl; return 0; }
// execl (execute and leave) ========= execl(char *path, char *arg0,...,char *argn, 0); where path points to name of a file holding a command to be executed, arg0 points to a string that is the same as path (or at least its last component). arg1 ... argn are pointers to arguments for the command The last parameter must always be 0. It is a NULL terminator, since the argument list is variable. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main(void) { pid_t childpid; childpid = fork(); if (childpid == -1) { // fork error perror("Failed to fork"); return 1; } if (childpid == 0) { // CHILD code - run another program in this process space execl("/bin/ls", "ls", "-l", NULL); // will only get here on exec fail perror("Child failed to exec ls"); return 1; } if (childpid != wait(NULL)) { // PARENT code - will only get here if pids do not match perror("Parent failed to wait due to signal or error"); return 1; } return 0; }
// execv ========= This is the same as execl() except that the arguments are passed as null terminated array of pointers to char. The first argument is a character string that contains the name of a file to be executed. The second argument is a pointer to an array of character strings. More precisely, its type is char **, which is exactly identical to the argv array used in the main program: #include <errno.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include "restart.h" int main(int argc, char *argv[]) { pid_t childpid; if (argc < 2) { /* check for valid number of command-line arguments */ fprintf (stderr, "Usage: %s command arg1 arg2 ...\n", argv[0]); return 1; } childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid == 0) /* child code */ { execvp(argv[1], &argv[1]); perror("Child failed to execvp the command"); return 1; } if (childpid != r_wait(NULL)) /* parent code */ { perror("Parent failed to wait"); return 1; } return 0; }