exec函数族

来源:互联网 发布:人工智能销售招聘 编辑:程序博客网 时间:2024/06/04 17:48
#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 execvpe(const char *file, char *const argv[],char *const envp[]);

区别1:
    前4个函数取路径名作为参数
    后2个取文件名作为参数
    最后一个取文件描述符作为参数
区别2:
    l表示参数列表
    v表示参数指针数组
区别3:
    有e 的函数代表最后输入一个环境字符串的指针数组
    其余的的环境字符串都取自environ
    

对与文件打开时设置了执行时关闭 FD_CLOEXEC , 则执行exec时关闭文件描述符,否个关闭文件描述符

#include<unistd.h>void exceTest(void){    pid_t pid = vfork();    if(pid == 0)    {        cout<<"child start"<<endl;        char *bug = "exec";        if(execl("/home/ch/ch/exec/bin/Debug/exec","ssss","111",NULL)==-1)            cout<<"run fail"<<endl;        cout<<"child end"<<endl;    }    else    {        pid =vfork();        if(pid==0)        {             cout<<"child 2  start"<<endl;             char *p[] ={"ssss","111",NULL} ;             char *p1[] ={"PATH=/home/ch/ch",NULL} ;             execve("/home/ch/ch/exec/bin/Debug/exec",p,p1);        }        cout<<"parent end"<<endl;    }    int status =0 ;    waitpid(pid,&status,0);    waitpid(pid,&status,0);    sleep(10);}int main(){    cout<<"=====execTest======"<<endl;    exceTest();    return 1;}



0 0