Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Perl forked processes and variable sharing

by fireblood (Scribe)
on Feb 02, 2022 at 00:32 UTC ( #11141037=note: print w/replies, xml ) Need Help??


in reply to Re: Perl forked processes and variable sharing
in thread Perl forked processes and variable sharing

Hi Dave,

Thanks for your reply. I had considered that perhaps the same address in two processes might be in two entirely different address spaces and therefore not physically the same addresses, so that is why in my code I had included the test where the child process reads the value of the scalar at the address passed to it as an argument by the parent process. What the child process finds at that passed address is exactly the same value that the parent wrote to that address. So it seems that in my program the same two virtual addresses map to the same two physical addresses.

Perhaps when the child reads from that address the physical addresses are the same, but after the child writes to that address what happens behind the scenes is that that physical address is not overwritten in place, lest the new value from the child be longer than the original value from the parent and neighboring variable values be clobbered, so instead the new value is written by the child to a new physical address which remains unknown to the parent.

Thanks for your thoughts on this.

Richard
  • Comment on Re^2: Perl forked processes and variable sharing

Replies are listed 'Best First'.
Re^3: Perl forked processes and variable sharing
by dave_the_m (Monsignor) on Feb 02, 2022 at 07:44 UTC
    I still don't really follow what point you're trying to make, but you don't seem to get getting how forking and virtual memory works.

    Consider a process which uses two int/pointer sized memory locations. One contains a value, and the second contains a pointer to the first:

    virtual value address 0x10000 0x12345678 0x10008 0x10000
    Those are virtual addresses - those are the addresses the process sees, and which are printed out in things like SCALAR(0x10000). The CPU's hardware behind the scenes maps that page of address space for that process to some physical page in RAM. This is all behind the scenes - you never get to see physical addresses. After the fork, the child process gets a *copy* of the parents address space - same virtual address locations, but mapping to a different physical page of memory which contains a copy of the values:
    physical virtual value address address parent 0x2220000 0x10000 0x12345678 0x2220008 0x10008 0x10000 child 0x4440000 0x10000 0x12345678 0x4440008 0x10008 0x10000
    After the child modifies the value, you get this:
    parent 0x2220000 0x10000 0x12345678 0x2220008 0x10008 0x10000 child 0x4440000 0x10000 0xdeadbeef 0x4440008 0x10008 0x10000
    There is absolutely no connection between the parent and child aside from the fact that initially the child's value in its memory is a copy of the parent's. If you think your code shows shows something else, please say which exact line of output shows this, and what you think its output should be.

    Dave.

      > I still don't really follow what point you're trying to make

      from what I understood he's claiming that the child can see values being changed by the parent after forking.

      I doubt this and I can't see it demonstrated either.

      COW should also happen if the parent is changing a value.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re^3: Perl forked processes and variable sharing
by LanX (Sage) on Feb 02, 2022 at 01:01 UTC
    your code is hard to read for me, part of the problem is that you used <pre> instead of <code> tags.

    > What the child process finds at that passed address is exactly the same value that the parent wrote to that address

    when did you write to that address?

    If it's prior to forking, the value was copied.

    If the effect happens after forking, it could be a bug in COW (at least I wouldn't expect this)

    > but after the child writes to that address what happens behind the scenes is that that physical address is not overwritten in place,

    I'd say that's pretty much a description of COW = Copy On Write

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Hi again Rolf,

      I had tried enclosing my code in <code></code> tags, but found that all of my lines that were longer than a certain length were flowing onto the next line instead of just extending out to their full length, e.g.

      <code>
      This is a moderately long line of code which I would expect to see flo +wing all the way across the page.
      </code>

      I couldn't find a way to prevent that, so I tried <pre></pre> instead and it worked, e.g..

      <pre>
      This is a moderately long line of code which I would expect to see flowing all the way across the page.
      
      </pre>

      Thanks, Richard

        Hello fireblood,

        I had tried enclosing my code in <code></code> tags, but found that all of my lines that were longer than a certain length were flowing onto the next line...

        That’s not a bug, it’s a feature! Just find your Settings Nodelet and choose Display, then under Code List Settings make the value of Code Wrap Length larger (whatever suits your display)1 — and you can return to using <code> tags with no problems. :-)

        1Update: This only affects your view of the code; other monks see the same code wrapping according to their personal display settings.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2022-12-02 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?