Linux使用exec函數(shù)實(shí)現(xiàn)進(jìn)程替換的代碼分享

這篇文章主要介紹了linux 進(jìn)程替換(execlinux)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下

linux 進(jìn)程替換(exec函數(shù))實(shí)現(xiàn)代碼

??#?include<stdio.h>???  ??#include<stdlib.h>?  ??#include<unistd.h>??  ??#include<fcntl.h>??  ??#include<sys>  ??#include<sys>??????????????????????????????????????????????????  ??#include<string.h>??  ??int?main()?????  ???{?????????  ????pid_t?id=fork();?  ??if(id==0)??????  ???{????????  ???printf("child?is?runningn");  ???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?donen");??//exec成功的話不執(zhí)行此語(yǔ)句  ??????exit(1);???  ????}???????????????  ????else????????????????????????????????????????????????????????  ????{?????????  ???????pid_t?ret=waitpid(id,?NULL,0);  ??????if(ret&gt;0)??  ???????{??????  ????????printf("father?wait?successn");?}  ?????????else{??  ??????????printf("child?quit?not?normaln");  ????????????  ???????}???}???return?0;??????????????????????????????????????????????}</string.h></sys></sys></fcntl.h></unistd.h></stdlib.h></stdio.h>

下圖就是execl execlp execv execvp的結(jié)果圖

Linux使用exec函數(shù)實(shí)現(xiàn)進(jìn)程替換的代碼分享

下圖是execle execve的結(jié)果圖

Linux使用exec函數(shù)實(shí)現(xiàn)進(jìn)程替換的代碼分享

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊8 分享