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

Hi all,

I am currently developing a client-server application which goal is to measure the maximum number of open connections.
I wrote a client script that opens the desired number of sockets (using forks).
The problem is, however, with the amount of memory that the server script needs = that the big number of threads need.

Here is the code of a server script:
my $REQUESTED_SESSIONS : shared = 0; my $ACT_SESSION_CNT : shared = 0; # ************** START ************** # print "[SERVER] START\n"; my $socket = create_socket($protocol, $port, $listen, $reuse); while ( ($client, my $client_addr) = $socket->accept()) { if($REQUESTED_SESSIONS == 0) { print "[SERVER] ------- Sessions requested..? -------\n"; # get the desired number of sessions from client $REQUESTED_SESSIONS = receive_command($client); if(!defined $REQUESTED_SESSIONS) { $REQUESTED_SESSIONS = 10; } $ACT_SESSION_CNT = 0; next; } # get CLIENT data my ($client_port, $client_ip) = sockaddr_in($client_addr); my $client_ip_num = inet_ntoa($client_ip); print "[SERVER] received ([$client_ip_num] on $client_port)\n"; print "[threading]\n"; send_command($client, "connection_established"); my $thread = threads->new(\&process_session, $client)->detach; } # ************** END ************** # ##################################################################### ## ## function for processing client ## ##################################################################### sub process_session { my $client = shift; my $thread_id = threads->self()->tid(); print "[SERVER] [Thread $thread_id]\n"; increment_act_sessions(); # every thread waits for all connections while($ACT_SESSION_CNT < $REQUESTED_SESSIONS) { sleep 1; } # send a message to every client that the test is over send_command($client, "close_session"); print "[closing the client - THREAD $thread_id]\n"; close $client; lock($REQUESTED_SESSIONS); $REQUESTED_SESSIONS--; if($REQUESTED_SESSIONS == 0) { print "[SERVER] Finished.\n"; print "[SERVER] Ready for more!\n"; print "[SERVER] ------- Sessions requested..? -------\n"; } }
I know that the idea of continuously checking in every thread if all the requested clients are connected isnt perfect but to my mind it should work.. and it works..
However, the amount of memory that f.ex. 500 threads need is huge and that is why I would like to ask you a simple question:
Is it because the idea of the program is so bad or the reason is maybe that I did something wrong with threads?

Big thanks in advance for any help.
Pawel

Replies are listed 'Best First'.
Re: Multiple threads for many connections - memory problem
by BrowserUk (Patriarch) on Feb 21, 2008 at 11:44 UTC

    You could try using use threads ( stack_size => 4096 );. On my system that means that 500 simple threads consume 177 MB, or around 350k per thread.

    If that is still too much, then a IO::Select solution will definitely consume less memory, and probably wouldn't be much more complex for an application as simple as this.


    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.