zoukankan      html  css  js  c++  java
  • exec函数簇

    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("----------------------------------------
    ");
    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("----------------------------------------
    ");
    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
    ",args );
     7     for(int i=0;i!=args;++i){
     8         printf("argv[%d]:%s
    ",i,argv[i]);
     9     }
    10     printf("USER:%s
    ", getenv("USER"));
    11     printf("PATH:%s
    ", getenv("PATH"));
    12     showEnv();
    13 }
    14 void showEnv(){
    15     int i=0;
    16     while(environ[i]){
    17         printf("environ[%d]:%s
    ",i,environ[i] );
    18         ++i;
    19     }
    20     printf("----------------------END---------------------------
    " );
    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---------------------------
  • 相关阅读:
    TFS 安装遇到的问题
    批量将MP4 转换为 MP3
    sqlite like 通配符 ,匹配区分大小写(默认不区分大小写)
    AutoCAD 2007-2012 长度统计工具
    python27 ImportError: No module named site
    github push时,要求密码的问题
    sqlserver中自定义计算函数
    关于win10家庭版不能开启虚拟机的问题
    js的MD5实现
    高德各省行政区显示不同区别颜色(转)
  • 原文地址:https://www.cnblogs.com/HPhone/p/3870046.html
Copyright © 2011-2022 走看看