当前位置:网站首页>Use of exec series functions (EXECL, execlp, execle, execv, execvp)

Use of exec series functions (EXECL, execlp, execle, execv, execvp)

2022-06-26 10:09:00 Ruo_ Xiao

One 、exec Replace process image

In process creation Unix It took a unique approach , It will Process creation and loading a new process image are separated . The advantage is that there's more room to manage both operations .

When we create a process , The child process is usually replaced with a new process image , It works exec A series of functions . Of course ,exec A series of functions can also replace the current process .

for example : stay shell Command line execution ps command , It's actually shell Process call fork Copy a new child process , Using exec The system call completely replaces the newly generated subprocess with ps process .

Two 、exec Series of functions (execl、execlp、execle、execv、execvp)

function :

         use exec Function can replace the current process with a new process , The new process is the same as the original process PID.exec Under the name is a complete series composed of multiple correlation functions .

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[]);

path Parameter indicates the name of the program you want to start, including pathname .

arg The parameter indicates the parameter of the startup program , Generally, the first parameter is the name of the command to be executed , Not with path and arg Must be NULL end .

Return value : Successfully returns 0, Failure to return -1.

notes : Above exec The bottom layer of a series of functions is through execve System call implementation .

#include <unistd.h>

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

DESCRIPTION:
       execve() executes the program pointed to by filename.  
       filename must be either a binary executable, or a script 
starting with  a  line  of  the form

above exec The difference between a series of functions :

1、 belt l Of exec function :execl、execlp、execle, Indicates that the following parameters are given in the form of variable parameters and all end with a null pointer .

Example :

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

int main(void)
{
    printf("entering main process---\n");
    execl("/bin/ls","ls","-l",NULL);
    printf("exiting main process ----\n");
    return 0;
}

result :

entering main process---
Total usage 24
-rwxr-xr-x 1 xcl xcl 16456 6 month   7 09:11 execl
-rw-r--r-- 1 xcl xcl   215 6 month   7 09:11 execl.c

utilize execl Put the current process main Replace , All the last printed statements will not be output . 

2、 belt p Of exec function :execlp、execvp, Represents the first parameter path Do not enter the full path , Just give the command name , It will be in the environment variable PATH Find the command in

Example :

When not p But when the full path is not given :

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

int main(void)
{
    printf("entering main process---\n");
    execl("ls","-l",NULL);
    printf("exiting main process ----\n");
    return 0;
}

result :

entering main process---
exiting main process ----

The result shows that... Could not be found , All replacements failed ,main The process continues

Now bring it p:

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

int main(void)
{
    printf("entering main process---\n");
    execlp("ls","-l",NULL);
    printf("exiting main process ----\n");
    return 0;
}

  result :

entering main process---
main  main.c

Replacement successful .

3、 No l Of exec function :execv、execvp Represents the parameters required by the command to char *arg[] The form is given and arg The last element must be NULL .

Example :

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

int main(void)
{
    printf("entering main process---\n");
    int ret;
    char *argv[] = {"ls","-l",NULL};
    ret = execvp("ls",argv);
    if(ret == -1)
        perror("execl error");
    printf("exiting main process ----\n");
    return 0;
}

result :

entering main process---
Total usage 24
-rwxr-xr-x 1 xcl xcl 16512 6 month   7 09:21 main
-rw-r--r-- 1 xcl xcl   306 6 month   7 09:21 main.c

Process replacement succeeded .

4、 belt e Of exec function :execle Express , Pass the environment variable to the process that needs to be replaced

From the above function prototype, we find that :

extern char **environ;

Here environ It's an array of Pointers , Every pointer in it points to char by “XXX=XXX”

environ The data storing the environment information can env Command view :

QQ Screenshot 20130712231740

It consists of shell The process is passed to the current process , Then the current process passes it to the new process to be replaced .

Example :execle.c

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

int main(int argc, char *argv[])
{
    //char * const envp[] = {"AA=11", "BB=22", NULL};
    printf("Entering main ...\n");
    int ret;
    ret =execl("./hello", "hello", NULL);
    //execle("./hello", "hello", NULL, envp);
    if(ret == -1)
        perror("execl error");
    printf("Exiting main ...\n");
    return 0;
}

 hello.c

#include <unistd.h>
#include <stdio.h>
extern char** environ;

int main(void)
{
    printf("hello pid=%d\n", getpid());
    int i;
    for (i=0; environ[i]!=NULL; ++i)
    {
        printf("%s\n", environ[i]);
    }
    return 0;
}

result :

QQ Screenshot 20130712232918

It can be seen that the original process does pass the environment variable information to the new process .

So now we can use execle The function itself gives the environment variable information that needs to be passed :

The sample program :execle.c

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

int main(int argc, char *argv[])
{
    char * const envp[] = {"AA=11", "BB=22", NULL};
    printf("Entering main ...\n");
    int ret;
    //ret =execl("./hello", "hello", NULL);
    ret =execle("./hello", "hello", NULL, envp);
    if(ret == -1)
        perror("execl error");
    printf("Exiting main ...\n");
    return 0;
}

hello.c

#include <unistd.h>
#include <stdio.h>
extern char** environ;

int main(void)
{
    printf("hello pid=%d\n", getpid());
    int i;
    for (i=0; environ[i]!=NULL; ++i)
    {
        printf("%s\n", environ[i]);
    }
    return 0;
}

result :

Entering main ...
hello pid=13886
AA=11
BB=22 

  Indeed, the given environment variables are passed .

Reprint :exec Series of functions (execl,execlp,execle,execv,execvp) Use _ning1350 The blog of -CSDN Blog _execlp and execvp

(SAW:Game Over!)

原网站

版权声明
本文为[Ruo_ Xiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260918361101.html