exec family functions

 Table of contents

execve() function

 The execl() function runs the ls command

The execv() function runs the ls command

 The execlp() function runs the ls command

 The execvp() function runs the ls command

 The execle() function runs the ls command

The execvpe() function runs the ls command 


       When the job of the child process is no longer to run the code segment of the parent process, but to run the code of another new program, then the child process can run another new program through the exec function at this time.

        Why do new programs need to be executed in a subprocess? Although you can directly write the code that the sub-process needs to run in the sub-process branch, it is not flexible enough and the scalability is not good enough. Is it better to directly put the code that the sub-process needs to run in an executable file, so the exec operation appears
 

execve() function

        The system call execve() can load a new program into the memory space of a certain process. By calling the execve() function, an external executable file can be loaded into the memory space of the process to run. The old program is replaced with the new program, and the process The stack, data, and heap data of the new program will be replaced by the corresponding components of the new program, and then executed from the main() function of the new program

code 1

         Define two arrays of character pointers, and then use the execve function to replace the current process with a new program, the path name of the executed program file argv[1], the parameters and environment variables of the new process will be set to the values ​​specified in and arg_arr, env_arrIf execve-1 is returned, it means that the execution failed, and the program will print an error message and exit with a return value of -1

code 2

 

         Print the program's command-line arguments and environment variables

 verify

 The execl() function runs the ls command

The execv() function runs the ls command

         Both execl() and execv() are basic exec functions, both of which can be used to execute a new program. The difference between them is that the parameter format is different; the meaning and format of the first parameter parameter are the same, , pointing to the path name of the new program , which can be either an absolute path or a relative path. The difference between execl() and execv() lies in the second parameter, execv() is an array of string pointers; while execl() arranges the parameter list in order and passes it in the form of variable parameters, which is essentially multiple strings. NULL terminated 

 The execlp() function runs the ls command

 The execvp() function runs the ls command

 The execle() function runs the ls command

The execvpe() function runs the ls command 

         The two functions execle() and execvpe() add an e to the name. This e actually represents the environment environment variable, which means that these two functions can specify a custom environment variable list for the new program.

Guess you like

Origin blog.csdn.net/weixin_46829095/article/details/129665472