Linux exec () function

Source: Internet
Author: User

YesExec system callIn fact, in Linux, there is no exec () function form. Exec refers to a group of functions. There are 6 Functions in total:

# Include <unistd. h>

Extern char ** environ;

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 calling process, including the code segment, data segment, and stack, has been replaced by new content, only the information on some tables such as the process id remains unchanged. It is quite similar to the "golden shell" in the "Plan ". It looks like an old shell, but it has injected a new soul. Only when the call fails will they return a-1, which is executed from the original program's call point.

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 or the user, it can give full play to 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 a technology called "Copy-on-write, so that the fork does not copy the content of the parent process immediately after the end, but is copied only when the fork is actually practical, so that if the next statement is exec, it will not be useless, this improves efficiency.

Return Value
If the execution is successful, the function will not return. If the execution fails,-1 will be returned. The cause of the failure is stored in errno.

In normal programming, if you use the exec function family, you must add an incorrect judgment statement. Compared with other system calls, exec is prone to injury, and the execution file location, permissions, and many other factors can cause the call to fail. The most common errors are:
1. The file or path cannot be found, and errno is set to enoent;
2. If the array argv and envp forget to end with null, errno is set to efault;
3. You do not have permission to run the file to be executed. errno is set to eacces.

L indicates calling in the form of a parameter list

V indicates calling in the form of parameter Arrays

E indicates that environment variables can be passed

P indicates the file to be searched for in the path. If the given path is not an absolute path, the file with the corresponding name will be searched in the path. If the path is not set, it will be in/bin by default, search under/usr/bin.

In addition, the parameter must end with null. File descriptors opened by the original process will not be closed in Exec unless they are set with fcntl to "close on EXEC) and the directory stream opened by the original process will be closed in the new process.

Example:

# Include <unistd. h>
Int main (INT argc, char * argv [])
{
Char * envp [] = {"Path =/tmp", "user = lei", "status = testing", null };
Char * argv_execv [] = {"Echo", "excuted by execv", null };
Char * argv_execvp [] = {"Echo", "executed by execvp", null };
Char * argv_execve [] = {"env", null };
If (Fork () = 0 ){
If (execl ("/bin/echo", "Echo", "executed by execl", null) <0)
Perror ("Err on execl ");
}
If (Fork () = 0 ){
If (execlp ("Echo", "Echo", "executed by execlp", null) <0)
Perror ("Err on execlp ");
}
If (Fork () = 0 ){
If (execle ("/usr/bin/ENV", "env", null, envp) <0)
Perror ("Err on execle ");
}
If (Fork () = 0 ){
If (execv ("/bin/echo", argv_execv) <0)
Perror ("Err on execv ");
}
If (Fork () = 0 ){
If (execvp ("Echo", argv_execvp) <0)
Perror ("Err on execvp ");
}
If (Fork () = 0 ){
If (execve ("/usr/bin/ENV", argv_execve, envp) <0)
Perror ("Err on execve ");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.