前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Linux系统下进程编程之exec族函数解析(四)

Linux系统下进程编程之exec族函数解析(四)

作者头像
用户6280468
发布于 2022-03-18 12:51:53
发布于 2022-03-18 12:51:53
1.4K00
代码可运行
举报
文章被收录于专栏:txp玩Linuxtxp玩Linux
运行总次数:0
代码可运行
在前面的文章里面,我们用fork()函数创建的子进程都是在程序中的if语句中写入代码,这样可以,但是不够灵活,因为我们只能把子进程程序的源代码贴过来执行(必须要知道源代码,而且源代码太长了也不好控制),譬如说我们希望子进程来执行ls -la 命令就不行了(没有源代码,只有编译好的可执行程序);为了解决这种不灵活性,所以在Linux系统中引入了exec族函数。

一、族函数的引入:

1、族函数说明:

fork函数是用于创建一个子进程,该子进程几乎是父进程的副本,而有时我们希望子进程去执行另外的程序,exec函数族就提供了一个在进程中启动另一个程序执行的方法。它可以根据指定的文件名或目录名找到可执行文件,并用它来取代原调用进程的数据段、代码段和堆栈段,在执行完之后,原调用进程的内容除了进程号外,其他全部被新程序的内容替换了。另外,这里的可执行文件既可以是二进制文件,也可以是Linux下任何可执行脚本文件。

2、在Linux中使用exec函数族主要有以下两种情况:

a、当进程认为自己不能再为系统和用户做出任何贡献时,就可以调用任何exec 函数族让自己重生。

b、如果一个进程想执行另一个程序,那么它就可以调用fork函数新建一个进程,然后调用任何一个exec函数使子进程重生。

二、exec族函数的介绍和实战:

1、还是老套路,首先我们用man 3 exec来查看有哪些exec族函数:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
NAME
   execl, execlp, execle, execv, execvp, execvpe - execute a file

SYNOPSIS
   #include <unistd.h>

   extern char **environ;

   int execl(const char *path, const char *arg, ...
                   /* (char  *) NULL */);
   int execlp(const char *file, const char *arg, ...
                   /* (char  *) NULL */);
   int execle(const char *path, const char *arg, ...
                   /*, (char *) NULL, 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[]);
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 DESCRIPTION
   The  exec() family of functions replaces the current process image with
   a new process image.  The functions described in this manual  page  are
   front-ends  for execve(2).  (See the manual page for execve(2) for fur‐
   ther details about the replacement of the current process image.)

   The initial argument for these functions is the name of a file that  is
   to be executed.

   The  const  char *arg and subsequent ellipses in the execl(), execlp(),
   and execle() functions can be thought of  as  arg0,  arg1,  ...,  argn.
   Together  they  describe  a list of one or more pointers to null-termi‐
   nated strings that represent the argument list available  to  the  exe‐
   cuted  program.  The first argument, by convention, should point to the
   filename associated with the file being executed.  The  list  of  argu‐
   ments  must be terminated by a null pointer, and, since these are vari‐
   adic functions, this pointer must be cast (char *) NULL.

   The execv(), execvp(), and execvpe()  functions  provide  an  array  of
   pointers  to  null-terminated  strings that represent the argument list
   available to the new  program.   The  first  argument,  by  convention,
   should  point  to the filename associated with the file being executed.
   The array of pointers must be terminated by a null pointer.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 The execle() and execvpe() functions allow the caller  to  specify  the
   environment  of  the  executed program via the argument envp.  The envp
   argument is an array of pointers to null-terminated strings and must be
   terminated by a null pointer.  The other functions take the environment
   for the new process image from the external  variable  environ  in  the
   calling process.

  Special semantics for execlp() and execvp()
   The  execlp(),  execvp(), and execvpe() functions duplicate the actions
   of the shell in searching for an executable file if the specified file‐
   name does not contain a slash (/) character.  The file is sought in the
   colon-separated list of directory pathnames specified in the PATH envi‐
   ronment  variable.   If  this  variable  isn't  defined,  the path list
   defaults to a list that includes  the  directories  returned  by  conf‐
   str(_CS_PATH)  (which  typically returns the value "/bin:/usr/bin") and
   possibly also the current working  directory;  see  NOTES  for  further
   details.

   If  the  specified  filename  includes  a slash character, then PATH is
   ignored, and the file at the specified pathname is executed.

   In addition, certain errors are treated specially.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 If permission is denied for a file (the attempted execve(2) failed with
   the  error EACCES), these functions will continue searching the rest of
   the search path.  If no other file is found, however, they will  return
   with errno set to EACCES.

   If  the  header  of  a  file  isn't recognized (the attempted execve(2)
   failed with the error ENOEXEC), these functions will execute the  shell
   (/bin/sh)  with  the  path of the file as its first argument.  (If this
   attempt fails, no further searching is done.)

 RETURN VALUE
   The exec() functions return only if an error has occurred.  The  return
   value is -1, and errno is set to indicate the error.

 ERRORS
   All  of  these  functions  may fail and set errno for any of the errors
   specified for execve(2).

说明与实战demo:

a、我们首先来分析execl和execv :

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 int execl(const char *path, const char *arg, ... /* (char  *) NULL */);
  int execv(const char *path, char *const argv[]);

这两个函数是最基本的exec族函数,都可以用来执行一个程序,区别是传参的格式不同。execl是把参数列表(“...”它是一个变参,本质上是多个字符串,【必须以NULL结尾】)依次排列而成(execl中的“l”其实就是list的缩写),execv是把参数列表事先放入一个字符串数组中,再把这个字符串数组传给execv函数。而参数path都表示可执行的文件路径。

现在我们以可执行程序ls -la来演示,但是我们的先知道它的路径,要用命令---which ls 来查看:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 ubuntu@ubuntu-virtual-machine:~$ which   ls
 /bin/ls

我们先用execl函数来演示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  #include <stdio.h>
  #include <unistd.h>
  #include <sys/types.h>  
  #include <sys/wait.h>
  #include <stdlib.h>

  int main(void)
 {
pid_t pid = -1;
pid_t ret = -1;
int status = -1;

pid = fork();
if (pid > 0)
{
    // 父进程
    printf("parent, 子进程id = %d.\n", pid);
}
else if (pid == 0)
{
    // 子进程
    execl("/bin/ls", "ls", "-l", "-a", NULL);       // ls -l -a

    return 0;
}
else
{
    perror("fork");
    return -1;
}

return 0;
 }

演示结果:

接着我们用execv来演示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  #include <stdio.h>
  #include <unistd.h>
  #include <sys/types.h>  
  #include <sys/wait.h>
  #include <stdlib.h>
  int main(void)
  {
   pid_t pid = -1;
   pid_t ret = -1;
   int status = -1;

   pid = fork();
   if (pid > 0)
   {
    // 父进程
    printf("parent, 子进程id = %d.\n", pid);
   }
   else if (pid == 0)
   {
    // 子进程
    char * const arg[] = {"ls", "-l", "-a", NULL};
    execv("/bin/ls", arg);
    return 0;
  }
 else
 {
    perror("fork");
    return -1;
 }

 return 0;
 }

演示效果:

最后我们可以利用上面讲的函数来实现我们开头讲的那样(其实上面举得例子也是一样的效果),自己编写一个外部文件,来提高灵活性,这里我我创建了一个hello.c文件,内容是,然后再用execl调用执行:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <stdio.h>

int main(int argc, char **argv)
{
    int i = 0;

    printf("argc = %d.\n", argc);

    while (NULL != argv[i])
    {
            printf("argv[%d] = %s\n", i, argv[i]);
            i++;
    }

   return 0;
}
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>  
 #include <sys/wait.h>
 #include <stdlib.h>



 int main(void)
 {  
     pid_t pid = -1;
     pid_t ret = -1;
     int status = -1;

     pid = fork();
     if (pid > 0)
     {
            // 父进程
            printf("parent, 子进程id = %d.\n", pid);
     }
   else if (pid == 0)
   {
    // 子进程

       execl("hello","aaa", NULL);
       return 0;
   }
   else
   {
    perror("fork");
    return -1;
   }

    return 0;
}

演示结果:

b、分析execlp和execvp:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
      int execlp(const char *file, const char *arg, .../* (char  *) NULL */);
       int execvp(const char *file, char *const argv[]);

execlp和execvp 这两个函数在上面2个基础上加了p,较上面2个来说,区别是:上面2个执行程序时必须指定可执行程序的【全路径】(如果exec没有找到path这个文件则直接报错),而加了p的传递的可以是file(也可以是path,只不过兼容了file。加了p的这两个函数会首先去找file,如果找到则执行,如果没找到则会去环境变量PATH所指定的目录下去找,如果找到则执行如果没找到则报错)---(注意:

进程中的环境变量说明,在Linux中,Shell进程是所有执行码的父进程。当一个执行码执行时,Shell进程会fork子进程然后调用exec函数去执行执行码。Shell进程堆栈中存放着该用户下的所有环境变量,使用execl、execv、execlp、execvp函数使执行码重生时,Shell进程会将所有环境变量复制给生成的新进程;而使用execle、execve时新进程不继承任何Shell进程的环境变量,而由envp[]数组自行设置环境变量。这两个函数会在下面进行讲解的):

现在使用excel来演示可执行程序----ls -la,最后它在环境变量目录下找到了ls ,在当前用户目录找不到:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>  
 #include <sys/wait.h>
 #include <stdlib.h>



 int main(void)
 {
     pid_t pid = -1;
     pid_t ret = -1;
     int status = -1;

      pid = fork();
    if (pid > 0)
    {
    // 父进程
    printf("parent, 子进程id = %d.\n", pid);
    }
    else if (pid == 0)
    {
      // 子进程

      execlp("ls", "ls", "-l", "-a", NULL); 
      return 0;
    }
   else
   {
    perror("fork");
    return -1;
    }

   return 0;
  }

演示效果:

注明:execvp函数用法一样,这里我就不举例子了,可以按照execv用法模仿使用。

c、分析execle和execvpe:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    int execle(const char *path, const char *arg, ...   /*, (char *) NULL, char * const envp[] */);
    int execvpe(const char *file, char *const argv[],    char *const envp[]);

这两个函数较基本exec来说加了e,函数的参数列表中也多了一个字符串数组envp形参,e就是environment环境变量的意思,和基本版本的exec的区别就是:执行可执行程序时会多传一个环境变量的字符串数组给待执行的程序。

main函数的原型其实不止是int main(int argc, char **argv),而可以是int main(int argc, char **argv, char **env) 第三个参数是一个字符串数组,内容是环境变量,Linux系统下环境变量:

如果用户在执行这个程序时没有传递第三个参数,则程序会自动从父进程继承一份环境变量(默认的,最早来源于OS中的环境变量);如果我们exec的时候使用execle或者execvpe去给传一个envp数组,则程序中的实际环境变量是我们传递的这一份(取代了默认的从父进程继承来的那一份)

注意:execle和execvpe的第三个环境变量参数是可以更改从系统环境变量继承过来的这一份的。

下面我们还是以hello那个文件为例,然后再用execle函数来调用,hello文件里面的内容是:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 #include <stdio.h>

 // env就是我们给main函数额外传递的环境变量字符串数组
 int main(int argc, char **argv, char **env)
 {
int i = 0;

printf("argc = %d.\n", argc);

while (NULL != argv[i])
{
    printf("argv[%d] = %s\n", i, argv[i]);
    i++;
}

i = 0;
while (NULL != env[i])
{
    printf("env[%d] = %s\n", i, env[i]);
    i++;
}


return 0;
 }

执行结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>  
 #include <sys/wait.h>
 #include <stdlib.h>
 int main(void)
 {
    pid_t pid = -1;
    pid_t ret = -1;
    int status = -1;

     pid = fork();
    if (pid > 0)
   {
      // 父进程
      printf("parent, 子进程id = %d.\n", pid);
   }
   else if (pid == 0)
   {
    // 子进程
    char * const envp[] = {"AA=aaaa", "XX=abcd", NULL};
    execle("hello", "hello", "-l", "-a", NULL, envp);
   }
   else
   {
    perror("fork");
    return -1;
  }

  return 0;
 }

演示结果:

注明:execvpe函数用法一样,这里我就不举例子了,可以按照execv用法模仿使用。

三、总结:

好了,今天族函数的分享就结束了,后面还会继续分享进程的文章,在最后给大家分享一个有趣的照片,昨天一个网友发的 Linux系统中常见的目录名称以及相应内容:

上面的源代码链接:https://github.com/1121518wo/linux-/tree/master

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 txp玩Linux 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【进程控制】
父进程创建了子进程,子进程的PCB是拷贝的父进程的PCB,内容一致。并且数据段,代码段,子进程是与父进程共享的(此时权限会被设为只读),当子进程要对其进行修改,此时会发生写时拷贝,将子进程要修改的数据,拷贝一份,单独交给子进程(这一部分权限可写,可读)。
是预备程序员a
2025/04/28
740
【进程控制】
【Linux】进程控制
我们在前面的文章中多次使用过fork函数,我们在这里再来简单概括一下进程的创建 fork可以在已有的进程中创建出一个新进程,老进程为父进程,新进程为子进程
s-little-monster
2025/02/14
580
【Linux】进程控制
【Linux】进程的程序替换
红框中的代码实际上是父进程的代码,在没有执行fork之前代码就有了,在没有创建子进程之前,父进程的代码加载到内存了,子进程被创建出来是没有独立的代码,这个代码是父进程的代码,父进程通过if判断分流让子进程去跑了
lovevivi
2023/04/28
2K0
【Linux】进程的程序替换
Linux下exec函数族详解
       对于exec函数族来说,它的作用通俗来说就是使另一个可执行程序替换当前的进程,当我们在执行一个进程的过程中,通过exec函数使得另一个可执行程序A的数据段、代码段和堆栈段取代当前进程B的数据段、代码段和堆栈段,那么当前的进程就开始执行A中的内容,这一过程中不会创建新的进程,而且PID也没有改变。
Ch_Zaqdt
2020/03/02
8.7K0
【Linux】进程理解与学习-程序替换
进程可以通过程序替换的方式来执行一个全新的程序,具体的做法则是通过对应的程序替换的几个系统调用函数来实现,下面先来看一下程序替换的现象,根据这个现象来分析程序替换实现的原理。
诺诺的包包
2023/04/04
1.1K0
【Linux】进程理解与学习-程序替换
【Linux】开始学习进程替换吧!
送给大家一句话: 人生中有些事,你不竭尽所能去做,你永远不知道你自己有多出色。—— 尾田荣一郎《海贼王》
叫我龙翔
2024/03/30
1190
【Linux】开始学习进程替换吧!
【Linux课程学习】:进程程序替换,execl,execv,execlp,execvp,execve,execle,execvpe函数
1.理解子进程去调用替换函数,达到我们想要的目的。shell命令行也是这样进行处理,我们每次操作的命令,都是fork()创建子进程,然后让子进程调用exe函数进行程序替换,去执行我们想要的程序。比如ls……这些都是一些执行对应程序。
用户11396661
2024/12/09
1270
【Linux课程学习】:进程程序替换,execl,execv,execlp,execvp,execve,execle,execvpe函数
【Linux进程控制】四、exec函数族——进程替换详解
我们在使用fork()系统调用之后,创建出来的子进程是对父进程的复制,也就是说子进程和父进程执行的是相同的程序,虽然说父子进程可能执行的是不同的代码分支(if else语句),但是程序流程是一样。我们要想在新创建的子进程中执行其他程序,需要调用一种exec函数来拉起一个新的进程。当进程调用一种exec函数的时候,该进程的用户空间代码和数据全部被新程序替换掉,从新程序的启动例程开始执行。需要注意的是,调用exec并不会创建新进程,而是一种进程替换,所以调用exec前后,进程本身的ID不会改变。
mindtechnist
2024/08/08
2120
【Linux进程控制】四、exec函数族——进程替换详解
【Linux系统编程】八、进程程序替换
​ 将磁盘中指定的程序加载到内存中,让指定的进程进行执行。不论是哪种后端语言写的程序,exec* 类的函数都可以调用。
利刃大大
2025/02/23
690
【Linux系统编程】八、进程程序替换
Linux下使用exec族函数进行进程替换
版权声明:本文为博主原创文章,转载请注明博客地址: https://blog.csdn.net/zy010101/article/details/83692324
zy010101
2019/05/25
1.4K0
linux系统编程之进程(三):exec系列函数和system函数
一、exec替换进程映象 在进程的创建上Unix采用了一个独特的方法,它将进程创建与加载一个新进程映象分离。这样的好处是有更多的余地对两种操作进行管理。当我们创建 了一个进程之后,通常将子进程替换成新
s1mba
2018/01/03
2.2K0
linux系统编程之进程(三):exec系列函数和system函数
温故Linux后端编程(二):进程
fork调用一次返回两次 父进程中返回子进程id (就是大于0的意思) 子进程返回0 读时共享写时复制,可保高效
看、未来
2021/09/18
7310
【Linux】详解进程程序替换
用fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个程序。当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程开始执行。调用exec并不创建新进程,所以调用exec前后该进程的id并未改变。程序替换所做的本质工作就是将代码和数据加载到内存。
用户10923276
2024/03/30
1370
【Linux】详解进程程序替换
【Linux】从 fork() 到 exec():理解 Linux 进程程序替换的魔法
进程程序替换是指一个进程用另一个新的可执行程序来替换当前正在执行的程序,这个过程通过通过exec系列函数完成。在Linux或UNIX系统中,进程程序替换通常发生在一个进程通过fork()创建了子进程之后,子进程用exec()函数加载和执行另一个程序。 也就是说,进程程序替换就是在不改变进程的PID(进程ID)的情况下,用一个全新的程序来替换当前的内存空间和执行内容。 当程序调用一种exec函数时,该进程的用户空间代码和数据完全被新的程序替换,从新程序的启动例程开始执行。
Yui_
2024/10/21
1460
【Linux】从 fork() 到 exec():理解 Linux 进程程序替换的魔法
进程控制第二弹(进程程序替换)
程序运行后,调用execl函数后,我们的程序去执行了ls命令,原来的进程中printf("testexec end! ...\n"); 没有执行。
南桥
2024/04/22
1240
进程控制第二弹(进程程序替换)
Linux进程控制【进程程序替换】
子进程 在被创建后,共享的是 父进程 的代码,如果想实现自己的逻辑就需要再额外编写代码,为了能让 子进程 执行其他任务,可以把当前 子进程 的程序替换为目标程序,此时需要用到 Linux 进程程序替换相关知识
北 海
2023/07/01
2940
Linux进程控制【进程程序替换】
操作系统实验二归纳
本实验是要求在linux环境下测试fork()和exec(),并建立一个简单的shell(带cd、env、echo、help、jobs、quit命令)
Ewdager
2020/07/14
7920
操作系统实验二归纳
嵌入式Linux:子进程执行新程序
在 Linux 中,子进程在创建后可以通过 exec 系列系统调用执行一个全新的程序。
不脱发的程序猿
2024/12/05
1390
嵌入式Linux:子进程执行新程序
[操作系统] 进程程序替换
linux操作系统重大部分程序都是C语言写的,包括bash,ls等在内。用C语言写的程序都有main函数,可以接受argv和env,所以当使用**exec*e**系列的函数传入自定义的env时实际上就是给要执行的进程main传入env。
DevKevin
2025/02/12
520
[操作系统] 进程程序替换
Linux:进程控制(二.详细讲解进程程序替换)
进程程序替换是指在运行过程中将一个进程的地址空间中的代码、数据和堆栈等内容完全替换为另一个程序的代码、数据和堆栈的过程。这个过程通常是由操作系统提供的 exec 系列函数来实现的:
是Nero哦
2024/05/30
2550
Linux:进程控制(二.详细讲解进程程序替换)
相关推荐
【进程控制】
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验