美文网首页
exec系列函数介绍

exec系列函数介绍

作者: nyhoo | 来源:发表于2018-08-26 19:18 被阅读0次

介绍

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

#include <unistd.h>
extern char **environ;
//带l的表示list每个参数的传递是通过list的方式传递进去的
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[]);
//带v的表示vector参数的传递是通过vector的方式传递的,其中p代表从环境变量PATH中查找,e从传递的环境变量中获取。
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[]);

例子

进程调用exec函数替换进程映像
// prog.cpp
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main()
{
  char* const prog[3]  =
  {
    "test_prog",//modify name to test_prog
    "20",// the argv...
    NULL // the last must NULL
  };

  printf("start call exec function current pid [%d]\n",getpid());
  execvp("sleep",prog);
  printf("end call exec function\n");// can not output
    return 0;
}


prog运行结果
子进程拉起进程父进程等待子进程结束
// prog_sub.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
  char* const prog[3]  =
  {
    "test_prog_sub",//modify name to test_prog_sub
    "10",// the argv...
    NULL // the last must NULL
  };

  pid_t pid = fork();
  if(pid<0)
  {
    printf("fork child process error\n");
    exit(-1);
  }
  else if(pid==0)
  {
    printf("start call exec function current pid [%d]\n",getpid());
    execvp("sleep",prog);
  }
  int status=0;
  do {
    pid_t child_pid = wait(&status);
    if(child_pid==-1)
    {
      printf("wait pid error \n");
    }
    if(WIFEXITED(status))
    {
      printf("child process end that pid [%d] exit code [%d]",child_pid,WEXITSTATUS(status));
    }
  } while((!WIFEXITED(status) && !WIFSIGNALED(status)));

    return 0;
}
prog_sub运行结果图

相关文章

  • exec系列函数介绍

    介绍 fork函数是用于创建一个子进程,该子进程几乎是父进程的副本,而有时我们希望子进程去执行另外的程序,exec...

  • 进程控制二

    函数exec 函数介绍 这几个函数若出错返回-1,成功不返回。 这几个函数的区别 我们看到这几个函数都是exec开...

  • [python3] exec()函数

    exec()介绍 exec(str [, globals [, locals]]函数执行一个表达式字符串并返回结果...

  • php命令执行函数

    Exec函数 Exec函数的语法为: exec ( string $command [, array &$outp...

  • 代码审计——命令执行

    了解命令执行函数 system()passthru() 需回显示函数exec() shell_exec() ` `...

  • PHP的exec()

    php中可以使用 exec() 函数调用外部函数。 语法: string exec ( string $comma...

  • C 中的 exec 系列函数

    exec系列函数都具有用新进程替换当前正在运行的进程的功能。它可以通过一个C程序来运行另一个C程序。且允许进程运行...

  • PostgreSQL 源码解读(12)- 插入数据#11(exe

    本文简单介绍了PG插入数据部分的源码,主要内容包括exec_simple_query函数的实现逻辑,该函数位于sr...

  • Python可执行对象——exec、eval、compile

    Python提供的调用可执行对象的内建函数进行说明,涉及exec、eval、compile三个函数。exec语句用...

  • exec函数族

    fork()函数通过系统调用创建一个与原来进程(父进程)几乎完全相同的进程(子进程是父进程的副本,它将获得父进程数...

网友评论

      本文标题:exec系列函数介绍

      本文链接:https://www.haomeiwen.com/subject/ysnoiftx.html