Three tasks of Linux multitasking: Exec function family and its basic experiment

Source: Internet
Author: User

Source: CSDN Wang Wensong transfer from: Linux commune

exec function Family

Description of the function family

The fork () function is used to create a new child process that almost duplicates the entire contents of the parent process, but how does the newly created child process execute? The EXEC function family provides a way to start another program execution in the process. It can find the executable file according to 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, after execution, the contents of the original calling process are replaced by the new process except for the process. 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 cases of using the EXEC function family in Linux:

When a process thinks that it can no longer make any contribution to the system and the user, it 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, which looks like a new process is generated by executing the application (which is very common).

Function Family Syntax

In fact, there is no EXEC () function in Linux, but rather the 6 functions that begin with exec, the syntax between them is slightly different. The following table lists the syntax for the 6 member functions of the EXEC function family:

These 6 functions have subtle differences between the function name and the rules that use the syntax, which are compared in terms of how the executable looks, how the parameters are passed, and the environment variables.

Look up: The first 4 functions in table 1 are found in the full file directory path, and the last two functions (i.e. two functions ending in P) can only give the file name, and the system will automatically find the path specified by the environment variable "$PATH".

Parameter passing: the EXEC function family parameters are passed in two ways: one is enumerated each way, and the other is to pass all parameters as a whole array of pointer arrays. Here is the 5th letter of the function name to differentiate, the letter "L" (list) of the way to enumerate the parameters, its syntax is const char *arg, the letter "V" (vector) is a representation of all parameters to construct a pointer array, its syntax is char *const Argv[]. The parameters here are actually all of the command option strings that the user needs to use the executable, including the executor command itself. It is important to note that these parameters must end with null.

Environment variables: The EXEC function family can default the environment variable of the system, or it can pass in the specified environment variable. Here, the two functions execle () and Execve () ending with "e" (environment) can specify the environment variables used by the current process in envp[].

Table 2 again to the function name and corresponding syntax in the 6 functions of a summary, the main point of the function name of each corresponding to indicate the meaning of this table to remember the 6 functions.

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

① the file or path was not found, errno is set to ENOENT.

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

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

Basic experiments

Experiment 1

This experiment shows you how to use the file name to find the executable file and how to use the parameter list. The function used here is EXECLP (). The program code is as follows:

In this program, you first use the fork () function to create a child process and then use the EXECLP () function in a child process. As you can see, the parameter list here lists the command names and options used in the shell, and when the file name is used, the system looks for the executable in the default environment variable path.

Using the command: GCC execlp.c-o EXECLP compiled and then executed, the results are as follows:

Using the ENV command, you can view the path name of the environment variable

The result of this program is the same as the direct input command "PS-EF" in the shell, and of course there may be different results at different times in different systems.

Experiment 2

The function of this experiment is the same as that of experiment 1, and different function is used. This experiment will use the full file directory to find the corresponding executable file. Note that the directory must start with "/" or it will be treated as a file name. The program code is as follows:

Write the save source file, and then use the command: GCC execl.c-o execl compile, and then execute the command:./EXECL, you can see that the experimental results are the same as in experiment 1.

Experiment 3

This experiment is to use the execle () function to add environment variables to the new subprocess, where "env" is the command to view the current process environment variables, the experiment code is as follows:

After writing the save source file, use the command: GCC execle.c-o execle, and then execute the command:./execle, execute the results as

Experiment 4

The experiment realizes the function and the experiment 3, the difference is uses the EXECVE () function, by constructs the pointer array way to pass the parameter, the attention parameter list must be NULL as the end identifier, the experiment code is as follows:

Write save source file, use command: GCC execve.c-o execve, then execute command:./EXECVE, the result is as follows:

To this end of the process execution experiment, the next section of the learning process

Linux multitasking three: the EXEC function family and its basic experiment (GO)

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.