Strange print after fork

Jul 18, 2011 at 6:43pm
I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
	int n=5;
	pid_t pid = fork();
	if(pid==0)
	{
		printf("PRIMA FIGLIO %p\n", &n);
		printf("PRIMA FIGLIO %d\n", n);
		n+=15;
		printf("DOPO  FIGLIO %p\n", &n);
		printf("DOPO  FIGLIO %d\n", n);
	}
	else
	{
		printf("PRIMA PADRE  %p\n", &n);
		printf("PRIMA PADRE  %d\n", n);
		wait(0);
		printf("DOPO  PADRE  %p\n", &n);
		printf("DOPO  PADRE  %d\n", n);
		exit(0);
	}
}


It prints:

1
2
3
4
5
6
7
8
PRIMA PADRE  0x7fff3c74d538
PRIMA PADRE  5
PRIMA FIGLIO 0x7fff3c74d538
PRIMA FIGLIO 5
DOPO  FIGLIO 0x7fff3c74d538
DOPO  FIGLIO 20
DOPO  PADRE  0x7fff3c74d538
DOPO  PADRE  5


Why addresses are the same?
Jul 18, 2011 at 6:53pm
The addresses are the same because the two processes are in the same module with the same scope.
Jul 18, 2011 at 7:03pm
the fork() call duplicates a process scope of addresses, in fact father and child have different values for the variable n, so I expected addresses to be different
Jul 18, 2011 at 8:41pm
I don't think that is correct, glennhk. The fork() call creats a new process, but it does not initialize it from a new program standpoint. Instead, the new processes' instruction, user-data, and system-data segments are almost exact copies of those of the old process ("Advanced Unix Programming" - Marc Rochkind ISBN 0-13-011818-4.)
Jul 19, 2011 at 12:06am
Yes, fork() copies the (virtual) memory map of the parent to the child, such that child and parent both refer to the same physical pages of memory until a write occurs.
Jul 19, 2011 at 8:45am
I already know the copy-on-write technique, but I expected to see a change of address after the n+=15 statement because child and parent have different addresses.
Jul 19, 2011 at 2:00pm
No, they don't! This is not a copy-on-write issue: This is the way fork() works. The parent and the child share the same addresses.
Jul 19, 2011 at 7:52pm
How in the earth they can share the same addresses and have two differents variables???
Jul 19, 2011 at 8:03pm
I'm getting too old - I totally mis-read what jsmith said. They initially share the addresses until a write occurs. The reason the addresses appear the same is based upon when the output is written. Perhaps also printing out a timestamp or having the child process sleep for a bit would show this.
Jul 20, 2011 at 12:12am
They share the same virtual address but not the same physical address after copy-on-write. It has nothing to do with timing.
Jul 20, 2011 at 8:48am
Are you saying that the address written to stdout by printf("%p", &n); is the logical address (the one that gets translated by the MMU)?
Jul 20, 2011 at 12:42pm
jsmith, I'm not talking about the timing causing a different mapping, I'm saying that because both processes are sharing stdout, the output may show an "old" value.
Jul 20, 2011 at 11:53pm
@glennhk: yes, applications never see physical addresses.
Jul 21, 2011 at 7:50pm
I see, so processes share the same logical addresses but have different physical addresses.
Topic archived. No new replies allowed.