exec函式簇

HPhone發表於2014-07-26

exec一共有六種函式,都是以exec開頭。

 

1 int execl(const char *path, const char *arg, ...);
2 int execlp(const char *file, const char *arg, ...);
3 int execle(const char *path, const char *arg, ..., char * const envp[]);
4 int execv(const char *path, char *const argv[]);
5 int execvp(const char *file, char *const argv[]);
6 int execve(const char *path, char *const argv[], char *const envp[]);

 

字尾 含義
l l代表list,接受以逗號的引數,最後是以(char *)0結束。
v v代表vector,接受引數陣列,陣列以NULL結束。
p p代表取用PATH環境環境尋找可執行檔案,不需要單獨傳入一個路徑引數。
e 取代當前的環境環境

主程式

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <sys/wait.h>
 4 #include <errno.h>
 5 extern char **environ;
 6 int main(){
 7     char *env[]={"USER=hh","PATH=/home/map/users",NULL};
 8     int pid;
 9     if((pid=fork())<0){
10         perror("fork error;");
11     }else if(pid==0){
12         execle("/home/map/users/maxin/cpp/chr08/test","test","hello","world",(char *)0,env);
13         perror("exec error:");
14     }
15     if(waitpid(pid,NULL,0)!=pid){
16         perror("wait error");
17     }
18     printf("----------------------------------------\n");
19     if((pid=fork())<0){
20         perror("fork error;");
21     }else if(pid==0){
22         execle("/home/map/users/maxin/cpp/chr08/test","test","hello","world",(char *)0,environ);
23         perror("exec error:");
24     }
25     if(waitpid(pid,NULL,0)!=pid){
26         perror("wait error");
27     }
28 
29     printf("----------------------------------------\n");
30     if((pid=fork())<0){
31         perror("fork error;");
32     }else if(pid==0){
33         execlp("./test","./test","hello",(char *)0);
34         perror("exec error:");
35     }
36     if(waitpid(pid,NULL,0)!=pid){
37         perror("wait error");
38     }
39 }

被呼叫的程式

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 extern char **environ;
 4 void showEnv();
 5 int main(int args,const char *argv[]){
 6     printf("args%d\n",args );
 7     for(int i=0;i!=args;++i){
 8         printf("argv[%d]:%s\n",i,argv[i]);
 9     }
10     printf("USER:%s\n", getenv("USER"));
11     printf("PATH:%s\n", getenv("PATH"));
12     showEnv();
13 }
14 void showEnv(){
15     int i=0;
16     while(environ[i]){
17         printf("environ[%d]:%s\n",i,environ[i] );
18         ++i;
19     }
20     printf("----------------------END---------------------------\n" );
21 }

 最後的顯示結果

args3
argv[0]:test
argv[1]:hello
argv[2]:world
USER:hh
PATH:/home/map/users
environ[0]:USER=hh
environ[1]:PATH=/home/map/users
----------------------END---------------------------
----------------------------------------
args3
argv[0]:test
argv[1]:hello
argv[2]:world
USER:map
PATH:/home/tools/tools/svn/1.6.5/64/bin:/home/tools/tools/scmtools/usr/bin:/home/tools/tools/../bin/64/:/home/tools/tools/maven/apache-maven-2.2.0/bin:/home/tools/tools/ant/apache-ant-1.6.5/bin:/home/tools/tools/ant/apache-ant-1.7.1/bin:/home/tools/tools/php/5.2.17/64/bin:/java/jdk1.6.0_06/bin:/java/jdk1.6.0_06/jre/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/share/*****/bin:/opt/bin:/home/opt/bin:/home/map/bin:/home/work/.nlpcloud/nlpc/bin:/home/work/.qu/qu:/home/map/bin:/home/map/users/yinsongyang/fis/node-v0.10.26-linux-x64/bin:/home/map/users/yinsongyang/fis/jre1.7.0_55/bin:/home/map/odp24/php/bin
environ[0]:MAC=64
environ[1]:MANPATH=:/tools/*****_manpage/man
environ[2]:HOSTNAME=cq01-rdqa-*****.*****.*****.com
environ[3]:TERM=vt100
environ[4]:SHELL=/bin/bash
environ[5]:HISTSIZE=1000
environ[6]:SSH_CLIENT=172.22.4.13 36038 22
environ[7]:OLDPWD=/home/map/users/maxin/cpp
environ[8]:MAVEN_2_2_1=/home/tools/tools/maven/apache-maven-2.2.1/bin
environ[9]:SSH_TTY=/dev/pts/2
environ[10]:MAVEN_3_0_4=/home/tools/tools/maven/apache-maven-3.0.4/bin
environ[11]:ANT_HOME=/home/tools/tools/ant/apache-ant-1.6.5
environ[12]:USER=map
environ[13]:LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:
environ[14]:MAIL=/var/spool/mail/map
environ[15]:PATH=/home/tools/tools/svn/1.6.5/64/bin:/home/tools/tools/scmtools/usr/bin:/home/tools/tools/../bin/64/:/home/tools/tools/maven/apache-maven-2.2.0/bin:/home/tools/tools/ant/apache-ant-1.6.5/bin:/home/tools/tools/ant/apache-ant-1.7.1/bin:/home/tools/tools/php/5.2.17/64/bin:/java/jdk1.6.0_06/bin:/java/jdk1.6.0_06/jre/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/share/*****/bin:/opt/bin:/home/opt/bin:/home/map/bin:/home/work/.nlpcloud/nlpc/bin:/home/work/.qu/qu:/home/map/bin:/home/map/users/yinsongyang/fis/node-v0.10.26-linux-x64/bin:/home/map/users/yinsongyang/fis/jre1.7.0_55/bin:/home/map/odp24/php/bin
environ[16]:INPUTRC=/etc/inputrc
environ[17]:PWD=/home/map/users/maxin/cpp/chr08
environ[18]:JAVA_HOME=/java/jdk1.6.0_06
environ[19]:LANG=en_US
environ[20]:JAVA_HOME_1_6=/home/tools/tools/java/jdk1.6.0_20
environ[21]:JAVA_HOME_1_5=/home/tools/tools/java/jdk1.5.0_07
environ[22]:ANT_HOME_1_7=/home/tools/tools/ant/apache-ant-1.7.1
environ[23]:SHLVL=1
environ[24]:HOME=/home/map
environ[25]:LOGNAME=map
environ[26]:CLASSPATH=:/java/jdk1.6.0_06/lib:/java/jdk1.6.0_06/jre/lib
environ[27]:SSH_CONNECTION=172.22.4.13 36038 10.48.52.44 22
environ[28]:PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:
environ[29]:LESSOPEN=|/usr/bin/lesspipe.sh %s
environ[30]:G_BROKEN_FILENAMES=1
environ[31]:_=./a.out
----------------------END---------------------------
----------------------------------------
args2
argv[0]:./test
argv[1]:hello
USER:map
PATH:/home/tools/tools/svn/1.6.5/64/bin:/home/tools/tools/scmtools/usr/bin:/home/tools/tools/../bin/64/:/home/tools/tools/maven/apache-maven-2.2.0/bin:/home/tools/tools/ant/apache-ant-1.6.5/bin:/home/tools/tools/ant/apache-ant-1.7.1/bin:/home/tools/tools/php/5.2.17/64/bin:/java/jdk1.6.0_06/bin:/java/jdk1.6.0_06/jre/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/share/*****/bin:/opt/bin:/home/opt/bin:/home/map/bin:/home/work/.nlpcloud/nlpc/bin:/home/work/.qu/qu:/home/map/bin:/home/map/users/yinsongyang/fis/node-v0.10.26-linux-x64/bin:/home/map/users/yinsongyang/fis/jre1.7.0_55/bin:/home/map/odp24/php/bin
environ[0]:MAC=64
environ[1]:MANPATH=:/tools/*****_manpage/man
environ[2]:HOSTNAME=cq01-rdqa-*****.*****.*****.com
environ[3]:TERM=vt100
environ[4]:SHELL=/bin/bash
environ[5]:HISTSIZE=1000
environ[6]:SSH_CLIENT=172.22.4.13 36038 22
environ[7]:OLDPWD=/home/map/users/maxin/cpp
environ[8]:MAVEN_2_2_1=/home/tools/tools/maven/apache-maven-2.2.1/bin
environ[9]:SSH_TTY=/dev/pts/2
environ[10]:MAVEN_3_0_4=/home/tools/tools/maven/apache-maven-3.0.4/bin
environ[11]:ANT_HOME=/home/tools/tools/ant/apache-ant-1.6.5
environ[12]:USER=map
environ[13]:LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:
environ[14]:MAIL=/var/spool/mail/map
environ[15]:PATH=/home/tools/tools/svn/1.6.5/64/bin:/home/tools/tools/scmtools/usr/bin:/home/tools/tools/../bin/64/:/home/tools/tools/maven/apache-maven-2.2.0/bin:/home/tools/tools/ant/apache-ant-1.6.5/bin:/home/tools/tools/ant/apache-ant-1.7.1/bin:/home/tools/tools/php/5.2.17/64/bin:/java/jdk1.6.0_06/bin:/java/jdk1.6.0_06/jre/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/share/*****/bin:/opt/bin:/home/opt/bin:/home/map/bin:/home/work/.nlpcloud/nlpc/bin:/home/work/.qu/qu:/home/map/bin:/home/map/users/yinsongyang/fis/node-v0.10.26-linux-x64/bin:/home/map/users/yinsongyang/fis/jre1.7.0_55/bin:/home/map/odp24/php/bin
environ[16]:INPUTRC=/etc/inputrc
environ[17]:PWD=/home/map/users/maxin/cpp/chr08
environ[18]:JAVA_HOME=/java/jdk1.6.0_06
environ[19]:LANG=en_US
environ[20]:JAVA_HOME_1_6=/home/tools/tools/java/jdk1.6.0_20
environ[21]:JAVA_HOME_1_5=/home/tools/tools/java/jdk1.5.0_07
environ[22]:ANT_HOME_1_7=/home/tools/tools/ant/apache-ant-1.7.1
environ[23]:SHLVL=1
environ[24]:HOME=/home/map
environ[25]:LOGNAME=map
environ[26]:CLASSPATH=:/java/jdk1.6.0_06/lib:/java/jdk1.6.0_06/jre/lib
environ[27]:SSH_CONNECTION=172.22.4.13 36038 10.48.52.44 22
environ[28]:PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:
environ[29]:LESSOPEN=|/usr/bin/lesspipe.sh %s
environ[30]:G_BROKEN_FILENAMES=1
environ[31]:_=./a.out
----------------------END---------------------------

 

相關文章