Hi,
I'm still trying to get familiar with threads and sockets.

However, I got in some troubles with memory leaks, could anyone perhaps give 
me a hint in which way I should write a multithreaded socket server ?


I wrote a small server, which accepts connections on a tcp socket and spawns a 
new thread for each connection, which terminates when the socket has been 
closed.

The server runs fine, however, while stresstesting, I experienced a runaway of 
the memory consumption.
After a few hundred connects and disconnects I'll get an Out of memory. :-(

In order to test threads in perl, I wrote a new script which just spawns and 
terminates threads. ( Attached it below )

I already figured out that global variables seem to lead to memory leaks, 
however, although the memory size raises slower now, it still raises. ( which 
I didn't expect ).

I stumbled over http://search.cpan.org/~rybskej/forks-0.23/lib/forks.pm, which 
seems to be a replacement for ithreads and also has the advantage of not 
needing a thread enabled perl.
However, there are still memory leaks with this module.



My conclusion is to write a preforked server, using the forks module mentioned 
above and create a pool of  say 50 preforked threads at startup time.

But, are there perhaps any better possiblities or recommendations ?

Thanks,
Michael

--------------------------

#!/usr/bin/perl -w

use threads;
use threads::shared;

#use forks;
#use forks::shared;

share $threadscount;
$threadscount = 0;

share $createthreads;
$createthreads = 1;

share $maxthreads;
$maxthreads = 0;


sub thread{
                my $count;
                { 
                                lock $threadscount;
                                $threadscount ++;
                                $count = $threadscount;

                                lock $maxthreads;
                                if ( $count>$maxthreads ){
                                                $maxthreads = $count;
                                }
                }
                #print "$count\n";
                sleep 4;

                lock $threadscount;
                $threadscount --;
}


sub threadcreator{
                my $tc;
                do {
                                my $t = threads->create("thread");
                                $t->detach();
                                lock $createthreads;
                                $tc = $createthreads;
                } while ( $tc );
}
        

while ( 1 ){
{
                lock $createthreads;
                $createthreads = 1;
}

my $thread = threads->create("threadcreator");
$thread->detach();
sleep 4;

{
                lock $createthreads;
                $createthreads = 0;
}

my $count;
do {
                #sleep 3;
                lock $threadscount;
                $count = $threadscount;
} while ( $count > 0 );
{ 
                lock $maxthreads;
                print "maxthreads: $maxthreads\n";
}
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to