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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.