Principle of Program Replacement

Article Directory

1. Program replacement

Program replacement is used to replace all the code and data in the user space of the current process with the code and data of the new program. Program replacement does not create a new process, but uses the current process to execute the code of the new program.After fork creates a child process, the child process executes the code of the parent process by default, and the child process can execute the new program through program replacement

exec series of functions, header file 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 execvpe(const char *file, char *const argv[], char *const envp[]);

Return value: If the call is successful, the new program will be executed directly. If there is no return value, -1 will be returned if an error occurs . The functions of the exec series will return only when an error occurs.

parameter:

  • path: The path to the executable program to replace
  • file: the file name of the replaced executable program, the operating system will automatically search for the executable program in the environment variable PATH
  • arg and varargs... : the command-line arguments of the executable program to replace, the last argument in varargs must be NULL
  • argv array: an array of command line parameters, the last element must be NULL
  • envp array: An array of environment variables, the last element must be NULL
//process.c -> myprocess
#include <stdio.h>

int main()
{
    
    
    printf("hello, I am myprocess\n");

    return 0;
}

The functions of the exec series can be memorized by referring to the following methods:

  • l indicates that the command line parameters are passed in the form of a list list
  • v indicates that the command line parameters are passed as a vector array
  • p indicates that the operating system automatically searches in the environment variable PATH
  • e indicates that the user maintains environment variables by himself
// test.c -> mytest
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    
    
    printf("hello, I am mytest\n");

    // 程序替换
    char* const myargv[] = {
    
    "myprocess", NULL};
    execv("/home/starrycat/code/Linux/learn/learn_6_16/myprocess", myargv);
    // execl("/home/starrycat/code/Linux/learn/learn_6_16/myprocess", "myprocess", NULL);

    // 出错才会返回
    exit(1); 
}

insert image description here

// test.c -> mytest
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    
    
    printf("hello, I am mytest\n");

    // 程序替换
    // char* const myargv[] = {"myprocess", NULL};
    // execvp("myprocess", myargv);
    execlp("myprocess", "myprocess", NULL);

    // 出错才会返回
    exit(1); 
}

insert image description here

//process.c -> myprocess
#include <stdio.h>

int main()
{
    
    
    printf("hello, I am myprocess\n");

    extern char** environ;
    for (int i = 0; environ[i]; ++i)
      printf("%d : %s\n", i, environ[i]);

    return 0;
}

// test.c -> mytest
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    
    
	// 程序替换
	char* const myargv[] = {
    
    "myprocess", NULL};
    char* const myenvp[] = {
    
    "MYENV=you can see me!", NULL};
    // execle("/home/starrycat/code/Linux/learn/learn_6_16/myprocess", "myprocess", NULL, myenvp);
	execve("/home/starrycat/code/Linux/learn/learn_6_16/myprocess", myargv, myenvp); // 系统调用

    // 出错才会返回
    exit(1); 
}

insert image description here

The bottom layer of the exec series of library functions is to call the system call execve

int execve(const char *filename, char *const argv[], char *const envp[]);

Guess you like

Origin blog.csdn.net/qq_70793373/article/details/131236191