I am testing sharing of pages between a parent and child process. However, my script is reporting that very little sharing is going on.

Basically, all it is doing is allocating a large string in the parent, forking a child and having the child report how many pages of memory are shared (by looking at /proc/self/statm).

When I run it, however, the space used by the string seems to be in the UNSHARED pages. Is that really correct?

Some typical output:

$ ./size-check SIZE: 3792.000 SHARED: 324.000 UNSHARED: 3468.000 LENGTH($x): 0 $ ./size-check Allocating a large string SIZE: 23332.000 SHARED: 444.000 UNSHARED: 22888.000 LENGTH($x): 100000 +00 $
#!/usr/bin/perl our $x; if (rand(10) < 5) { print "Allocating a large string\n"; $x = ' ' x 10_000_000; } my $pid = fork(); die "unable to fork: $!" unless defined($pid); if ($pid) { # parent wait; } else { report_size(); } sub report_size{ my ($total, $shared) = _linux_size_check(); my $unshared = $total - $shared; printf "SIZE: %.3f SHARED: %.3f UNSHARED: %.3f LENGTH(\$x): %d\n", $ +total, $shared, $unshared, length($x); } # borrowed rom Apache::SizeLimit sub _linux_size_check { my ( $size, $share ) = ( 0, 0 ); if ( open my $fh, '<', '/proc/self/statm' ) { ( $size, $share ) = ( split /\s/, scalar <$fh> )[0,2]; close $fh; } else { die "Fatal Error: couldn't access /proc/self/status"; } # linux on intel x86 has 4KB page size... return ( $size * 4, $share * 4 ); }

In reply to shared pages between parent and child by perl5ever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.