Fork Function Summary __ function

Source: Internet
Author: User
Create a new process with the fork function in Unix/linux. The process is created by the currently existing process calling the fork function, and the forked process is called a subprocess, and the creator is called the parent process. The function is characterized by calling once, returning two times, once in the parent process, and once in the child process. The difference of two returns is that the return value of the subprocess is 0, and the return value of the parent process is the ID of the new subprocess. The child process continues to run concurrently with the parent process. If the parent process continues to create more child processes, the child processes are siblings, and the same subprocess can create its own subprocess, which establishes a hierarchical relationship between the processes that define the relationship.

A program contains multiple parts of memory, and the process of executing a program accesses the content as needed, including text segment, data segments (segments), stacks (stack), and heaps (heap). The text segment holds the command that the CPU executes, the data segment holds all the data variables of the process operation, the stack holds the automatic variables and function data, and the heap holds the dynamic memory allocation data. When a process is created, the child processes receive a copy of the data from the parent process, including data spaces, heaps, stacks, and process descriptors.

Program 1: Create a child process that modifies the inherited data and then prints the information for the parent-child process, respectively. The procedure is as follows: View Code

After the fork function is executed, the program structure diagram is as follows:

The child process executes in parallel with the parent process, so sleep in the parent process (10), let the child process execute first, and then execute the parent process.

The results of the program execution are as follows:

How do I create multiple child processes? The process pool model that is used when developing concurrent servers requires the creation of child processes for the specified bibliography first. For example, if we need to create 2 sub processes Now, it's easy to think of calling a loop and executing the fork function 2 times. It is feasible to try. The code is as follows: View code

The results of the procedure are as follows:

As a result, the number of child processes is not 2 but 3, which is why. First, a simple analysis: from the results of the parent process ID is 10669, the child process ID is: 10670, 10671, 10672.

The relationship between parent-child processes is as follows:

A subprocess with ID 10670 also calls the fork function, creating a process. Because the process created by the fork function is a copy of the parent process, the current data space, heap, stack, and shared code area are saved. The correct way to do this is to jump out of the subprocess and stop fork. The improved code is as follows: View code

The results of the procedure are as follows:

The result shows that the parent process (ID 10789) creates two child processes (ID: 10790, 10791).

There is such a face test, the procedure is as follows:

1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <stdlib.h>
 4 #include <sys/ Types.h>
 5 
 6 int main ()
 7 {
 8     pid_t   pid1;
 9     pid_t   Pid2;     pid1 = fork ();     Pid2 = fork ();     printf ("pid1=%d,pid2=%d\n", Pid1,pid2);     exit (0);
16}

Requirements are as follows:
It is known that no other new processes are executing during this time period from the end of all processes executing from this program to this program.
1, please say that after the implementation of this program, will run a total of several processes.
2, if the output of one of the processes is "pid1:1001, pid2:1002", write out the output of other processes (regardless of the sequence of process execution).

This topic examines the understanding of the fork function. The role of fork is to replicate a process that is the same as the current process. All the data (variables, environment variables, program counters, etc.) of the new process are consistent with the original process, but it is a completely new process, and as a child of the original process, the parent-child process executes the remainder in parallel.

The procedure is implemented as follows:

(1) When the program begins execution, the system assigns a process to execute, which calls the process as the main process p, and the process ID topic is not given,

(2) When the main process executes to the first fork function, create a new subprocess P1, there is a problem that the process ID is 1001,fork function has two return values, return pid=0 on behalf of the child process p1,pid1>0 on behalf of the parent process p.

(3) Now there are two processes p and P1, respectively, to perform the remainder,

(4) The P process (the parent process, so pid1=1001) calls fork to create the subprocess P2, returns two values pid2=1002 the process ID representing P2 returns to the parent process p,pid2=0 itself, so output P2, pid1=1001 1002 and Pid1=1001,pid2=0.

(5) The P1 process (subprocess, so pid1=0) invokes the fork create subprocess P3, the process ID analogy is 1003, and returns two values in which pid2=1003 represents the P3 process ID returned to the parent process p1,pid2=0 identity process P3 itself. So output pid1=0,pid2=1003 and pid1=0,pid2=0.

(6) Execution of the whole end.

According to the above analysis know the answer:

1. A total of four processes were implemented. (P0, P1, P2, P3)

2, the output of the other several processes are:

pid1:1001, pid2:0

pid1:0, pid2:1003

pid1:0, pid2:0

The machine test is as follows:

The test results are as follows:

Although the test results are not 1001, it can be seen that the theoretical analysis process is correct. Topic from: http://www.cnblogs.com/leoo2sk/archive/2009/12/11/talk-about-fork-in-linux.html

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.