Author: CCF published on: 17:11:01
# 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 % DN", getpid ());
Else
Printf ("I am the parent process, my process ID is % DN", 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
I can't imagine why the two lines are printed out. In my opinion, no matter what the PID is, there should be only one line.
Chg. sReply:21:09:30
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 ......
ZhaojinboReply: 12:35:50
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.
SniperReply: 22:11:15
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 running to "pid = fork ();", the system derives a sub-process identical to the main program. 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. Therefore, the program has two lines of output.
Note: This is not accurate. The PID value in the sub-process is 0. You can use getpid to obtain the ID of the sub-process. In the parent process, the PID is the ID of the parent process.
Errata: The PID value in the parent process is the sub-process number. Only the getpid () executed by the parent process is his own process number. Cold, completely in
Jjl3Reply: 11:43:20
Make the following changes:
# Include <unistd. h>;
# Include <sys/types. h>;
Main ()
{
Pid_t PID;
Printf ("fork! "); // Printf (" fork! N ");
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 ());
}
The result is
[Root @ localhost C] #./A. Out
Fork! I am the child process, my process ID is 4286
Fork! I am the parent process, my process ID is 4285
But I changed it to printf ("fork! N "); the result is
[Root @ localhost C] #./A. Out
Fork!
I am the child process, my process ID is 4286
I am the parent process, my process ID is 4285
Why is there only one fork! Printed? Why are there two in the previous one?
BashfulboyReply: 22:10:52
Let me also:
Wujiajia's understanding is incorrect,
Printf ("aaaaaaaa"); // print once; print twice
If you change printf ("aaaaaa") to printf ("aaaaaan"), it is printed only once.
The main difference is that there is an N-carriage return symbol.
This is related to the buffer mechanism of printf. In some content of printf, the operating system only places the content in the Buffer Queue of stdout and does not actually write it to the screen.
However, as long as N is displayed, stdout will be refreshed immediately, so it can be printed immediately.
After printf ("aaaaaa") is run, aaaaaa is only put in the buffer, and then run to fork, The aaaaaa quilt process in the buffer inherits
Therefore, aaaaaa is also available in the stdout buffer.
So what you finally see is that aaaaaa was printf twice !!!!
After printf ("aaaaaan") is run, aaaaaa is immediately printed to the screen, and there is no aaaaaa content in the stdout buffer in the child process to which the fork goes.
The result you see is that aaaaaa is printf once !!!!
(Excellent)
Albcamus
Reply: 15:56:11
>;>; The PID variable of the dispatch process is not changed. What does it mean that PID is not 0 for sub-processes?
1. The PID of the parent process is unchanged;
2. For sub-processes, fork returns 0 to it, but its PID is definitely not 0. The reason fork returns 0 to it is that it can call getpid () at any time () to obtain your own PID;
3. Your opinion on the upstairs is correct. Unless synchronous means is adopted, the Fork sub-process cannot determine who runs first or who ends first. The parent process returned from fork after the child process ends. This is not true for fork, but for vfork.