Linux Process Programming: Introduction to Child process creation and execution functions

Source: Internet
Author: User
Tags function definition

Linux Process Programming: Introduction to Child process creation and execution functions

There are three child process creation and execution functions:

(1) fork ();
(2) exec ();
(3) system ();
The following are detailed descriptions of each.
(1) Fork ()
function definition:
pid_t Fork ();
Function Description:
Linux processes in memory consist of three parts of data: Code snippets, data segments, stack segments. In a process, the fork function is called, and a new process can be created and started. The new process shares the code snippet with the parent process, copying the data segment and the stack segment of the parent process. After a successful creation, the fork () has a return value to all two processes. The return value to the parent process is the number of the child process, and the return value to the child process is 0. Since two processes share the code snippet, we will take advantage of the difference of two return values by if...else ... Distinguishes between two processes running after a child process is started.
return value:
After a successful creation, the fork () has a return value to all two processes. The return value to the parent process is the number of the child process, and the return value to the child process is 0. The function call failed with a return value of-1. The cause of the error exists in errno.

(2) EXEC () function family
The EXEC function family has a total of 6 functions, function Prototypes:
int execl (const char *path, const char *arg, ...);
int EXECLP (const char *file, const char *arg, ...);
int execle (const char *path, const char *arg, ..., Char *const envp[]);
int execv (const char *path, char *const arg[]);
int EXECVP (const char *file, char *const arg[]);
int Execve (const char *path, char *const arg[], char *const envp[]);
Description of the function family:
The EXEC () function family finds and executes an executable file by specifying a path or file name. The executable file can be executed in a binary or Linux system shell script files, once executed instead of the original process code, the abolition of the original process data segments and stack segments, but still follow the process number of the original process. In other words, the program running in the original process has been replaced with a new one, but it is still the same process for the system. If our program starts the execution of another program and wants the original process to continue running, you can use the fork and exec together to create a new process and then use exec in the new process to invoke the program that needs to be started.
function return value:
The function of the EXEC () function family does not return a value after successful execution, and returns 1 when the call fails, and the original program continues to execute from the point of call.
(3) system ()
function definition:
int system (const char *file);
Function Description:
System () is equivalent to the combination of fork and execl. First, a sub-process is established by the fork () function, which is then found and executed by the EXECL () function based on the file name given by the parameter file.
Both the system () and the EXEC function family can execute out-of-process commands, except that the system () function creates a new process on the original program and executes the executable file in the new process, while the EXEC function family is inserting new code in the newly opened process to replace the original program code.
function return value:
The function call returned successfully 0; The call failed to return-1. If the 8~15 bit of the return value is 127, the EXECL function in System () fails.

go to:Linux Process Programming: Introduction to Child process creation and execution functions

Finally, if you use the fork to copy the process, you must remember exit (0) inside the program/application that is called after the copy process, or the zombie process will occur if you exit the process.

And you will find that even if you are in the called Program/Application a inside to do exit processing, the next time to continue to fork () the GID will be incremented sequentially, will not reuse the previous PID, why?

Don't worry, this is a caching mechanism, the process once exited, the process number can be reused, but in order to avoid mistaken for the previous exit process, there will be some delay, do not worry about the process ID is consumed by you.

So, rest assured, it will be reused. Always add, add to the maximum, at the beginning of the small, another round.

You can also refer to this article: Linux Multithreading overview

Linux Process Programming: Introduction to Child process creation and execution functions

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.