一、为什么要用exec族函数,有什么用 

(1)一个父进程希望复制自己,使父、子进程同时执行不同的代码段。
这在网络服务进程种是常见的——父进程等待客户端的服务请求。
当这种请求到达时,父进程调用fork,使子进程处理此请求。
父进程则继续等待下一个服务请求到达。
(2)一个进程要执行一个不同的程序。这对shell是常见的情况。在这种情况下,子进程从fork返回后立即调用exec

二、exec族函数函数的作用:

我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。

当进程调用exec函数时,该进程被完全替换为新程序。

因为调用exec函数并不创建新进程,所以前后进程的ID并没有改变。

三、exec族函数的定义

       可以通过这个网站查询:linux函数查询
功能: 
  在调用进程内部执行一个可执行文件。可执行文件既可以是二进制文件,也可以是任何Linux下可执行的脚本文件。 
函数族: 
  exec函数族分别是:execl, execlp, execle, execv, execvp, execvpe 
函数原型:

#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[]);

带e在初学用的比较少,工作用的时候才会有  使用新的环境变量代替调用进程的环境变量

返回值: 
exec函数族的函数执行成功后不会返回,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行。
参数说明: 

path:可执行文件的路径名字 
arg:可执行程序所带的参数,第一个参数为可执行文件名字,没有带路径且arg必须以NULL结束 
file:如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。

exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助: 
l : 使用参数列表 
p:使用文件名,并从PATH环境进行寻找可执行文件 
v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。 
e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量

下面将exac函数归为带l、带p、带v、带e 四类来说明参数特点。

一、带l的一类exac函数(l表示list),包括execl、execlp、execle,要求将新程序的每个命令行参数都说明为 一个单独的参数。这种参数表以空指针结尾。 
以execl函数为例子来说明:

echoarg.c
#include <stdio.h>

int main(int argc,char *argv[])
{
    int i = 0;
    for(i = 0; i < argc; i++)
    {
        printf("argv[%d]: %s\n",i,argv[i]);
    }
    return 0;
}
excel_test1.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
    printf("before execl\n");
    if(execl("./echoarg","echoarg","abc",NULL) == -1)
    {
        printf("execl failed!\n");

        perror("why");
    }
    printf("after execl\n");
    return 0;
}


运行结果为: 

我们先用gcc编译echoarg.c,生成可执行文件echoarg并放在当前路径bin目录下。
文件echoarg的作用是打印命令行参数。
然后再编译execl.c并执行execl可执行文件。
用execl 找到并执行echoarg,将当前进程main替换掉,所以”after execl” 没有在终端被打印出来。

1.用exec函数实现ls

excel_test2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
    printf("before execl\n");
    if(execl("/bin/ls","ls",NULL,NULL) == -1)
    {
        printf("execl failed!\n");

        perror("why");
    }
    printf("after execl\n");
    return 0;
}

运行结果为:

2.用execl函数实现ls -l

excel_test3.c 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
    printf("before execl\n");
    if(execl("/bin/ls","ls","-l",NULL) == -1)
    {
        printf("execl failed!\n");

        perror("why");
    }
    printf("after execl\n");
    return 0;
}

运行结果为:

3.用excel函数实现日期显示date

excel_test4.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
    printf("this is pro system date\n");

    if(execl("/bin/date","date",NULL,NULL) == -1)
    {
        printf("execl failed!\n");

        perror("why");
    }
    printf("after execl\n");
    return 0;
}

运行结果为:

4.用excelp函数实现ps

excel_test5.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
    printf("this is pro system date\n");

    if(execlp("ps","ps",NULL,NULL) == -1)
    {
        printf("execl failed!\n");

        perror("why");
    }
    printf("after execl\n");
    return 0;
}

运行结果为:

上面的exaclp函数带p,所以能通过环境变量PATH查找到可执行文件ps

5.配置excelp函数对应的环境变量

ganboss@ubuntu:~/syslinux/22progress_op$ pwd
/home/ganboss/syslinux/22progress_op
ganboss@ubuntu:~/syslinux/22progress_op$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/ganboss/SYSTEM/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
ganboss@ubuntu:~/syslinux/22progress_op$ export PATH=$PATH:/home/ganboss/syslinux/22progress_op
ganboss@ubuntu:~/syslinux/22progress_op$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/ganboss/SYSTEM/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:/home/ganboss/syslinux/22progress_op

补充 cd ..是返回大目录   cd - 再返回小目录

配置好了环境变量 我在 home都可以运行我那个配置环境变量的文件里的 程序

6.用execvp函数实现ps

如char *arg[]这种形式,且arg最后一个元素必须是NULL,例如char *arg[] = {“ls”,”-l”,NULL}; 

excel_test6.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
    printf("this is pro system date\n");

    char *argv[] = {"ps",NULL,NULL};

    if(execvp("ps",argv) == -1)
    {
        printf("execl failed!\n");

        perror("why");
    }
    printf("after execl\n");
    return 0;
}

运行结果为:

四、exec配合fork使用

实现功能,当父进程检测到输入为1的时候,
创建子进程把配置文件的字段值修改掉。
fork7.c

 

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main()
{
        pid_t pid;
        int data = 10;
        while(1)
        {
                printf("please input a data\n");
                scanf("%d",&data);
                if(data==1)
                {
                        pid = fork();
                        if(pid > 0)
                        {

                        }
                        if(pid == 0)
                        {
                                int fdSrc;
                                char *readBuf = NULL;
                                fdSrc = open("TEST.CONFIG",O_RDWR);
                                int size = lseek(fdSrc,0,SEEK_END);
                                lseek(fdSrc,0,SEEK_SET);
                                readBuf = (char *)malloc(sizeof(char)*size + 8);
                                int n_read = read(fdSrc,readBuf,size);
                                char *p = strstr(readBuf,"LENG=");
                                if(p==NULL){
                                        printf("not found\n");
                                        exit(-1);
                                }
                                p = p+strlen("LENG=");
                                *p = '5';
                                lseek(fdSrc,0,SEEK_SET);
                                int n_write = write(fdSrc,readBuf,strlen(readBuf));
                        }

                }else{
                        printf("wait , do nothing\n");
                }
        }
        return 0;
}

运行结果为:

 

fork8.c比fork7.c多了 在父进程那加了wait(NULL)

fork8.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
        pid_t pid;
        int data = 10;
        while(1)
        {
                printf("please input a data\n");
                scanf("%d",&data);
                if(data==1)
                {
                        pid = fork();

                        if(pid > 0)
                        {
                                wait(NULL);
                        }
                        if(pid == 0)
                        {
                                int fdSrc;
                                char *readBuf = NULL;
                                fdSrc = open("TEST.CONFIG",O_RDWR);
                                int size = lseek(fdSrc,0,SEEK_END);
                                lseek(fdSrc,0,SEEK_SET);
                                readBuf = (char *)malloc(sizeof(char)*size + 8);
                                int n_read = read(fdSrc,readBuf,size);
                                char *p = strstr(readBuf,"LENG=");
                                if(p==NULL){
                                        printf("not found\n");
                                        exit(-1);
                                }
                                p = p+strlen("LENG=");
                                *p = '5';
                                lseek(fdSrc,0,SEEK_SET);
                                int n_write = write(fdSrc,readBuf,strlen(readBuf));
                                close(fdSrc);
                                exit(0);
                        }

                }else{
                        printf("wait , do nothing\n");
                }
        }
        return 0;
}

运行结果为:

fork9.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>


int main()
{
        pid_t pid;
        int data = 10;

        while(1)
        {
                printf("please input a data\n");
                scanf("%d",&data);
                if(data==1)
                {
                        pid = fork();

                        if(pid > 0)
                        {
                                wait(NULL);
                        }
                        if(pid == 0)
                        {
                                execl("./changeData","changeData","TEST.CONFIG",NULL);
                                exit(0);

                        }

                }else{
                        printf("wait , do nothing\n");
                }
        }
        return 0;
}

运行结果为:

子进程: 

execl("./changeData","changeData","TEST.CONFIG",NULL);
exit(0);

changeData的由来

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐