生活随笔
收集整理的這篇文章主要介紹了
进程控制2--exec族
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
系統(tǒng)調(diào)用exe函數(shù)族對(duì)當(dāng)前進(jìn)程進(jìn)行替換,替換著為一個(gè)指定程序,其參數(shù)包括文件名filename,參數(shù)列表argv,以及環(huán)境變量envp
整個(gè)函數(shù)家族如下:
#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[]);
int execve(const char *path, char *const argv[], char *const envp[]);
在名字中缺“e”的是從當(dāng)前進(jìn)程中拷貝環(huán)境,有字母“p”的是可以在當(dāng)前路徑中查找文件,而其他函數(shù)必須在指定路徑中找,這里要注意執(zhí)行路徑,有安全隱患,慎用。
?
練習(xí)1:用系統(tǒng)命令替換
[cpp]?view plaincopy
?? #include?<unistd.h>?? main()?? {????? ?????? ????char?*envp[]={"PATH=/tmp",??"USER=lei",?"STATUS=testing",NULL};??? ?????? ????char?*argv_execv[]={"echo",?"excuted?by?execv",?NULL};???? ?????? ????char?*argv_execvp[]={"echo",?"executed?by?execvp",?NULL};?? ?????? ????char?*argv_execve[]={"env",?NULL};???? ?????? ????if(fork()==0)????????? ????????if(execl("/bin/echo",?"echo",?"executed?by?execl",?NULL)<0)???? ????????????perror("Err?on?execl");?? ????????if(fork()==0)????? ????????if(execlp("echo",?"echo",?"executed?by?execlp",?NULL)<0)?? ?? ????????????????perror("Err?on?execlp");?????? ????????????if(fork()==0)????????? ????????????????if(execle("/usr/bin/env",?"env",?NULL,?envp)<0)???? ????????????????????perror("Err?on?execle");?????? ????????????????if(fork()==0)????????? ????????????????????if(execv("/bin/echo",?argv_execv)<0)???? ????????????????????????perror("Err?on?execv");??? ????????????????????if(fork()==0)????? ????????????????????if(execvp("echo",?argv_execvp)<0)?????? ????????????????????????????perror("Err?on?execvp");?? ????????????????????????if(fork()==0)????????? ????????????????if(execve("/usr/bin/env",?argv_execve,?envp)<0)???????? ?? ????????????????????????????????perror("Err?on?execve");?? }??
程序里調(diào)用了2個(gè)Linux常用的系統(tǒng)命令,echo和env。echo會(huì)把后面跟的命令行參數(shù)原封不動(dòng)的打印出來,env用來列出所有環(huán)境變量。
?
編譯,測(cè)試:
$ ./exec
executed by execl
executed by execlp
PATH=/tmp
USER=lei
STATUS=testing
executed by execlp
excuted by execv
executed by execvp
PATH=/tmp
USER=lei
STATUS=testing
?
練習(xí)2:子進(jìn)程執(zhí)行用戶自己編寫的程序
先寫父進(jìn)程的程序:
[cpp]?view plaincopy
?? #include?<sys/types.h>?? #include?<unistd.h>?? #include?<stdio.h>?? ?? int?main(void)?? {?? ????pid_t?pid;?? ????const?char?*usr_envp[]?=?{"MYDEFINE=unknown","PATH=/home",NULL};?? ????printf("Begin?Fork()/n");?? ????pid?=?fork();?? ????switch(pid)?? ????{?? ????case?-1:?? ????????perror("fork?failed!/n");?? ????????break;?? ????case?0:?? ?????????? ????????if(execle("/home/child","myarg1","myarg2",(char?*)0,usr_envp))??? ????????perror("execle?failed!/n");?? ????????break;?? ????default:?? ????????break;?? ????}?? ?????? ????if(waitpid(pid,NULL,0)<0)?? ????????perror("waitpid?failed!/n");?? ????printf("parent?excting!");?? ????return?0;?? }??
然后我們編寫子進(jìn)程要執(zhí)行的程序:
[cpp]?view plaincopy
?? #include?<sys/types.h>?? #include?<unistd.h>?? #include?<stdio.h>?? int?main(int?argc,char?*argv[])?? {?? ????int?i;?? ????char?**pStr;?? ????extern?char?**environ;?? ????printf("child?starting!/n");?? ?????? ?????? ????for(i?=?0;i?<?argc;i++)?? ????{?? ????????printf("argv[%d]:%s/n",i,argv[i]);?? ????}?? ?????? ????for(pStr?=?environ;?*pStr?!=?0;pStr++)?? ????{?? ????????printf("%s/n",i,*pStr);?? ????}?? ????printf("child?exiting!/n");?? ????return?0;?? }??
然后編譯,執(zhí)行parent
$ ./parent
Begin Fork()
child starting!
argv[0]:myargv1
argv[1]:myargv2
child exiting!
parent excting!
總結(jié)
以上是生活随笔為你收集整理的进程控制2--exec族的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。