Analysis of Fork () Functions

Source: Internet
Author: User

#######################################

First, I declare that this article is organized from a blog of Daniel.

#######################################



# Include <unistd. h>

# Include <sys/types. h>

Int main (void)

{

Pid_t PID;

PID = fork ();

If (PID <0)

Printf ("error in fork! ");

Else if (pid = 0)

Printf ("I am the child process, my process ID is % DN", getpid ());

Else

Printf ("I am the parent process, my process ID is % DN", getpid ());

Return 0;

}

The execution result is:

I am the parent process, my process
ID is 2826

I am the child process, my process ID is 2827

To understand the execution process of fork, we must first clarify the concept of "process" in the operating system. A process mainly contains three elements:

O. an executable program;

O. All data associated with the process (including variables, memory space, buffers, etc );
O. Execution context of the program ).
Simply put, a process represents a State in one execution process of an executable program. The management of processes by the operating system is typically completed through the process table. Each table entry in the table records a process in the current operating system. For a single CPU, only one process occupies the CPU at each specific time, but there may be multiple active (waiting for execution or continuing execution) processes in the system at the same time.

A register called "program counter (PC)" indicates the position of the next instruction to be executed by the CPU-consuming process.
When the CPU time allocated to a process is used up, the operating system saves the value of the process-related registers to the corresponding table items of the process in the progress table; read the context of the process that takes over the CPU of the process from the Progress table and update the corresponding register (this process is called "process context switch )", the actual context exchange requires more data, which is irrelevant to fork. Remember that the program register PC indicates where the program has been executed and is an important part of the process context, the process for switching out the CPU needs to save the value of this Register, and the process for switching into the CPU should also update this register according to the execution context information of this process saved in the progress table ).
Well, with these concepts, we can say fork. When your program runs the following statement: pid = fork ();
The operating system creates a new process (child process) and creates a new table item for it in the process. The new process and the executable program of the original process are the same program. The context and data are mostly copies of the original process (parent process), but they are two independent processes! At this time, the program register PC claims in the context of the Parent and Child processes that the current fork call is about to return (at this time, the child process does not occupy the CPU, the PC of the sub-process is not actually saved in the register, but stored as the process context in the corresponding table in the progress table ). The problem is how to return data. In the Parent and Child processes, we will part with each other.

The parent process continues to execute. The operating system implements fork so that the call returns the PID (a positive integer) of the created child process in the parent process ), therefore, in the following if statement, neither of the two branches of PID <0, pid = 0 will be executed. So output I am the parent process...

The Subprocess is scheduled at a later time, and its context is swapped in to occupy the CPU. The fork Implementation of the operating system makes the fork call in the subprocess return 0. So in this process (note that this is not a parent process. Although it is the same program, this is another execution of the same program, in the operating system, this execution is represented by another process. From the execution point of view, it is said that the parent process is independent of each other.) pid = 0. When this process continues to run, if statement PID <0 does not meet, but pid = 0 is true. So output I am the child process...

I'm wondering why the two branches in the program are executed. This is of course impossible during one execution of a program; but the two lines of output you see come from two processes, which come from two executions of the same program.

My God, I don't know. It means it's white ......


After fork, the operating system will copy a child process that is exactly the same as the parent process. Although it is a parent-child relationship, in the operating system's view, they are more like siblings, the two processes share the code space, but the data space is independent of each other. The content in the data space of the child process is the complete copy of the parent process, and the command pointers are identical, but there is only one difference, if fork is successful, the return value of fork in the child process is 0, and the return value of fork in the parent process is the process number of the child process. If fork is not successful, the parent process returns an error.

As you can imagine, the two processes have been running at the same time, and the steps are consistent. After fork, they perform different jobs separately, that is, splitting. This is why fork is called fork.
As for the first operation, it may be related to the operating system, and this problem is not important in practical applications. If the parent-child process coordination is required, it can be solved through the primitive method.


Fork () returns the process number of the child process. Therefore, the PID in the parent process is the process number of the child process, and the PID in the child process is 0, only through getpid () to obtain the respective process numbers of the Parent and Child processes.

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.