You're right. As soon as you call fork()
, two identical processes exist. Therefore, the address of y
is the same for the copy of y in each process. The only difference between the two processes is that in one, fork()
returned 0
, and in the other it returned the PID of the child. Your program is using that information to do different behaviour in the parent and child, so you get the appropriate output.
In "real life", operating systems do a lot of optimizations to make fork()
really fast. That means that the actual physical behaviour probably doesn't involve a complete copy of the memory space. Logically, however, you can treat it as such.