Why the address of variable of child process and parent process is same
Asked Answered
I

2

10

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?

Interracial answered 31/8, 2011 at 7:8 Comment(0)
P
28

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.

Parental answered 31/8, 2011 at 7:14 Comment(3)
Also note that it has to be this way. If the address of y did change, then any pointers (addresses) held in variables or structures before the fork would no longer be correct after the fork. That would render fork much less useful, since the child process wouldn't be able to access any data structures (linked lists, trees) or dynamically allocated data from before, since those things rely on pointers. Systems without virtual memory pretty much can't implement fork, you get vfork (pubs.opengroup.org/onlinepubs/7908799/xsh/vfork.html) instead.Encomiast
Not technically "has to be this way" - you could use doubly indirected pointers provided your C runtimes support that (in other words, a pointer is an offset from a known value and, when forking, you move the data somewhere else for the child and adjust the known value). It would be a performance killer though. I think real-mode Windows did something similar to this with locking and unlocking of memory.Parental
Fair enough, you've basically described a cheap-and-cheerful (or expensive-and-miserable, depending how you look at it) software implementation of virtual memory. In that case you'd still see what the questioner observes, that the address before and after the fork prints as the same value. Unless the %p print formatter also added in the base address, I suppose :-)Encomiast
I
1

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.

Illyrian answered 31/8, 2011 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.