is data shared between processes when we use fork

2020-02-15 07:45发布

In this C program, data is not being shared between process i.e. parent and child process. child has it's own data and parent has it's own data but pointer is showing the same address for both processes. How it is being done on background? Does fork generates copies of same data? if so then we have the same pointer's address for both processes. Or is it due to the statically allocated data that is being copied for each process and the data is independent for each process? I want to know how it is being done?

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

int main()
{
    //Static Array
    int X[] = {1,2,3,4,5};
    int i, status;

    //The fork call
    int pid = fork();

    if(pid == 0) //Child process
    {
        //Child process modifies Array
        for(i=0; i<5; i++)
            X[i] = 5-i;
        //Child prints Array
        printf("Child Array:\t");
        for(i=0; i<5; i++)
            printf("%d\t", X[i]);
        printf("\nArray ptr = %p\n", X);
     }
     else //Parent process
     {
        // Wait for the child to terminate and let 
        // it modify and print the array
        waitpid(-1, &status, 0);

        //Parent prints Array
        printf("Parent Array:\t");
        for(i=0; i<5; i++)
           printf("%d\t", X[i]);
        printf("\nArray ptr = %p\n", X);
        }
    return 0;
}

Here is the output of the program.

 1  Child Array:    5   4   3   2   1   
 2  Array ptr = 0x7fff06c9f670
 3  Parent Array:   1   2   3   4   5   
 4  Array ptr = 0x7fff06c9f670

When child process modifies array it should have also modified the data of parent process. What is going on in background?

标签: c gcc stack fork
3条回答
看我几分像从前
2楼-- · 2020-02-15 08:05

fork creates exact copy of the parent process memory image (exception see in man page). This is called Copy On Write(COW) fork. Upto time child only read data, both parent and child have same copy of data but when child write, a new copy is generated and then both child and parent have different copyies for their own data

查看更多
Luminary・发光体
3楼-- · 2020-02-15 08:11

When you fork a new process the new child process is a copy of the parent process. That's why pointers etc. are equal. Due to the wonders of virtual memory two processes can have the same memory map, but still be using different memory.

查看更多
smile是对你的礼貌
4楼-- · 2020-02-15 08:21

fork() creates a copy of the calling process, including all the memory allocated to it.

Each process has its own address space and the values of pointers are within context of that address space. So printing the address of some variable in the original process will give the same output as printing that address in the spawned process.

However, as far as the operating system is concerned, the addresses are not equal. The operating system takes care of ensuring each process has the illusion of its own memory.

There are means of sharing memory between processes (i.e. what one process writes to the shared memory, the other one sees). However, that is not what happens by default, and still happens with the help of the host operating system.

查看更多
登录 后发表回答