Fork ()

Source: Internet
Author: User

#include <unistd.h>;
#include <sys/types.h>;main ()
{
       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 %d/n",getpid());
        else                printf("i am the parent process, my process id is %d/n",getpid());}

The result is

[Root @ localhost C] #./A. Out

I am the child process, my process ID is 4286

I am the parent process, my process ID is 4285

 

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. Process
Each table item 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 the system may have many
Activities (waiting for execution or continuing execution.

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.
The context of the process that uses the CPU is read from the Progress table, and the corresponding register is updated (this process is called "process context switch ").
The Inter-context exchange requires more data, which has nothing to do with fork. Remember that the program register PC indicates where the program has been executed and is an important part of the process context, change
The process that outputs the CPU needs to save the value of this Register. The process that is switched to the CPU should also update this register according to the execution context information of the 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 vast
Some are copies of the original process (parent process), but they are two mutually independent processes! At this time, the program register PC claims in the context of the Parent and Child processes that the process is currently executed until the fork call is
(At this time, the sub-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, in the parent-child process
Different ways.

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, similar to the parent process from the execution perspective
Mutual independence) pid = 0. When this process continues to run, if statement PID <0 does not meet, but pid = 0 is true. Therefore, the 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 ......

 

Oh, I understand that fork () is used in the program segment; after that, the Program Splits and derives two processes. The scheduling algorithm of the system depends on which one runs first.

Here, we can think that when [color = Red] runs to "pid = fork ();", the system derives a sub-process identical to the main program. This process
In the "pid = fork ();" sentence, PID gets the PID of the child process itself; after the child process ends, the "pid = fork (); "PID gets the parent process.
Body PID. Therefore, the program has two lines of output.

.. [/Color]


I thought it was quite good. But you don't understand it here. The sub-process must end first? If the system is scheduled to execute the parent process first, then it will not exit after execution? Then the sub-process will execute.

In the "pid = fork ();" statement of the process, the PID is the PID of the sub-process itself. After the sub-process ends, the "pid = fork (); "PID is the PID of the parent process.

What does this mean? How can we restore the PID of the molecular process and the parent process = fork?

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.