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

Re^2: Standard way to convert timezone in multithreaded script

by tirwhan (Abbot)
on Nov 24, 2009 at 11:37 UTC ( #809056=note: print w/ replies, xml ) Need Help??


in reply to Re: Standard way to convert timezone in multithreaded script
in thread Standard way to convert timezone in multithreaded script

i would bet for security, a pid gets spawned into 1 core, and since threads on linux share the same master pid, all threads probably will be assigned to the same core

Can I name an amount? Because I'll take that bet :-). Sorry, but this is completely wrong, and if you think about it for a minute you'll realize that it would be completely brainless and negate the usefulness of kernel threads entirely. Yes, threads are spawned under a single PID, but they can very well use more than a single core. This is simple to demonstrate (WARNING, following code will hog your CPUs, don't run it on a production system!):

perl -Mthreads -e'$n=3;for (1..$n){$t[$_]=threads->create(sub{my $r; while(1){$r++}});}; sleep 10;threads->exit()'

Set $n to 1 less than the number of cores your machine has (in this case 3 for a quad-core). Then run the command and observe its behaviour in the system monitor of your choice (top will do). You'll see that it runs on the number of CPU cores specified, despite retaining a single PID. So your assumption is wrong.

As for the usefulness/performance benefit of updating a shared variable from threads running on several cores, I'm staying firmly out of that debate. My own gut feeling would be that it is indeed not worth it on Linux, context switching Perl threads is more expensive than doing the same with processes (or at least it was the last time I measured), and a short test with the above command line using threads::shared seems to support the feeling. But the true answer will invariably depend (almost exclusively) on the concrete problem the OP is trying to solve, so he should benchmark competing solutions to that concrete problem and find out for himself.


All dogma is stupid.


Comment on Re^2: Standard way to convert timezone in multithreaded script
Download Code
Re^3: Standard way to convert timezone in multithreaded script
by zentara (Archbishop) on Nov 24, 2009 at 12:58 UTC
    ... i don't have a multi-cpu system to test on, but i accept your explanation that a single process can be running things across core boundaries.... but that seems quite insecure and it would seem to be a good kernel option to have ( like in the NSA-security kernel )...why?.... because according to your explanation, any process can start injjecting code into all the other cores of a multicore machine, just by starting a few threads..... how does this affect the nice value of the parent thread... does it increase according to thread count?....... what does it mean then to give a nice value to a script?... if the script can start spawning an infinite number of threads..... or what is to stop core hijacking by me spawning a thread until it gets into the desired core.... then executing something that halts the core, or other malicious intent...oO( maybe promoting threading is good for national security ;-) )

    ... my original intent was to point out that threads are not the fastest IPC out there, and continuing in that vein, there is Why use threads over processes, or why use processes over threads? and Threads versus fork, which to use? and Using forks and threads ...then choose the IPC right for you

    ..... by the way.... you would have no such POSIX module thread-safety in a fork-and-exec


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

      The security concerns you name are completely irrelevant. Any user who can execute code can hog all cores of a system, any time they want to, by starting processes that do so (unless you restrict them with something like ulimit, which works just as well on threaded processes). The kernels job is to keep the system running responsive enough so that other user processes and the system in general run well. Try running the line I sent, it'll work on a single-core system just as well. If you're running a reasonably modern version of Linux you may experience some slowdown on certain other applications, but your system should not grind to a halt, nor show real signs of unresponsiveness. That's the kernel doing its job, no reason to panic, business as usual.

      So all the talk about hijacking, injecting etc. completely misses the point of a multi-user system, yes you can do all these things with threads (if you're root), just as you could with processes. But no, it isn't a problem (if it were, by your reasoning, system security would be impossible on a single-core machine). The kernel only allows you as much access as the system policy dictates, and the system administrator sets system policy. You may have a point that multi-threaded apps can make things harder for the system administrator by obscuring what they're doing, because most Linux tools are built primarily with the multi-process model in mind. But, honestly, in over a decade of sysadmining multi-user systems I have never found this to be a problem.

      BTW, the nodes you link to aren't really pertinent to a discussion about threads vs. processes today, they're from 2003 (only the last one, which contains very little helpful discussion on the topic, is from 2006), a time when most people were still running Perl 5.6 and (if on Linux) a 2.4 kernel. Lots of things have changed since then, and any serious discussion on the topic would have to take these changes into account (I have no desire to hold such a debate, I find it pretty pointless).


      All dogma is stupid.
        BTW, the nodes you link to aren't really pertinent to a discussion about threads vs. processes today, they're from 2003

        .... that's not true..... the problems have not been totally solved..... just look at all the relatively recent nodes where threads are crashing due to module safety problems..... and most thread use involve modules


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku

      You need to look up "Ingo Molnar" and "Native POSIX Thread Library (NPTL)" and read (a lot!), before you expose the depth of your misunderstanding even further.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        .... i'll take your word for it in Re: Why use threads over processes, or why use processes over threads? which is in an interesting about face from your current position

        BrowserUk states:

        The result is, that perl threading implementation as is, is at best utilitarian, and at worst, broken. This is unsurmountable given the nature of perl's core as is, and would only be fixable with a complete re-write of the perl core. The problem runs very deep. Even the POSIX C-runtime that underlies so much of the perl core is inherently non-reentrant, and without reentrancy built-in from the lowest levels, making effective use of threads, where these are natively fast and efficient, simply isn't possible.

        .... i dunno where my ignorance comes from...maybe you?


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku
Re^3: Standard way to convert timezone in multithreaded script
by rowdog (Curate) on Nov 24, 2009 at 18:09 UTC
    perl -Mthreads -e'$n=3;for (1..$n){$t[$_]=threads->create(sub{my $r; while(1){$r++}});}; sleep 10;threads->exit()'

    FWIW, I can attest that setting n=2 pegs both cores on my amd 3800+ running Arch Linux. However, n=1 (recommended setting) only pegs one core.

      However, n=1 (recommended setting) only pegs one core.

      Yes, of course. A single thread will only execute on one core. Which core that is may change (it's up to the OS), but it won't run on more than one core simultaneously. Multiple threads will.

      (I guess you already know this, just saying it explicitly in case someone reads all of this and is under the impression that simply saying "use threads;" will make a program run concurrently on several CPUs. That would be magic, not concurrency :-)


      All dogma is stupid.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-12-27 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls