Exec function of Process Control

Source: Internet
Author: User

After a child process is created using the fork function, the child process usually calls one exec function to execute another program. When a process calls an exec function, the Program executed by the process is completely replaced with a new program, and the new program is executed from its main function. Because calling exec does not create a new process, the process ID does not change. Exec only replaces the body, Data, heap, and stack segments of the current process with a new program.

There are 6 different exec functions available, which are often referred to as exec functions. These exec functions improve the UNIX Process Control primitive. You can use fork to create new processes and exec to execute new programs. The exit function and two wait functions terminate processing and wait for termination. These are the basic process control primitives we need.

# Include <unistd. h> execl (* pathname, * arg0 ,... execv (* pathname, * execle (* pathname, * arg0 ,... execve (* pathname, * argv [], * execlp (* filename, * arg0 ,... execvp (* filename, *-. If it succeeds, no return value is returned.

Between these functionsThe first difference is that the first four take the PATH name as the parameter, and the last two (execlp and execvp, p indicates the PATH, which is personally understood) take the file name as the parameter..When filename is specified as a parameter:

  • If filename contains/, it is regarded as the path name.
  • Otherwise, search for executable files in the Directories specified by the PATH environment variable.

The PATH variable contains a directory table (called the PATH prefix) separated by a colon. For example, name = value environment string:

PATH =/bin:/usr/local/bin /:.

Specify four directories for search. The last path prefix (.) indicates the current directory. (The zero-length prefix also indicates the current directory. It can be used at the beginning of the value: indicates that, in the middle of the row, it must be represented by:, and at the end of the row, it is represented .)

If execlp or execvp finds an executable file using one of the path name prefixes, but the file is not a machine executable file generated by the connection Editor, it is considered as a shell script, so try to call/bin/sh and use the filename as the shell input.

The second difference is related to parameter table transmission (l indicates list, v indicates vector ).The execl, execlp, and execle functions require that each command line parameter of the new program be described as a separate parameter. This parameter table ends with a null pointer. For the other three functions (execv, execvp, and execve), we should first construct a pointer array pointing to each parameter, and then use the array address as the parameters of the three functions.

Before the iso c prototype, the common methods for the execl, execle, and execlp functions to represent the command line parameters are:

Char * arg0, char * arg1 ,..., Char * argn, (char *) 0

It should be noted that a null pointer is followed by the last command line parameter. If a constant 0 is used to represent a null pointer, it must be forcibly converted to a character pointer; otherwise, it is interpreted as an integer parameter. If the length of an integer is different from that of char *, the actual parameter of the exec function will fail.

The last difference is related to passing environment tables to new programs.The two functions ending with e (execle and execve) can pass a pointer to the Environment string pointer array. The other four functions use the environ variable in the calling process to copy the existing environment for the new program. Generally, a process allows its environment to be propagated to its sub-processes, but sometimes the process wants to specify a specific environment for the sub-process.

Before using the iso c prototype, the execle parameter is:

Char * pathname, char * arg0 ,..., Char * argn, (char *) 0, char * envp []

It can be seen that the last parameter is the address of the array consisting of character pointers pointing to the Environment string. In the iso c prototype, all command line parameters, null pointers, and envp pointers are represented by ellipsis.

Description of the characters in the Six exec function names: the letter p indicates that the function takes filename as the parameter and uses the PATH environment variable to find the executable file. The letter l indicates that the function takes a parameter table, which is mutually exclusive with the letter v. The letter v indicates that this function is used to obtain an argv [] vector. The letter e indicates that this function is used to retrieve the envp [] array without using the current environment.

Table 8-6 Differences between Six exec Functions

<Sys/wait. h> * env_init [] = {, (pid = fork () <(pid =) (execle (, (*), env_init) <(waitpid (pid, NULL,) <(pid = fork () <(pid =) (execlp (, (*) <

Note: we set the first parameter (argv [0] in the new program) as the file name component of the path name. Some shells set this parameter to a complete path name. This is just a convention.We can set argv [0] to any string.

The program echoall to be executed in program listing 8-8 is shown in program listing 8-9. This is a common program that sends back all its command line parameters and all the environment tables.

Program list 8-9 command line parameters used for sending back and all environment strings

 argc,  *        **  **(i=; i<argc; i++)   (ptr = environ; *ptr != ; ptr++)    , *

The execution program list is 8-8.

[root@localhost apue]# ./prog8-==/]: only ====/bin/

 

Finally, we will summarize the parameters that need to be considered when using the exec function: first,New program name(Without path); second,Command Line Parameters(List form or pointer array form); finally,Environment table(Do you want to pass the environment table)

 

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

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.