Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Threads Vs Fork

by targetsmart (Curate)
on Nov 08, 2008 at 08:22 UTC ( #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 (Canon) 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 (Pope) 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
Node Status?
node history
Node Type: perlquestion [id://722374]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-02-20 06:28 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (294 votes). Check out past polls.