Linux operating process (process of implementation)

If you need to perform some custom actions in the child process, you need to call exec family of functions.

When a process calls exec family of functions, procedures performed by this process is immediately replaced with a new program, the new program from the main function begins execution, and immediately replace the text segment of the current process, the data segment, heap and stack, Note that its process identifier and the process descriptor is not going to change.

1. exec function aromatic base

exec family of functions provides a way to start another program to be executed in the process, it can find the executable file from the specified file or directory name, and use it to replace the original data segment of the calling process, code and stack segment, in after execution, the original content in addition to the calling process process ID, the other all been replaced with a new process.

Usually call exec family of functions in the following two situations in Linux:

  • When the process think they can not make any contributions to the system and the user, you can call any of the exec family of functions is
    intended to function reinvent themselves.
  • If a process wants to execute another program, then it can be called fork () function to create a new process, and then calling a function exec family of functions, so that looks like and created a new process by executing an application program (this case is very common).

Call format standard exec family of functions are described as follows:

#include <unistd.h>
int execl(const char *path, const char *arg,...);
int execv(const char *path, char *const argv[]);
int execle(const char *path, const char *arg,...,char * const envp[]);
int execlp(const char *file, const char *arg,...);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[], char *const envp[]);

If the function succeeds six is ​​no return value, if the error "-1" is returned, its parameters are as follows.

  • Parameters pathname: pathname pointed out that an executable object file.
  • Parameters filename: indicates the file name of the executable file of the target.
  • Parameters arg: As agreed, as noted with the pathname path name of the target file.
  • Parameter argv: is a character pointer array, which states that the target program from the command line parameters using the table, according to the agreement with the pointer to the first character the same character string filename or pathname, a pointer to the last empty string, the remaining pointing command carried when the program is executed line parameters.
  • Parameters envp: Like with argv is a pointer to an array of characters, it is pointed out by the process environment when the target program is executed, it ends with a null pointer.

In fact, six functions are exec family of functions packaged library functions, their role is to find the specified file name of the executable file, and use it to replace the contents of the calling process is called internally execute an executable process file. Here's an executable file can be either a binary file, it can be any Linux executable script file.

The main difference of the exec family of functions are described as follows:

  • execl function to execv function execle function, the execve function uses the pathname argument, take the path name as a parameter; execlp function and the execvp function takes a file name as a parameter.
  • execl function, execle function, execlp function of the "l" character is represented as "list", which requires each new program
    command line options illustrated as a single, a null pointer is at the end of the parameter table; to execv function, the execve execvp function and the function "v" character is represented as "vector", which is required to construct a pointer to each parameter, then the address of the array as its argument.

execl, execel and execlp three functions used to represent a general parameter is the command line:

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

Note that the use of a constant 0 to cast a null pointer character pointer to the command line arguments as the end, if not cast, which is interpreted as integer parameters, which are errors.

execle execve function and the last character "e" represents an environment variable can be passed to the new process envp, its command parameters as follows:

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

Environment variable refers to a set of values, this set of values ​​from a Linux user logs in after there has been a lot of applications need to rely on it to determine some of the details of the system, the most common environment variables is the path (PATH), it should be pointed out to where to search for the appropriate application, such as / bin; HOME addition is relatively common in the environment variables that specify the user's personal directory in the system; environment variable is usually in the form of a string "XXX = xxx" is, XXX represents the variable name, xxx represents the value of the variable.

2. exec family of functions of the application

And comparing the difference between the exec function family.

Function name pathname parameter filename parameter Parameters Table argv[] environ parameters envp[]
execl function
execlp function
execle function
execv function
execvp function
execve function

After the series exec family of functions performed, not only the process descriptor identifier is not changed, the following characteristics of the process will also be retained:

  • Process identifier and the parent process identifier.
  • Real user ID, real group ID.
  • Additional group ID.
  • Process group ID.
  • Session ID.
  • Alarm time remaining.
  • Control terminal.
  • The current working directory.
  • Root directory.
  • File mode creation mask character.
  • File lock.
  • Process signal shielding.
  • Unprocessed signal.
  • Resource constraints.
  • tms_utime, tms_ Stime, tms_cutime 以及 tms_ cstime.

Applications

1. excel function call date command
2. ls command function call execlp
3. ls command function call execv
4. execl function were used in the parent and child process

Published 71 original articles · won praise 131 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43239560/article/details/102702859