Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^3: Critical sections; In the Perl interpreter

by andal (Hermit)
on Feb 20, 2017 at 10:50 UTC ( #1182334=note: print w/replies, xml ) Need Help??

in reply to Re^2: Critical sections; In the Perl interpreter
in thread Critical sections; In the Perl interpreter

And, copy on write has almost zero benefit in perl scripts

Well, there's nothing to argue about here. I was not talking about benefits for perl scripts. I've just mentioned existing feature. So, you are right when you say that it does not bring much of benefits for perl because perl scripts are not in code segments but in data segments.

Actually, Perl's threads are very different from your description

It all depends on what is "difference". I only mentioned basic idea of thread vs process. That idea stays the same: thread shares memory of process with other threads. Of course there are different strategies on how that memory is shared and particular sharing may only worsen memory consumption at the end.

The bottom line is that a working knowledge of the basic executable structure doesn't tell you much about Perl processes and almost nothing about Perl threading.

That is true. I just didn't see that the OP wanted to have knowledge about Perl threading. I thought, that the question was more about memory management for Perl. So I pointed out, that perl scripts are loaded into data segment and they don't get shared like segments of perl itself. If I misunderstood the question, then sorry.

  • Comment on Re^3: Critical sections; In the Perl interpreter

Replies are listed 'Best First'.
Re^4: Critical sections; In the Perl interpreter
by BrowserUk (Patriarch) on Feb 20, 2017 at 13:03 UTC
    Of course there are different strategies on how that memory is shared and particular sharing may only worsen memory consumption at the end.

    The main difference between your description and Perl's threading strategy is that in addition to per-thread stack segments, Perl threads have per-thread data segments as well.

    You are right that C threads have shared access to all of the process' memory and that the Perl separation is only enforced by Perl. But equally, Perl's lexical variables are "only enforced by perl" and they work quite well.

    Also, I know many see those separate data segments as "just chunks of the heap", but the fact is that "the heap" is rarely a single heap in the old sense of the terms; and hasn't been for ... well nearly forever.

    Although the "segments" I describe above are actually all just part of a single contiguous virtual address space -- not true segments in the 80286 sense -- if they are each allocated using separate calls to VirtualAlloc() (nmap() I think?), and some (even just a single page of) virtual address space is left unallocated between each of the "segments", then the classical fear of an unbounded loop in one thread succeeding in silently scribbling across all the "segments" cannot happen; as once the loop exceeds the bounds of the segment it started in and hits one of the unallocated (guard) pages, a 'segfault' occurs.

    Thus, in most cases, with the language enforcing the allocation of variable addresses and guard pages to detect attempts to cross boundaries, multiple heaps can work very effectively to provide virtual segments that are largely enforced by the hardware without the need for the creation and manipulation of expensive Critical Sections to control access.

    Indeed, with Perl thread's explicitly shared data, only the explicitly shared virtual segment needs a CritSec to synchronise access; and because the explicitly sharedness is a language level construct; most of the CritSec manipulations required for the (internal) safety of shared data can be moved internal to the code providing that construct. Some user level locking is required to protect user-level sharing; but internal level sharing is transparent and automatic.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-12-10 10:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found