程序清单 8-8 exec函数实例,a.out是程序8-9产生的可执行程序
1 /* 2 ============================================================================ 3 Name : test.c 4 Author : blank 5 Version : 6 Copyright : Your copyright notice 7 Description : 程序清单 8-8 exec函数实例,a.out是程序8-9产生的可执行程序============================================================================ 8 */ 9 10 #include "ourhdr.h" 11 #include <sys/wait.h> 12 13 char *evn_init[] = { "USER=unknown", "PATH=/tmp", NULL}; 14 int main(int argc, char *argv[]) 15 { 16 pid_t pid; 17 18 if ((pid = fork()) < 0){ 19 err_sys("fork error"); 20 }else if (pid == 0){ 21 // specify pathname, specify environment 22 if (execle("/home/blank/workspace/test/src/a.out", "a.out", "myarg1", 23 "MY ARG2", (char *)0, evn_init) < 0){ 24 err_sys("execel error"); 25 } 26 } 27 28 if (waitpid(pid, NULL, 0) < 0){ 29 err_sys("waitpid error"); 30 } 31 32 if ((pid = fork()) < 0){ 33 err_sys("fork error"); 34 }else if (pid == 0){ 35 // specify filename, inherit environment 36 if (execlp("./a.out", "a.out", "only 1 arg", (char *)0) < 0){ 37 err_sys("execlp error"); 38 } 39 } 40 41 exit(0); 42 }