Linux Process 6

Source: Internet
Author: User

Learning the exec () function of process control functions

When we watch horror movies, there are often such scenarios: when a person is put behind a ghost, the human body is still the same as before, but his soul and thoughts have been possessed by this ghost, so it will control this person to do what he wants-so there is also such a situation in the process. So how is it implemented? Now let's learn the exec () function family.

1. .exe C () function family

 

1. First, enter the command man exec on the terminal to view the function prototype:

# 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 execve (const char * pathname, const char * argv [], char * const envp []);
Int execlp (const char * file, const char * Arg ,...);
Int execvp (const char * file, char * const argv []);
The first difference between these functions is that the first four take the path name as the parameter, and the last two take the file name as the parameter.
When filename is specified as a parameter:
A. If filename contains/, it is regarded as the path name.
B. Otherwise, search for executable files based on the path environment variable.
Path =/bin:/usr/bin:/user/local/bin :.
The last path prefix indicates the current directory. The zero prefix refers to the current directory (available at the beginning of name = value: represents, in the middle of the Use: represents, at the end of the line use: represents)
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, in which the parameter table ends with a null pointer. Execv, execve, and execvp must first construct a pointer array pointing to each parameter, and then use the address of the array as the address of the three functions.
The third difference is related to the transfer of environment tables to new programs. The execve and execle functions can pass a pointer to the Environment string pointer array.

Summary of tiger-JOHN:
1>. Only execve is a real system call, and other database functions are packaged on this basis.
2>. The exec function family finds an executable file based on the specified file name and replaces 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.

However, a shell script must start with the following format:

The first line must be :#! Interpretername [Arg]. Where interpretername can be a shell or another interpreter. For example,/bin/sh or/usr/bin/perl, Arg is a parameter that passes an interpreter.

3> memory method:

L: List)

E: indicates the use of new environment variables, not inherited from the current

P: indicates using the file name and searching from the path environment

4> after the exec () function family is successful, the return value is not returned because the execution image of the process has been replaced and the returned value is not received. But if there is an error event, it will return-1. These errors are usually caused by file name or parameter errors.

2. Exec () functions:

1> the exec () function call does not generate a new process. Once a process calls the exec function, it will "die" in this province-it is like a zombie, and your body is still yours, but the soul and thought have been replaced-the system replaces the code segment with the code of the new program, discards the original data segment and stack segment, and assigns a new data segment and stack segment for the new program, the unique retained process ID is the process ID. That is to say, for the system, it is still the same process, but another program is executed.

2> after the exec () function is executed, the process not only maintains the original process ID, parent process ID, actual user ID, and actual group ID, but also maintains many other original features, mainly include

A. current working directory

B. root directory

C. Shielding words used when creating files

D. Process signal shielding.

E. Pending warning

F. Time of using the processor RELATED TO THE PROCESS

G. control terminal

H. file lock
Tiger-John note:

1. It must be distinguished that only the fork () or vfork () function can create a new process, while the exec () function cannot create a process.

2. Before using the exec () function, you must use fork () or vfork () to create a sub-process. Then, the sub-process calls the exec () function to execute another program.

 

 

3. Specific implementation of exec () function family

1> 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 the main function. Because exec () is called and no new process is created, the process id remains unchanged. The Exec () function only replaces the body, Data, heap, and stack segments of the current process with a brand new program.
2> no matter which exec () function is used, the executable program path, command line parameters, and environment variables are transmitted to the main () function of the executable program.

3> details how to pass the parameters required by the main () function in the exec () function family.

A.exe CV () function: execv () function uses the path name to call the executable file as a new process image. Its argv parameter is used to provide the argv parameter to the main () function. The argv parameter is a string array ending with null.

B .exe CVE () function: the path name of the program to be executed when the pathname parameter is used. The argv parameter corresponds to the argv and envp OF THE MAIN () function.

C.exe Cl () function: The subfunction is similar to the execv function. When passing the argv parameter, each command line parameter is declared as a separate parameter (the parameter uses "... "The number of parameters is uncertain.) Note that these parameters should end with a null pointer.

D.exe CLE () function: This function is similar to the execl function, but only displays the specified environment variable. The environment variable is located after the last parameter of the command line parameter, that is, after the NULL pointer.

E.exe CVP function: This function is similar to execv function. The difference is the filename parameter. If this parameter contains/, it is regarded as the path name. Otherwise, the executable file is searched based on the path environment variable.

F.exe CLP () function: similar to execl functions, the difference is the same as that between execvp and execv.

 

----------------------------------------------

Through the above learning, we will compile a program to experience its execution process.

4. Function instances

Exec. c

1 # include <stdio. h>
2 # include <sys/types. h>
3 # include <unistd. h>
4 # include <stdlib. h>
5 Int main (INT argc, char * argv [], char ** environ)
6 {
7 pid_t PID;
8 int status;
9 printf ("Exec example! /N ");
10 pid = fork ();
11 if (PID <0 ){
12 perror ("process creation failed/N ");
13 exit (1 );
14}
15 else if (0 = PID ){
16 printf ("child process is running/N ");
17 printf ("My pid = % d, parentpid = % d/N", getpid (), getpid ());
18 printf ("uid = % d, gid = % d/N", getuid (), getgid ());
19 execve ("processimage", argv, Environ );
20 printf ("process never go to here! /N ");
21 exit (0 );
22}
23 else {
24 printf ("parent process is runnig/N ");
25}
26 wait (& status );
27 exit (0 );
28}

 

 

Processimage. c

1 # include <stdio. h>
2 # include <sys/types. h>
3 # include <unistd. h>
4
5 Int main (INT argc, char * argv [], char ** environ)
6 {
7 int I;
8
9 printf ("I Am a process image! /N ");
10 printf ("My pid = % d, parentpid = % d/N", getpid (), getppid ());
11 printf ("uid = % d, gid = % d/N", getuid (), getpid ());
12
13 For (I = 0; I <argc; I ++ ){
14 printf ("argv [% d]: % s/n", I, argv [I]);
15}
16
17}

Function Compilation:

Think @ Ubuntu :~ /Work/process_thread/exec1 $ GCC processimage. C-o processimage
Think @ Ubuntu :~ /Work/process_thread/exec1 $ GCC exec. C-o Exec

Think @ Ubuntu :~ /Work/process_thread/exec1 $./exec test Exec

Function execution result:

Exec example!
Parent process is runnig
Child process is running
My pid = 5949, parentpid = 5949
Uid = 1000, gid = 1000
I am a process image!
My pid = 5949, parentpid = 5948
Uid = 1000, gid = 5949
Argv [0]:./exec
Argv [1]: Test
Argv [2]: Exec
Tiger-John note:

1. Through the execution of the above program, we can see that the process executing the new program maintains the original process ID, parent process ID, actual user ID, and actual group ID.

2. After the execve () function is called, the original sub-process image is replaced and no longer executed.

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.