"Linux&unix--exec and Fock system calls"

Source: Internet
Author: User

personal Learning and collation, if there are deficiencies, please feel free. Reprint Please specify:@CSU-max


Series Blog:

                      Linux&unix Learning the first play--file descriptors and permissions

          Linux&unix Learning Second Play--file descriptors and permissions


Processes and Procedures

 

Simply put, the difference between a process and an application is that the application is stored as a static file in the storage space of the computer system's hard disk, while the process is a system resource management entity maintained by the operating system under dynamic conditions.

(1) The process can describe concurrency more realistically, and the program cannot.
(2) The process is composed of two parts: procedure and data, which is the basic unit of competitive computer system limited resources and the basic unit of process processor dispatch.
(3) The procedure is a static concept; a process is the process of executing a program on a processing machine, and it is a dynamic concept.
(4) The process has a life cycle, there is birth and extinction. is short-lived, and the procedure is relatively long-term.
(5) A program can act as a running program for multiple processes, and a process can run multiple programs.
(6) The process has the ability to create other processes, and the program does not.


EXEC and Fock brief

The exec system calls the process to reinitialize from the specified program, although the process is still in progress, but the program has changed .

The Fock system call creates a new process that is cloned from an existing process only through replication instructions, user data, and system data fields, which are not initialized from the program, so the old and new processes execute the same instruction.

In addition to starting the UNIX kernel itself, exec is the only way for the program to execute on UNIX, not only the shell, but also the shell and its ancestor shell, which is called by exec. Fock is the only way to create a new process.

Detailed EXEC system call

In fact, there is no system call called "Exec", the so-called "exec" system call is actually 6 system calls in Execab form, as shown in the following way:


The A can be either L or V, depending on whether the parameter is directly in the call (list) or in the array (vector), b either has no, or p, p means to use the PATH environment variable finder, or E, E to use a specific environment. (Users cannot get features p and E at the same time in the same call.) Therefore, the 6 calls were Execl,execv, EXECLP, EXECVP, Execle, Execve.

function prototypes and explanations

EXECL--executes file with parameter list int execl (const char *path,/*program pathname*/const char *arg0,/*first arg (file name) */const char * Arg1,/*second arg (if needed) */..., (char *) null/*arg list terminator*/);

The path parameter is a program file pathname that can be executed by a valid user ID (for example, mode 755), and the contents of the executable program are correct.

A set of parameters that follow the path parameter proceeds to an array of character pointers, and the value of the last parameter is NULL, which is used to stop the revenue and terminate the array, the first of which is the name of the program file (not the entire path).

by re-initializing the stack, the instructions from the program overwrite the instruction segment of the process, and the data from the program overwrites the user data of the process, and the process executes the new program from the top (that is, it calls its Mian function).

5 Other Exec system calls

EXECV-Executes the file int execv with the parameter vector (const char *path,/*program pathname*/char *const argv[]/*argument vector*/);//EXECLP-Execution File with parameter list and path search int EXECLP (const char *file,/*program file Name*/const char *arg0,/*first arg (file name) */const Char *arg1,/ *second arg (if needed) */..., (char *) null/*arg list terminator*/);//EXECVP-performs file with parameter vector and path search int EXECVP (const char *FI Le,/*program file Name*/char *const argv[]/*argument vector*/);//execle-executes files with parameter lists and environment variables int execle (const char *path,/* Program Pathname*/const Char *arg0,/*first arg (file name) */const Char *arg1,/*second arg (if needed) */..., (char *) NUL L/*arg list Terminator*/char *const envv[]/*environment vector*/)//execve-execute file with parameter vectors and environment variables int execve (const char *path ,/*program Pathname*/char *const argv[]/*argument vector*/char *const envv[]/*environment vector*/);

Note the EXECLP and EXECVP, as described in the previous introduction, when B is P, the use of the PATH environment variable to find the program, let us take a simple look.

If the file parameter of the EXECLP or EXECVP does not have a slash, the string listed in the PATH environment variable is compared one by one until it navigates to an ordinary file with the result pathname, where the result pathname has execute permissions.

Case one: If the file contains an executable program (indicated by a code number at its first word), it is executed;

Scenario Two: If you do not include a program, it is assumed to be a script file, usually to run the script file, the path as the first parameter of the shell to execute the shell.

If the result of PATH lookup proves that nothing is executable, then exec fails.

If there is "/" in the file parameter, it is not necessary to look for it, that is to say, the path lookup is completed, but it may still be a script file.

Detailed Fock system call

To some extent, we can consider Fock and exec to be the opposite: Fock can create a new process, but it does not initialize the process in the new program, whereas the new process's instructions, user data segments, and system data segments are almost completely duplicated by the old process.

function prototypes and explanations

Fock--Create a new process pid_t Fock (void);

After the Fock call returns, two processes (both child and parent) receive the return value, but the return value is different, and the parent process accepts the process ID of the child process, and the child process receives a return value of 0. Returns-1 indicates an error, but because Fock has no parameters, there is very little error. The only reason to raise an error is resource exhaustion, such as insufficient swap space or too many processes being executed.

when the call to Fock succeeds, the generic child process then executes exec, and the parent process either waits for the child process to terminate, or leaves to do other things.

Parent and child processes under Fock invocation

 

The child process created by fork inherits most of the properties of the parent process because its system data segments are copied from the parent process. However, there are very few attributes that are not inherited: The child process ID is different from the parent process ID, they are different processes, and if the parent process is running multi-threaded, then only the thread that executes Fock will be present in the child process, and the child process will not be affected by multiple threads in the parent process; , because it is at the beginning of the life cycle.



***************************************************************************

                                        & nbsp;            *   Reprint Please specify source:   @CSU-max    http ://blog.csdn.net/csu_max      *

***************************************************************************






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.