is data shared between processes when we use fork in c?

前端 未结 3 795
故里飘歌
故里飘歌 2020-12-21 18:39

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

3条回答
  •  情话喂你
    2020-12-21 19:30

    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.

提交回复
热议问题