It is called by the exec system. In fact, in Linux, there is no exec () function form. Exec refers to a group of functions, with a total of six functions:
# 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 execve (const char * path, char * const argv [], char * const envp []);
|
Among them, only execve is a real system call, and others are packaged library functions on this basis.
The exec function family finds an executable file based on the specified file name and uses it to replace the content of the calling process. In other words, it executes an executable file within the calling process. The executable file can be either a binary file or any script file that can be executed in Linux.
Unlike in general, functions in the exec function family are not returned after successful execution because the entity of the called process, includingCodeSegments, data segments, and stacks have been replaced by new content, leaving only the information on the surface of the process ID to be unchanged, which is quite like "golden shell" in ". It looks like an old shell, but it has injected a new soul. Only when the call fails will they return a-1, from the originalProgramThe call point is then executed.
Now we should understand how a new program is executed in Linux. Every time a process thinks that it cannot make any contribution to the system and support, it can make full use of the last point, call any exec to regenerate itself with a new look; or, more commonly, if a process wants to execute another program, it can fork a new process, then call any exec, which looks like a new process is generated by executing the application.
In fact, the second scenario is so widely used that Linux has made special optimizations for it. We already know that, fork will copy all the content of the calling process to the newly generated child process, which consumes a lot of time. If fork is finished, we will call exec immediately, these hard-to-copy items will be immediately erased, which seems very uneconomical, so people have designed"Copy-on-write (copy-on-write) technology, so that after the fork is finished, it does not copy the content of the parent process immediately, but is copied only when it is actually practical, in this way, if the next statement is exec, it will not be useless, which improves the efficiency.