http://qs1969.pair.com?node_id=722374

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