Hey, Thanx in advanced to anyone who can help solve my delima. I have written a little remote sync program that allows simple socket program to allow remote clients to get updates based on queries. The script works fine except for when a child dies the process goes and eats the cpu at 100%. I have tried several different ideas that I've come up with in my head.
The code below is what I am currently working with. Everything works fine in the idea that when the program starts it starts it breaks away from the terminal that started it. Then it goes in and starts a child for each client that connects to it, but when that child dies or quits the parent process hogs the CPU at 100% I am able to kill the parent but I need to be able to prevent this from happening. Please help me.
use POSIX qw(:sys_wait_h);
use IO::Socket;
use DBI;
$port=8080;
$pid = fork;
exit if $pid;
die "Couldn't fork: $!" unless defined($pid);
POSIX::setsid() or die "Can't start a new session: $!";
$time_to_die=0;
sub signal_handler{
$time_to_die=1;
}
sub REAPER{
1 until (-1 == waitpid(-1, WNOHANG));
}
$sock = IO::Socket::INET->new(LocalPort => $port,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 25,
Timeout => 120) or die "Couldn't be a tc
+p server on port $port : $@\n";
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler;
$SIG{CHLD} = \&REAPER;
until ($time_to_die){
while($new_sock = $sock->accept()) {
next if $child = fork;
die "child fork: $!" unless defined $child;
$sock->close;
login($new_sock);
exit;
}
close($sock);
}
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.