Previously, we learned from the function fork() that there is a copy of code between the parent and child processes, and the parent processes execute independently of each other. Therefore, whether the parent and child processes share the same data, that is, whether there is data sharing. Next, we will analyze whether there is data sharing between parent and child processes.

As we all know, under Linux, memory is stored in global variables, stack areas, heap areas, and files. The character constant area is not analyzed here. Let’s verify that they share data with the actual code in turn. The so-called parent-child process data sharing, generally speaking, means that the parent process or the child process changes the data, so that the child process or the parent process data synchronization changes. The idea of code detection is to have one of the parent and child processes modify the data. The process that does not modify the data calls the data to see if the data has been modified. If the data has been modified, then it proves that there is data sharing between the two processes, and vice versa.

1. Global variables

The code operation and the results are as follows:

The result shows that the child process has modified data A, but the parent process still gets the initialized value. So we know that when the data type is a global variable, data is not shared between parent and child processes.

2. Stack area (local variable area)

The code implemented is similar to global, just declaring int a=0 in the main function. Here directly to run the results, not show the source code.

The code and running results are as follows:

We can see that the result of the run is the same as the result of the global variable, so it can be concluded that when the data type is local, the parent process does not share the data.

3. Heap area (dynamic memory)

The code and running results are as follows:

As can be seen, there is also no change, consistent with local and global results. The conclusion is: when the data type is dynamic, the parent process does not share the data.

4. The file

From the results, we can see that when the data type is file, the parent and child processes share data, specifically the file offset.

The result of data sharing between parent and child processes is as follows:

Global variable stack area (local variables) heap area (dynamic open) file

No Share No share No share Indicates the offset of the shared file

[Supplement] PCB process control block:

As shown in the figure above, both parent and child processes use a variable (global variable.data segment) int num = 100. When both processes read only this variable, they read the same area of physical memory. When num– is executed by the parent process and num++ is executed by the child process, a copy of num is made and placed in different physical memory areas. In this case, physical memory contains three copies of num.

Data sharing between parent and child processes: shared while reading and replicated while writing. (Refer to the previous article.)