[Linux]进程程序替换

[Linux]进程程序替换

进程程序替换的意义

Linux系统下使用fork系统函数创建子进程后,子进程只能执行继承的部分父进程代码,如果要想让子进程执行一份单独的代码就要进行进程程序替换。

见一见进程程序替换

进程程序替换的头文件和函数如下:

image-20230831104800830

编写如下代码来见一见进程程序替换:

#include <stdio.h>
#include <unistd.h>

int main()
{
    
    
  printf("进程程序替换前\n");
  printf("进程程序替换前\n");
  printf("进程程序替换前\n");
  execl("/bin/ls", "ls", "-a", "-l", NULL);
  printf("进程程序替换后\n");
  printf("进程程序替换后\n");
  printf("进程程序替换后\n");
  return 0;
}

编译代码并运行查看结果:

image-20230831104439925

可以看出,进程程序替换后,进程不再执行原有的代码而是转而执行替换后的代码。进程替换函数后的代码不再被执行,因此可以看出程序替换是整体替换,替换后原有的代码和数据都不存在了。

进程程序替换的原理

进程程序替换是在不修改进程pcb中的id的情况下,将磁盘中的可执行程序的代码和数据传送到内存中,替换进程原有的代码和数据,并且修改页表映射,完成进程程序的替换,示意图如下:

image-20230831104507648

  • 从进程的角度看:代码和数据被操作系统替换了。
  • 从程序的角度看:自身的代码和数据被加载到了内存中。

程序加载的原理: 在Linux操作系统下,启动的任何一个进程都是shell进程的子进程,启动进程的就是先让shell进程创建一个子进程的pcb,然后用我们编写好的进程的代码和数据替换这个shell进程的子进程的,从而完成进程的加载启动。

进程程序替换中的写时拷贝

为了体会进程程序替换中的写时拷贝,编写如下代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    
    
  pid_t id = fork();
  if (id == 0)
  {
    
    
    //子进程
    printf("我是子进程,我的pid:%d\n", getpid());
    execl("/bin/ls", "ls", "-a", "-l", NULL);
  }
  waitpid(id, NULL, 0);
  printf("我是父进程,我的pid:%d\n", getpid());
  return 0;
}

编译代码运行并且查看结果:

image-20230831104608439

可以看出进程程序替换是不影响父进程的代码和数据的,因为在替换子进程的代码和数据时,发生了写时拷贝,另外可以看出实际上代码区的数据是可以修改的。

介绍进程程序替换接口

返回值

进程程序替换函数只有在执行失败时才会有返回值,如果进程程序替换失败了会返回-1,并且设置错误码。由于进程程序替换函数的替换成功后,该函数内部的返回代码也被替换了,因此一旦执行成功是不会有返回值的。一旦替换失败了,由于进程程序替换不会修改pcb,因此也不影响父进程接收子进程的退出码。

编写如下代码进行测试:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
    
    
  pid_t id = fork();
  if (id == 0)
  {
    
    
    //子进程
    int n = execl("/bin/lsss", "lsss", "-a", "-l", NULL);
    printf("我是子进程,我的id:%d, 程序替换失败,n: %d\n", getpid(), n);
    exit(1);
  }
  int status = 0;
  waitpid(id, &status, 0);
  printf("我是父进程,子进程退出码:%d\n", WEXITSTATUS(status));
  return 0;
}

编译代码运行并查看结果:

image-20230831104523903

由于我们传入的替换程序是错误的,进程程序替换失败了,接收到了返回值-1,父进程也正常接收到了退出码-1。

再编写如下代码进行测试:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
    
    
  pid_t id = fork();
  if (id == 0)
  {
    
    
    //子进程
    int n = execl("/bin/ls", "ls", "hello.txt", NULL);//该文件不存在
    printf("我是子进程,我的id:%d, 程序替换失败,n: %d\n", getpid(), n);
    exit(1);
  }
  int status = 0;
  waitpid(id, &status, 0);
  printf("我是父进程,子进程退出码:%d\n", WEXITSTATUS(status));
  return 0;
}

编译代码运行并查看结果:

image-20230831102532182

可以看出即使程序替换成功了,也不影响子进程退出码的接收。

execl函数

//execl函数声明
int execl(const char *path, const char *arg, ...);
  • path参数 – 要替换的程序的路径
  • arg参数 – 可变参数,接收替换程序的命令行参数,以NULL结尾

编写如下代码进行测试:

#include <stdio.h>
#include <unistd.h>

int main()
{
    
    
  printf("进程程序替换前\n");
  execl("/bin/ls", "ls", "-a", "-l", NULL);
  printf("进程程序替换失败\n");
  return 0;
}

编译代码运行并查看结果:

image-20230831102536883

execv函数

//execv函数声明
int execv(const char *path, char *const argv[]);
  • path参数 – 要替换的程序的路径
  • argv参数 – 接收要替换程序的命令行参数,以NULL结尾

编写如下代码进行测试:

#include <stdio.h>
#include <unistd.h>

int main()
{
    
    
  printf("进程程序替换前\n");
  //execl("/bin/ls", "ls", "-a", "-l", NULL);
  char * const argv[] = {
    
    
    "ls",
    "-a",
    "-l",
    "-n",
    NULL
  };
  execv("/bin/ls", argv);
  printf("进程程序替换失败\n");
  return 0;
}

编译代码运行并查看结果:

image-20230831104539226

execlp函数

//execlp函数声明
int execlp(const char *file, const char *arg, ...);
  • file参数 – 要替换的程序名,会自动去环境变量的路径中查找该程序
  • arg参数 – 可变参数,接收替换程序的命令行参数,以NULL结尾

编写如下代码进行测试:

#include <stdio.h>
#include <unistd.h>

int main()
{
    
    
  printf("进程程序替换前\n");
  execlp("ls", "ls", "-l", "-n", NULL);
  printf("进程程序替换失败\n");
  return 0;
}

编译代码运行并查看结果:

image-20230831104738842

execvp函数

//execvp函数声明
int execvp(const char *file, char *const argv[]);
  • file参数 – 要替换的程序名,会自动去环境变量的路径中查找该程序
  • argv参数 – 接收要替换程序的命令行参数,以NULL结尾

编写如下代码进行测试:

#include <stdio.h>
#include <unistd.h>

int main()
{
    
    
  printf("进程程序替换前\n");
  char * const argv[] = {
    
    
    "ls",
    "-l",
    "-n",
    NULL
  };
  execvp("ls", argv);
  printf("进程程序替换失败\n");
  return 0;
}

编译代码运行并查看结果:

image-20230831093047903

execle函数

//execle函数声明
int execle(const char *path, const char *arg, ..., char * const envp[]);
  • path参数 – 要替换的程序的路径
  • arg参数 – 可变参数,接收替换程序的命令行参数,以NULL结尾
  • envp参数 – 接收传给替换程序的环境变量,以NULL结尾,覆盖式传入,接收的进程中只会有传入的环境变量

编写如下文件目录结构:

image-20230831100637858

使用myproc程序和otherproc程序进行测试,其代码内容如下

//myproc
#include <stdio.h>
#include <unistd.h>

int main()
{
    
    
  printf("进程程序替换前\n");
  char * const envp[] = {
    
    
    "MYENV=YOUCANSEEME",
    NULL 
  };
  execle("../otherproc/otherproc", "otherproc", NULL, envp);
  printf("进程程序替换失败\n");
  return 0;
}
//otherproc
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    
    
  cout << " MYENV: " << (getenv("MYENV")==NULL?"NULL":getenv("MYENV")) << endl;
  cout << " PATH: " << (getenv("PATH")==NULL?"NULL":getenv("PATH")) << endl;
  return 0;
}

使用otherproc程序替换myproc程序,并且只传入只含有一个环境变量的参数envp。

编译代码运行并查看结果:

image-20230831101123814

由于execle函数传入环境变量采用的是覆盖式传入,因此替换程序只有传入的一个环境变量。

补充:

  • 可以使用全局变量environ让替换程序获取父进程的全部环境变量。
  • shell进程创建子进程时也是调用这样的系统接口将环境变量传入。

execvpe函数

int execvpe(const char *file, char *const argv[], char *const envp[]);
  • file参数 – 要替换的程序名,会自动去环境变量的路径中查找该程序
  • argv参数 – 接收要替换程序的命令行参数,以NULL结尾
  • envp参数 – 接收传给替换程序的环境变量,以NULL结尾,覆盖式传入,接收的进程中只会有传入的环境变量

补充知识

程序替换函数的命名规律:

  • l后缀 – 以可变参数形式接收替换程序的命令行参数
  • v后缀 – 以指针数组的形式接收替换程序的命令行参数
  • p后缀 – 传入替换程序的程序名,会自动在环境变量的路径中查找
  • e后缀 – 以指针数组的形式接收替换程序的环境变量

以上所有的进程程序替换系统调用都是对execve系统调用函数的封装,不同的封装是为了更适合不同的应用场景。

猜你喜欢

转载自blog.csdn.net/csdn_myhome/article/details/132598332