The fork function of Linux

Source: Internet
Author: User
Tags function prototype

fork Function

header files:#include <unistd.h>

Function prototype :p id_t fork (void); (pid_t is a macro definition whose essence is that int is defined in the #include <sys/types.h> )

Return value: Two values are returned if the call is successful, the child process returns 0, the parent process returns the child process ID; otherwise, an error returns-1

function Description: An existing process can call the fork function to create a new process. A new process created by Fork is called a subprocess (child process). The fork function is called once but returns two times. The only difference of two returns is that the child process ID is returned in the parent process with a value of 0. A child process is a copy of the parent process that obtains a copy of the parent process's data space, heap, stack, and so on. Note that the child process holds a "copy" of the above storage space, which means that these storage spaces are not shared between parent and child processes. Linux copies the address space content of the parent process to the child process, so that the child process has a separate address space. After a successful creation of a new process, there are two fundamentally identical processes in the system, which do not have a fixed sequencing and which process first executes the process scheduling policy to look at the system.

  As an example:

  

  

  Operation Result:

  

 Analysis:

  

 Fork Function Summary:

1. call once and return two times.

2. in the child process Fork returns 0, theparent process Fork returns the PID of the child process .

id

② does not have a function that enables the parent process to obtain pidfork when returned, the pid return directly to the parent process)

Note: The ID of the child process cannot be 0because the PID 0 process is the swapper process.

3. parent, child process share body segment, do not share data, heap, stack segment, child process get parent process data, heap, copy of stack segment.

Note: Currently, most implementations and directly replicate the parent process's data, stack segments, and instead use the Write-time replication (copy-on-write) technique, which creates a copy of the modified data when the area of memory is modified.

4. The child process obtains a copy of the buffer, that is, the data in the process buffer before the fork is not flush , then the child process can get the data in the parent process buffer after the fork.

5. The parent process all open file descriptors are copied to the child process.

Note: There are usually two cases of working with file descriptors after fork:

① The parent process waits for the child process to end;

② the parent and child processes each execute different body segments (the parent, child processes each close the file descriptor that is not needed);

the difference between parent and child processes after 6.fork:

The return value of the ① Fork;

② process ID is different;

③ The parent process is also different;

The tms_utime, Tms_stime, Tms_cutime, and Tms_ustime of the ④ subprocess are set to 0;

⑤ the file lock set by the parent process is not inherited by the quilt process;

⑥ the unhandled alarm of the child process is cleared;

The unhandled signal set of the ⑦ child process is set to the empty sets;

Two main reasons for 7.fork failure:

The number of processes in the ① system has reached the upper limit;

② the total number of processes of the actual user reaches the system limit;

two ways to use 8.fork:

① a process wants to replicate itself so that the parent and child processes execute different pieces of code. such as the parent process listens on the port, after receiving the message,Fork out the child process processing the message, the parent process is still responsible for listening messages.

② a process needs to execute another program. executes a shell command, such as Fork .

The fork function of Linux

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.