Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the address of variable of child process and parent process is same

Here is my code

int main()
{
  pid_t pid;
  int y = 3;  
  if ( (pid = fork()) <0 )
   return -1;;

  if( pid == 0 )  /* child */
  {
    printf(" before: %d %p\n", y, &y );
    y *= 10;
    printf("after: %d %p\n", y, &y );
  }
  else /* father */
  {
   sleep(1);
   printf("father: %d %p\n" , y , &y );

  }
  return 0;
}

The output of the program is like following:

before: 3 ffbff440
after: 30 ffbff440
father: 3 ffbff440

My question is why is address of variable of child and parent same but the value different?

like image 278
Janus.Le Avatar asked Aug 31 '11 07:08

Janus.Le


People also ask

Do parent and child process share the same address space?

Also, Linux uses copy on write when creating child processes. This means that the parent and child will share the parent address space until one of them does a write, at which point the memory will be physically copied to the child. This eliminates unneeded copies when exec ing a new process.

Can a parent and child process both have the same PID?

A parent process may have multiple child processes, but a child process only one parent process. On the success of a fork() system call: The Process ID (PID) of the child process is returned to the parent process.

What is common between child and parent process?

3.3 When a process creates a new process using the fork() operation, which of the following state is shared between the parent process and the child process? Answer: Only the shared memory segments are shared between the parent process and the newly forked child process.

Do parent and child process execute concurrently?

Father and child processes do run concurrently, and it is not predictable which process is running at what time. From a first inspection you have a loop that starts from the father process and creates 8 child processes, that on their hand each creates other child processes!


2 Answers

Because it's a virtual address, not a physical one.

Each process gets its own address space (for example, a 32-bit system may allow each process to have its own address space with the full 4G range).

It's the memory management unit that will map virtual addresses to physical ones (and handle things like page faults if swapped out pages need to be bought back in from secondary storage).

The following diagram may help, each section representing a 4K block of memory:

   Process A           Physical Memory      Process B
   +-------+           +-------------+      +-------+
0K |       |---->   0K |  (shared)   | <----|       | 0K
   +-------+           +-------------+      +-------+
4K |       |--+     4K |             | <----|       | 4K
   +-------+  |        +-------------+      +-------+
8K |       |  +->   8K |             |      |       | 8K
   +-------+           +-------------+      +-------+
       |                : : : : : : :           |
       |               +-------------+          |
       |          128K |             | <--------+
       |               +-------------+
       +--------> 132K |             |
                       +-------------+

You can see, in that diagram, the disconnect between virtual memory addresses and physical memory addresses (and the possibility for processes to share memory blocks as well). The addresses down the left and right sides are virtual addresses which the processes see.

The addresses in the central block are actual physical addresses where the data "really" is, and the MMU handles the mapping.

For a deeper explanation of fork (and exec), you may also want to look at this answer.

like image 85
paxdiablo Avatar answered Oct 19 '22 11:10

paxdiablo


The address is the 'same' as each process has its own virtual address space and the variable will generally be loaded into the same location. Note that this is not the physical address in memory. Also note that there are schemes that deliberately randomise the location at which a process is loaded in order to make it harder to attack/hack the process. In that case the address will be different.

like image 22
trojanfoe Avatar answered Oct 19 '22 11:10

trojanfoe