Question

this, I hope, is a simple question. I am familiar with the idea that fork in unix/linux/etc does not actually copy an entire image, but maps shared memory as private with a copy-on-write flag. To illustrate this, I tried the following example below which I expected would show the large malloc'd region as private (copy on write). However, I get the output below. Can anyone help shed light on why this is? My assumption is that my OS works as expected (uname -a: Linux xxxxxx 2.6.32-279.19.1.el6.x86_64 #1 SMP Tue Dec 18 17:22:54 CST 2012 x86_64 x86_64 x86_64 GNU/Linux) but pmap is not working as I expect..

int main(int argc, char *argv[]) {

    pid_t pid;
    char syscmd[80];
    char *somebuffer=(char*)malloc(999999999l);
    pid = fork();
    if (0 == pid) { /* Child */
            sprintf(syscmd, "pmap -x %d", getpid());
            system(syscmd);

    } else { /* parent */
            wait(NULL);
    }

    return 0;
}

output:

9822:   ./a.out
Address           Kbytes     RSS   Dirty Mode   Mapping
0000000000400000       4       4       0 r-x--  a.out
0000000000600000       4       4       4 rw---  a.out
000000357e000000     128      24       0 r-x--  ld-2.12.so
000000357e21f000       4       4       4 r----  ld-2.12.so
000000357e220000       4       4       4 rw---  ld-2.12.so
000000357e221000       4       4       4 rw---    [ anon ]
000000357e400000    1572     120       0 r-x--  libc-2.12.so
000000357e589000    2048       0       0 -----  libc-2.12.so
000000357e789000      16      12       8 r----  libc-2.12.so
000000357e78d000       4       4       4 rw---  libc-2.12.so
000000357e78e000      20      16      16 rw---    [ anon ]
00007f64228ad000  976576      16      16 rw---    [ anon ]
00007f645e27b000       4       4       4 rw---    [ anon ]
00007f645e27c000       4       0       0 r-x--    [ anon ]
00007fff2a1eb000      84      12      12 rw---    [ stack ]
ffffffffff600000       4       0       0 r-x--    [ anon ]
----------------  ------  ------  ------
total kB          980480     228      76

:: why isn't that large region (and probably others) marked as private?

Thanks!

Était-ce utile?

La solution

I think the pmap program just fails to print this information. Why are you using an odd utility like that instead of just cat /proc/%d/maps?

Autres conseils

I'm not sure how pmap indicates "copy-on-write", I was under the impression that it wasn't visible outside the kernel (that is, your second "anon" section is your malloc'd region), but I could be wrong. That is certainly where I would be looking for it, and it's about the right size. You could do a bit of measurement to see if you fill the memory twice (measure how long it takes) and then do the same in the child process, you'll see that filling the second time takes less time in both, but with the first time in teh forked process takes longer.

The higher version of pmap has a new option `-X'. With this option pmap show details in /proc/pid/smaps

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top