Linux: Program replacement

Program replacement

Program replacement simply means to reload another program into the memory; then point the memory pointer of an existing pcb to the memory space to point to the new program, and then the existing pcb runs to schedule the new program Up

Replacement principle

After the child process is created with fork, it executes the same program as the parent process (but may execute different code branches), and the child process often calls an exec function
to execute another program. When a process calls an exec function, the user space code and data of the process are completely replaced by the new program, and
execution starts from the startup routine of the new program . Calling exec does not create a new process, so the id of the process does not change before and after calling exec.
Insert picture description here

Replacement method

The implementation of program replacement is mainly through the exec function family, which refers to 6 kinds of functions starting with exec

#include <unistd.h>`//头文件
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 execvp(const char *path, char *const argv[],char *const envp[]);

Note : If these functions are successfully called, a new program will be loaded and executed from the startup code, and will not return. If the call fails, it will return -1, so the exec function only has an error return value but no success return value.

Function law

As we can see from the above, these function prototypes look the same and it is easy to be confused, but in fact there are certain rules between them, as long as we master these rules, it is easy to distinguish them.

l(list) : 表示参数采用列表
v(vector) : 参数用数组
p(path) : 有p自动搜索环境变量PATH
e(env) : 表示自己维护环境变量
Function name Parameter format With path Whether to use current environment variables
execl List Is not Yes
execlp List Yes Yes
execle List Is not No, you need to assemble the environment variables yourself
execv Array Is not Yes
execvp Array Yes Yes
execve Array Is not No, you need to assemble the environment variables yourself

Summary :
In fact, in general, it is
(1) the difference between l and v: the method of assigning program operating parameters is different
(2) whether there is a difference between p: whether the name of the new program file needs to have a path
(3) whether there is e Difference: Whether to customize environment variables
Example:

     #include <stdio.h>                                                           
     #include <stdlib.h>
     #include <unistd.h>
     
     int main()
     {
    
    
       pid_t pid = fork();
       if(pid < 0)

      {
    
              
        perror("fork");
        return -1;     
      }           
      else if(0 == pid)
      {
    
                    
       
        char *const argv[] = {
    
    "ps","-ef",NULL};
        char *const envp[] = {
    
    "PATH=/bin:/usr/bin", "TERM=console",NULL};
        execl("/bin/ps","ps","-ef",NULL);                                
                                         
        execlp("ps","ps","-ef",NULL,envp);
                                          
        execle("ps","ps","-ef",NULL,envp);
                                          
        execv("/bin/ps",argv);
                              
        execvp("ps",argv);
                          
        execve("/bin/ps",argv,envp);
        sleep(30);                  
        exit(0);  
      }         
      else
      {
    
       
        sleep(30);
        printf("子进程退出!\n");                                                                                                                               
        while(1)  
        {
    
           
          printf("正在打麻将!\n");
        }                          
      }  

Guess you like

Origin blog.csdn.net/qq_43825377/article/details/113833817