Features of the fork function call
One call, two returns
Why?
Each process returns in its own address space----> stand in the Linux kernel thinking fork call
Fork is a system call that is done in the Linux kernel
Why is the fork return value designed as pid>0 is the parent process, and =0 is a child process?
A parent process can have n child processes, the father to find his son's PID is more difficult
Fork Understanding angle: Call two branches at a time (concurrent)
Why does the fork child process run from the fork, not from the beginning of the main function?
This is related to the mechanism by which the fork creates the child process, and the mechanism created by the child process is copy-on-write
Child process to copy the code snippet, stack segment, data segment, PCB Process control block of the parent process
The PCB control block is the data structure of the Linux kernel management process (TASK_STRUCT)
Because the child process copies the running scene of the parent process, there is no need for the child process to run again from the main function.
Copy-on-write understanding:
Copy the child process when modifying the variable
Copy pages only-----memory management of the >linux kernel
The system error code is inside the global variable errno
In the program, perror can print the errno corresponding to a string
"Linux" fork