In Linux, a running process occupies three parts of Linux: Code zone, stack zone, and data zone. if multiple identical programs are run at the same time, they will use the same code area, and the code stored in the code area will be the code of the program, however, the data zone and the stack zone respectively store program data, global variables, and local variables. Therefore, even the same program cannot use the same data and stack zone at the same time.
# Include <stdio. h>
# Include <unistd. h>
Int main ()
{
If (Fork () = 0)
{
Printf ("first./N ");
If (Fork () = 0)
{
Printf ("second./N ");
}
Else
{
Printf ("third./N ");
}
}
Else
{
Printf ("fourth./N ");
If (Fork () = 0)
{
Printf ("fivth./N ");
}
Else
{
Printf ("Sixth./N ");
}
}
}
You can pay attention to the running results of the program and the output sequence, and understand why this is the output result.
Fork () function:
When the program calls the fork () function, the system prepares the stack zone, data zone, and code zone for the new process. the system first uses the same code zone for the fork () process and the original process. therefore, the data zone and the stack zone cannot be shared, so the system will copy the process that is exactly the same to fork. in this way, all the data of the parent process is assigned to the child process. when the process starts to run, although the data and stack of the parent process are copied, the data and stack are separated, and there is no impact between them. for the parent process, the fork () function returns the process number of the child process. For the child process, the fork () function returns 0. Therefore, the fork () function returns the value of 0, the current position of the program is in the child process or in the parent process.
That is, in the legend, a function has two return values, that is, the fork () function.
So there is a question... how can we start another process in one process ..?
In Linux, the exec function is basically used, but the exec function has a feature that once you use the exec function, the original process is discarded, because the code segment is occupied by new processes, the data zone and the stack zone are also discarded, and a new data zone and stack zone are generated for new processes. the only thing that hasn't changed is the process number. In fact, only the PID is the same, and other things are lost. for the system, it is still the same process, because the system only recognizes the process number, and for programming, it is already a new process.
So... come again...
What if you want to continue the original process and start a new process at the same time ..?
Look at the code I gave above... by the way... it is used with the fork () function and the exec class function.
Fork () generates a process exactly the same as the parent process, and then uses the exec function to start a new process. In this way, the original process is also running, the new process is also running. however, the difference is that the current relationship is the relationship between the parent process and the child process, rather than the original relationship between the same process.