UNIX环境高级编程8.10exec函数
2015-02-06 16:05
351 查看
// proc/exec1.c 8-8 #include "apue.h" #include <sys/wait.h> const char* env_init[] = { "USER=unknown", "PATH=/home/sunyj/apue/proc/", NULL }; int main(void) { pid_t pid; if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { /* specify pathname, specify environment */ if (execle("/home/sunyj/apue/proc/echoall", "echoall", "myarg1", "MY ARG2", (char *)0, env_init) < 0) { err_sys("execle error"); } } if (waitpid(pid, NULL, 0) < 0) { err_sys("wait error"); } if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { /* specify filename, inherit environment */ if (execlp("echoall", "echoall", "only 1 arg", (char *)0) < 0) { err_sys("execlp error"); } } return 0; }
// proc/echoall.c 8-9 #include "apue.h" int main(int argc, char *argv[]) { int i; char **ptr; extern char **environ; for (i = 0; i < argc; i++) /* echo all command-line args */ { printf("argv[%d]: %s\n", i, argv[i]); } for (ptr = environ; *ptr != 0; ptr++) /* and all env strings */ { printf("%s\n", *ptr); } return 0; }
我测试的结果是,execle可以正确执行,execlp报找不到echoall的错误,除非,我将echoall的路径在shell中加入环境变量PATH中,我不清楚是为什么。
相关文章推荐
- 进程编程3 - UNIX高级环境编程第9章读书笔记
- 进程编程1 – Unix环境高级编程7章读书笔记
- Unix环境高级程序设计入门----文件系统的相关编程(上)
- Unix环境高级编程第三章文件I/O摘记
- 进程编程1 – Unix环境高级编程7章读书笔记
- 使用pthread库进行多线程编程1 - UNIX环境高级编程第11章读书笔记
- Unix环境高级编程第一章摘记
- 进程编程2 – Unix环境高级编程8章读书笔记
- 进程编程3 - UNIX高级环境编程第9章读书笔记
- Unix环境高级程序设计入门----文件系统的相关编程(上)
- UNIX环境高级编程(第二版) 读书代码(1-5)
- 进程编程3 - UNIX高级环境编程第9章读书笔记
- 进程编程3 - UNIX高级环境编程第9章读书笔记
- Unix环境高级编程 读书笔记一
- 进程编程1 – Unix环境高级编程7章读书笔记
- 新书上架--UNIX环境高级编程中文版
- LINUX开发学习日记(第一部)(UNIX环境高级编程)
- 进程编程1 – Unix环境高级编程7章读书笔记
- 进程编程1 – Unix环境高级编程7章读书笔记
- unix环境高级编程(信号)