2022-1-14 Niuke C + + Project -- exec function family learning

Questions for review:
1. What is a function family?
2. What is the function of exec function family?
3. What will happen if exec is used in the parent process function? What better way?
4. execl function, execlp function

Function families are functions with the same or similar functions. Because there is no function overload in C language, function families are used.

What will happen if exec is used in the parent process function? What better way?
If the execution is successful, the entity of the original process call after the exec function will be replaced by the newly called process entity. It is impossible to use the code after the original parent process.
A better way is to create a child process, and then execute the exec function in the child process. The child process is the clone of the parent process, and there is no loss if it is replaced.

As can be seen from the figure below, when exec is executed, the user area of the original process is replaced completely, and then the user area of a.out is connected to the kernel area of the original process.

What are the functions of the exec function family?

execl() function learning

/*
#include <unistd.h>
int execl(const char *path, const char *arg, ...);
Parameter: - path: pathname of the file to execute
        arg: Parameter list of the executable to execute
        For example/ a.out a b c 
        The first parameter is usually the name of the executable
        The following parameters are the parameters passed to the executable.
        Finally, the parameter needs to be NULL to mark the end of the parameter list (used as a sentry)

Return value: only when the call fails will there be a return value, and - 1 will be returned
 If the call is successful, there will be no return value (if they are replaced, a fart return value is required)        
*/
#include <unistd.h>
#include<sys/types.h>
#include<stdio.h>
int main(void){
    pid_t pid = fork();

    if(pid > 0){
        printf("i am parent process,pid = %d\n",getpid());
    }
    else if(pid == 0){
        execl("hw","hw",NULL);
        printf("i am child process, pid = %d\n",getpid());

    }
    for(int i = 0;i < 3;++i){
        printf("i = %d , pid = %d\n",i,getpid());
    }
    //I guess this code will only be executed in the parent process, and the pid is the parent process
    return 0;
}      

The direct use operation failed. I don't know why. It succeeded only after using gcc.
Directly use the results of the run

Results after using gcc

But why hello_ The contents of the world are not printed together?
Because of the orphan process, it is not printed together.

To print together, you can add a sleep (1) to the parent process


In addition to our own executable files, we can also use the system executable files.

#include <unistd.h>
#include<sys/types.h>
#include<stdio.h>
int main(void){
    pid_t pid = fork();

    if(pid > 0){
        printf("i am parent process,pid = %d\n",getpid());
        sleep(1);
    }
    else if(pid == 0){
        //execl("hw","hw",NULL);
        //The above is an executable file, or you can execute shell commands
        execl("/bin/ps","aux",NULL);
        printf("i am child process, pid = %d\n",getpid());

    }
    for(int i = 0;i < 3;++i){
        printf("i = %d , pid = %d\n",i,getpid());
    }
    //I guess this code will only be executed in the parent process, and the pid is the parent process
    return 0;
}                    

execlp function

#include <unistd.h>
int execlp(const char *file, const char *arg, ...);
The path of the executable file will be found in the environment variable. If it is found, it will be executed. If it is not found, it will not be executed.
Parameter: - file: the file name of the executable to be executed
arg: parameter list of executable to execute
a.out a b c
ps
The first parameter is usually the name of the executable
The following parameters are the parameters passed to the executable.
Finally, the parameter needs to be NULL to mark the end of the parameter list (used as a sentry)

Return value: only when the call fails will there be a return value, and - 1 will be returned
If the call is successful, there will be no return value (if they are replaced, a fart return value is required)

int execv(const char *path, char *const argv[]);
Use a string array to pass parameters
char *argv[] = {"a.out","a","b","c",NULL}
execv("/bin/ps",argv);

int execvpe(const char *file, char *const argv[], char const envp[]);
The last parameter is used to add environment variables
char envp[] = {"/home/three","/home/aaa"}

Keywords: C++ Back-end

Added by burhankhan on Tue, 18 Jan 2022 20:37:17 +0200