Process management under the unix/linux of "summary" (ii): The function of creating process and its application, contrast

Source: Internet
Author: User

functions to create a process fork (), vfork (), and execl ()
This content mainly introduces the function fork and vfork of the 2 creation process under Unix/linux and their differences.


1.fork function


(1) Format of the function
#include <unistd.h>
pid_t fork (void);
function function:
It is primarily used to create new processes in a way that replicates running processes, where new processes are called child processes, and processes that are running are called parent processes.
return value:
When the function call succeeds, the parent process returns the PID of the child process, the child process returns 0, and when the function call goes wrong, the parent process returns-1, and the child process is not created.

Attention:

The return value of this function is 2, mainly because a child process has been created by running to the X line before the end of the fovk execution, so the parent-child process will execute the code of the X-line to the return statement. In order to find his own son, the parent process returns the PID of the child process, which can find the parent process through getpid (), so the child process returns 0.



(2) How fork creates a child process
1) before the Fork function code, the parent process executes sequentially.
2) After the Fork function code, the parent-child process executes once.
3) The return value of the fork function, and the parent-child process returns once.


(3) Relationship between parent-child processes created by fork
1) The parent process starts the child process, and the parent process starts at the same time, and if the child process ends first, it signals the parent process to help the child process reclaim the resources.
2) If the parent process now ends and the child process becomes an orphan process, the child process changes the parent process (re-specifies the init process as the new parent process), and the Init process is called the orphanage.
3) If the child process ends first, but the parent process does not receive a signal from the child process for various reasons, that is, no through these process recycles the resource, the child process becomes a zombie process.


(4) memory resource relationship between parent-child processes created by the fork function
Memory zoning of processes: Code area, global zone, heap area, stack area
A child process is a copy of the parent process that obtains a copy of the parent process data segment and the stack segment (including the I/O stream buffer), but the child process shares the code snippet of the parent process. The File descriptor table of the parent process is also copied to the child process, and both share the same file tables. So if you have an unwanted descriptor in a parent-child process, it's best to close.


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


2.vfork and EXECL functions


(1) Vfork function
#include <sys/types.h>
#include <unistd.h>
pid_t vfork (void);
function function:
The function is similar to the fork function, the return value and the reason for the error, such as the fork function can be, but the difference is that the function creates a child process does not replicate the memory area in the parent process, but instead directly occupy, causing the parent process to enter the blocking state until the child process ends or call the EXEC series function, The end of the child process should call the _exit () function.


Note:
The Vfork function guarantees that the created child process executes first.


(2) EXEC series functions
int execl (const char *path, const char *arg, ...);
First parameter: File path to execute
The second parameter: the execution of the parameters, generally to the file name can
Third parameter: variable long parameter, end with NULL
function function:
Used primarily to execute files in the path and mode area specified by the parameter.
Such as:
When you execute the ls-l command using the EXECL function:
Execl ("/bin/ls", "ls", "-l", NULL);


Note:
The Vfork function itself does not have much practical meaning and is typically used in conjunction with the Exec series functions, while the vfork function is responsible for creating child processes, and the exec series functions are responsible for executing the new code.
The fork function can also be used in conjunction with the Exec series functions, which are rarely used.


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


the difference between fork and Vfork and its application

Conceptually, the fork () can be thought of as a copy of the parent process, except that the text area is a share, and the others are created as copies. This is the implementation of the early fork function, which greatly consumes memory resources, compared to vfork () created by the child process is shared with the parent process of memory, efficiency is far higher than the fork function. Vfork () can be said to completely replace the fork () before fork () takes a write-time copy.


in order to solve the unnecessary waste of the fork function, the kernel uses the write-time replication (copy-on-write) technique to process the parent process's data, BBS, heap, and stack areas. This technique is generally understood to be that the parent-child process shares memory first, and if the parent-child process does not write the memory before the entire program finishes, the child process does not need to copy the memory of the parent process. Initially, the kernel made some settings so that the page table entries for these segments point to the same physical memory industry as the parent process and mark the pages themselves as read-only. When fork () is called, the kernel captures any modification attempts made by the parent or child processes against those pages and creates a copy of the page that will be modified. The system assigns a page copy of the heart to a process that is captured by the kernel and makes appropriate adjustments to the corresponding page table entries for the child process. From this moment on, the parent and child processes can individually modify their own page copies, no longer interacting with each other.


Compared with fork (), vfork () is specifically designed for the child process to execute the EXEC () series program immediately. The main reasons are as follows
1:vfork () You do not need to copy virtual memory or page tables for a child process. Instead, the child process shares the parent process memory until it successfully executes EXEC () or calls-exit () to exit.
2:vfork () Creates a child process that pauses execution of the parent process before calling exec () or _exit ().


the application of fork and Vfork
The General fork is used with vfork, exec mates to the process termination function, and the function about the process is not discussed much here. Let me give you an example of a mock bank, a general description of the basic use of these 2 functions.


Assuming that this simulation bank only withdrawals and deposits so two functions, the request is that the program can simultaneously perform withdrawals and deposits of the 2 programs, and can achieve multiple customers simultaneously deposit and withdrawal.
The Vfork () +exec () function is most suitable for executing deposits and withdrawals at the same time, however, the parent process creates 2 sub-processes, child process A is responsible for the deposit business, and sub-process B is responsible for the withdrawal business. So and so on, how many operations does the parent process use Vfork () +execl () to create the number of child processes.
In the deposit process, a will wait for the deposit message, when a deposit message is captured, process a FOVK () a sub-process, the child process is responsible for processing the message, then in the deposit process, the parent process is responsible for capturing the message, the specific processing work is given to the child process, so that multiple processes can be implemented concurrently processing, Accelerate the efficiency of your business processes.
The withdrawal process is ibid.


Summary: Fovk () +execl () is to create multiple functions that differ from one process to another, FOVK is to create multiple functions just like the process to handle the same business!


(Finally, the practice code for this demo bank is sorted out soon.)

Process management under the unix/linux of "summary" (ii): The function of creating process and its application, contrast

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.