2

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!

Mat
  • 202,337
  • 40
  • 393
  • 406

3 Answers3

0

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?

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Thanks for the reply. I was under the impression that the proc map does not show copy-on-write information, which was why I tried the pmap program. Perhaps pmap also does not show this info. – user1922401 Dec 21 '12 at 20:38
  • Neither does. This `pmap` utility seems to be doing nothing but formatting the contents of `/proc/%d/smaps` (and doing it poorly, i.e. omitting some of the most useful information). – R.. GitHub STOP HELPING ICE Dec 21 '12 at 20:55
  • This seems to be the right answer, which disagrees with the manpage. I wonder if different versions on different OS variants have access to this info – user1922401 Dec 23 '12 at 22:17
0

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thank you for your response. I think you may be right that pmap does not have access to that information, and that region seems to be the malloc'd one (commenting the malloc removes it). The reason I was looking for it was that the man page says: "Mode: permissions on map: read, write, execute, shared, private (copy on write)" and so there is a field for it – user1922401 Dec 21 '12 at 20:32
0

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

where23
  • 483
  • 3
  • 9