Linux Multi-task programming (iii) EXEC function family and its basic experiment

Source: Internet
Author: User

exec function Family

Function Family Description

The fork () function is used to create a new child process that almost duplicates the entire contents of the parent process, but how does this newly created subprocess execute? The EXEC function family provides a way to start another program execution in a process. It can find the executable file based on the specified file name or directory name, and use it to replace the data segment, code snippet, and stack segment of the original calling process, and after the execution, the contents of the original calling process are replaced by the new process, except for the process extra. In addition, the executable file here can be either a binary file or a script file that can be executed under Linux.

There are two main scenarios for using the EXEC function family in Linux:

When the process believes that it can no longer make any contribution to the system and users, you can invoke any function in the EXEC function family to regenerate itself.

If a process wants to execute another program, it can call the fork () function to create a new process, and then call any of the functions in the EXEC function family so that it looks like a new process has been created by executing the application (which is very common).

Function Family Syntax

Instead of the EXEC () function in Linux, there are 6 functions that start with exec, and there are nuances of syntax between them. The following table lists the syntax of the 6 member functions of the EXEC function family:

These 6 functions are slightly different in terms of the function name and the rules for using the syntax, and the following is a comparison of the ways in which executable files are found, how they are passed, and the environment variables.

How to find: the first 4 functions in table 1 find the full file directory path, and the last two functions (that is, two functions ending with P) can give only the file name, and the system automatically looks for the path specified by the environment variable "$PATH".

Parameter passing mode: There are two kinds of parameters passing in the EXEC function family: One is enumerating each other, and the other is passing all the parameters to the whole array of pointers. This is distinguished by the 5th-digit letter of the function name, the letter "L" (list) is an enumeration of arguments, whose syntax is const char *arg, and the representation of the letter "V" (vector) passes all parameters as a whole to the array of pointers, with the syntax of Char *const Argv[]. The arguments here are actually all the command option strings (including the executable command itself) that are required for the user to use the executables. It is important to note that these parameters must end with null.

Environment variables: The EXEC function family can default to the system's environment variables, or you can pass in the specified environment variable. Here the two functions at the end of "E" (Environment) execle () and Execve () can specify the environment variables used by the current process in envp[.

Table 2 makes a summary of the function names and corresponding grammars in these 6 functions, and mainly points out the meaning of each digit in the function name, and remembers these 6 functions in this table.

In fact, the real system calls in these 6 functions are only EXECVE (), the other 5 are library functions, and they will eventually invoke the system call EXECVE (). When using the EXEC function family, be sure to add the error judgment statement. The most common causes of exec failures are easy to perform:

① could not find the file or path, at which point errno was set to Enoent.

② Arrays argv and envp forget to end with NULL, at which point errno is set to Efaul.

③ does not have permission to run the executable file, at which point errno is set to Eacces.

Related Article

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.