Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Threads Vs Fork

by targetsmart (Curate)
on Nov 08, 2008 at 08:22 UTC ( [id://722374]=perlquestion: print w/replies, xml ) Need Help??

targetsmart has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a requirement to write a socket server(daemon), I have been using fork for handling multiple child connections. I am interested in using threads in perl, but as far as my reading about perl threads(perlthrtut, threads) tells me that going for threads in perl will not be a better choice than using fork. Can someone help me on this issue.

Replies are listed 'Best First'.
Re: Threads Vs Fork
by Perlbotics (Archbishop) on Nov 08, 2008 at 10:28 UTC
      I have read the URL you pointed out, thanks for providing the useful link, I should have used super search.
Re: Threads Vs Fork
by BrowserUk (Patriarch) on Nov 08, 2008 at 15:54 UTC

    There's a lot of guff and bluster around on this subject. And a lot of it comes from otherwise rational and reliable sources.

    I needed a quick "concurrent clients" socket server for testing another piece of code against, so I knocked this up:

    #! perl -slw use strict; use IO::Socket; use threads; our $ADDR ||= 'localhost:35007'; my $listener = IO::Socket::INET->new( LocalAddr => $ADDR, Listen => 5, Reuse => 1, ) or die $^E; while( my $client = $listener->accept ) { async { my($port, $iaddr) = sockaddr_in( getpeername( $client ) ); printf "From %s:%d: %s", join('.',unpack 'C4', $iaddr ), $port +, $_ while <$client>; close $client; }->detach; }

    Simplistic, but it easily keeps up with 5 of these each fowarding the traffic from 100 monitored log files, so it didn't need to be any more complex.

    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.
Re: Threads Vs Fork
by zentara (Archbishop) on Nov 08, 2008 at 14:48 UTC
    Try Simple threaded chat server and see how it works for you. What you want to watch out for is a memory gain after a long run time. The thing is forks cleanup after themselves completely, whearas a threaded app MAY retain some useless references and gain memory with prolonged usage.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Threads Vs Fork
by szabgab (Priest) on Nov 08, 2008 at 13:43 UTC

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-07-19 09:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.