让您轻松理解execl函数系列 ^_^
execl函数功能如下:启动一个可执行文件,并且对他进行传送参数。一些原型如下
#include <unistd.h> extern char **environ; int execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg , ..., char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]);
execlp execl execle构造argv构造argv 构造argvexecvp 试每一个path前缀->execv 使用environ->execve(系统调用)
/*假设我系统里有/home/oracle/bin/child.exe这个程序,path目录包括了/home/oracle/bin/*/int main(int argc ,char **argv){pid_tpid;if ((pid = fork()) < 0) {cout<<"fork err"<<endl;return 0;} else if (pid == 0) {// specify pathname, specify environment //int execle(const char *path, const char *arg , ..., char * const // envp[]);if (execle("/home/oracle/bin/child.exe", "execle", "A",(char *)0, env_init) < 0){cout<<"execle err"<<strerror(errno)<<endl;return 0;}}if (waitpid(pid, NULL, 0) < 0){cout<<"waitpid err"<<endl;return 0;}if ((pid = fork()) < 0) {cout<<"fork err"<<endl;return 0;} else if (pid == 0) {/* specify filename, inherit environment */if (execlp("child.exe", "execlp", "A", (char *)0) < 0){cout<<"execlp err:"<<strerror(errno)<<endl;return 0;}}///home/oracle/binif (waitpid(pid, NULL, 0) < 0){cout<<"waitpid err"<<endl;return 0;}if ((pid = fork()) < 0) {cout<<"fork err"<<endl;return 0;} else if (pid == 0) {/* specify filename, inherit environment *//*if (execl("/home/oracle/bin/child.exe", "execl", "a", (char *)0) < 0){cout<<"execlp err:"<<strerror(errno)<<endl;return 0;}*/if (execl("/bin/ls", "ls", "-al", "/home/oracle/bin/child.exe", (char *)0) < 0){cout<<"execlp err:"<<strerror(errno)<<endl;return 0;}}//int execv(const char *path, char *const argv[]);char *execv_arg[]={"execv","a",NULL};if (waitpid(pid, NULL, 0) < 0){cout<<"waitpid err"<<endl;return 0;}if ((pid = fork()) < 0) {cout<<"fork err"<<endl;return 0;} else if (pid == 0) {/* specify filename, inherit environment */if (execv("/home/oracle/bin/child.exe", execv_arg) < 0){cout<<"execv err:"<<strerror(errno)<<endl;return 0;}}return 0;}