A detailed description of the fork () function in Linux

Source: Internet
Author: User

Reference address

1. understanding of the fork function: a process that includes code, data, and resources assigned to the process. The fork () function creates a process that is almost identical to the original process through a system call.

That is, two processes can do exactly the same thing, but two processes can do different things if the initial parameters or the variables passed in are different.

After a process calls the fork () function, the system first assigns resources to the new process, such as space for storing data and code. And then put all the values of the original process

Copied to a new new process, only a few values are different from the value of the original process. The equivalent of cloning a self.

One thing to note: After calling the fork function, it must be that the code snippet that executes at the same time as the two process is the code after the fork function, and the previous code and the parent process have finished executing.

Let's look at an example:

/** FORK_TEST.C * Version 1 * Created on:2010-5-29 * author:wangth*/#include<unistd.h>#include<stdio.h>intMain () {pid_t fpid;//Fpid represents the value returned by the fork function    intCount=0; Fpid=Fork (); if(Fpid <0) printf ("Error in fork!"); Else if(Fpid = =0) {printf ("I am the child process and my process ID is%d/n", Getpid ()); printf ("I am father's son/n");//for some people, Chinese looks more straightforward. count++; }    Else{printf ("I am the parent process, my process ID is%d/n", Getpid ()); printf ("I'm a child, his father/n"); Count++; } printf ("The statistical result is:%d/n", Count); return 0;}

The operating result is:

I am the child process and my process ID is 5574
I'm the father's son.
The statistic results are: 1
I am the parent process, my process ID is 5573
I'm the kid, his father.
The statistic results are: 1

Before the statement fpid=fork (), only one process executes the code, but after this statement , it becomes two processes executing, the two processes are almost identical, and the next statement to be executed is the IF (fpid<0) ...

Why the fpid of two processes is different, which is related to the characteristics of the fork function. A wonderful thing about a fork call is that it is called only once, but it can return two times, and it may have three different return values:
1) In the parent process, fork returns the process ID of the newly created child process;
2) in the sub-process, fork returns 0;
3) If an error occurs, fork returns a negative value;

  After the fork function finishes executing, if the new process is created successfully, there are two processes, one child process and one parent process. In the subprocess, the fork function returns 0, and in the parent process, fork returns the process ID of the newly created child process. We can determine whether the current process is a child process or a parent process by the value returned by the fork.

Quote a netizen to explain why the value of Fpid is different in the parent-child process. "In fact, the equivalent of a linked list, the process forms a linked list, the parent process fpid (p means point) to the process ID of the child process, because the child process has no child process, so its fpid is 0.
There are two possible reasons for fork errors:
1) The current number of processes has reached the system-specified limit, when the value of errno is set to Eagain.
2) system memory is low, then the value of errno is set to Enomem.

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. Each process has a unique (distinct) process ID, which can be obtained through the getpid () function, a variable that records the PID of the parent process, and the value of the variable can be obtained through the getppid () function.

After the fork executes, two processes appear,

Some people say that the content of the two process is exactly the same Ah, how to print the result is not the same ah, that is because the reason for judging the conditions, the above list is only the process of code and instructions, and variables ah. After the fork is executed, the variable for process 1 is count=0,fpid! =0 (parent process). The variable for process 2 is count=0,fpid=0 (child process), the variables of both processes are independent, there are different addresses, not shared, and this should be noted. We can say that we are using fpid to identify and manipulate parent-child processes.

Others may wonder why the code is not copied from # include, because Fork is a copy of the current situation of the process, and when the fork is executed, the process has finished executing int count=0;fork only the next code to execute to the new process.

Two 360 choice questions about fork are attached:

1.

 for (int i=0;i<2; + +i) {      fork ();      printf ("Hello\ n");        }
Print how many times hello

There is a convenient way to analyze this in Csapp, as shown below:

2.

int Main () {    fork ()| | fork ();}
Create several processes altogether

A detailed description of the fork () function in Linux

Related Article

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.