Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Perl forked processes and variable sharing

by dave_the_m (Monsignor)
on Feb 01, 2022 at 21:58 UTC ( [id://11141026] : note . print w/replies, xml ) Need Help??


in reply to Perl forked processes and variable sharing

I didn't look at your code and description in depth, but are you aware that in a virtual memory system, (every major UNIXy OS in the last 25 years), a forked child and its parents have separate memories? i.e. if the parent writes to a byte at address 0x10000, that change will not appear in the child's address 0x10000, since those two (identical) virtual addresses map to different physical addresses.

The slight exception is Windows, where perl emulates a fork by using two threads sharing the same virtual address space.

Dave.

  • Comment on Re: Perl forked processes and variable sharing

Replies are listed 'Best First'.
Re^2: Perl forked processes and variable sharing
by fireblood (Scribe) on Feb 02, 2022 at 00:32 UTC
    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
      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

      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