Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Perl forked processes and variable sharing

by dave_the_m (Monsignor)
on Feb 02, 2022 at 07:44 UTC ( [id://11141051] : note . print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: Perl forked processes and variable sharing
by LanX (Saint) on Feb 02, 2022 at 11:46 UTC
    > 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