Linux 进程替换(exec函数)实现代码
Linux 进程替换(exec函数)实现代码
# include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> int main() { pid_t id=fork(); if(id==0) { printf("child is running\n"); sleep(1); char* env[]={"MYENV=/A/B/C/D/AAAA",NULL}; char* argv[]={"ls","-l","-n","-i",NULL}; // execl("/bin/ls","ls","-l","-n","-i",NULL); // execlp("ls","ls","-l","-n","-i",NULL); // execv("/bin/ls",argv); // execvp("ls",argv); // execle("./myenv","myenv",NULL,env); execve("./myenv",argv,env); printf("child id done\n"); //exec成功的话不执行此语句 exit(1); } else { pid_t ret=waitpid(id, NULL,0); if(ret>0) { printf("father wait success\n"); } else{ printf("child quit not normal\n"); } } return 0; }
下图就是execl execlp execv execvp的结果图
下图是execle execve的结果图
感谢阅读,希望能帮助到大家,谢谢大家对对本知道支持!
相关推荐
farwang 2020-11-25
星愿心愿 2020-11-24
tianhuak 2020-11-24
zhjn0 2020-11-24
昭君出塞 2020-11-23
bluecarrot 2020-11-23
linuxwcj 2020-10-21
以梦为马不负韶华 2020-10-20
彼岸随笔 2020-10-20
yutou0 2020-10-17
applecarelte 2020-10-16
ourtimes 2020-10-16
waterhorse 2020-09-19
MRFENGG 2020-11-11
rainandtear 2020-10-30
kyssfanhui 2020-10-20
liuhangtiant 2020-10-20