這篇文章主要介紹了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的結果圖
以上是Linux使用exec函數實現進程替換的程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!