$SIG{CHLD} = \&REAPER;
# now do something that forks...
-or even the more elaborate:
+or better still:
use POSIX ":sys_wait_h";
sub REAPER {
my $child;
+ # If a second child dies while in the signal handler caused by the
+ # first death, we won't get another signal. So must loop here else
+ # we will leave the unreaped child as a zombie. And the next time
+ # two children die we get another zombie. And so on.
while (($child = waitpid(-1,WNOHANG)) > 0) {
$Kid_Status{$child} = $?;
}
STDIN. If you open a pipe I<from> minus, you can read from the filehandle
you opened whatever your kid writes to his STDOUT.
- use English;
+ use English '-no_match_vars';
my $sleep_count = 0;
do {
my $waitedpid = 0;
my $paddr;
+ use POSIX ":sys_wait_h";
sub REAPER {
- $waitedpid = wait;
+ my $child;
+ while (($waitedpid = waitpid(-1,WNOHANG)) > 0) {
+ logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
+ }
$SIG{CHLD} = \&REAPER; # loathe sysV
- logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
}
$SIG{CHLD} = \&REAPER;
my $waitedpid;
+ use POSIX ":sys_wait_h";
sub REAPER {
- $waitedpid = wait;
+ my $child;
+ while (($waitedpid = waitpid(-1,WNOHANG)) > 0) {
+ logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
+ }
$SIG{CHLD} = \&REAPER; # loathe sysV
- logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
}
$SIG{CHLD} = \&REAPER;
Once the generic server socket has been created using the parameters
listed above, the server then waits for a new client to connect
-to it. The server blocks in the C<accept> method, which eventually an
-bidirectional connection to the remote client. (Make sure to autoflush
+to it. The server blocks in the C<accept> method, which eventually accepts a
+bidirectional connection from the remote client. (Make sure to autoflush
this handle to circumvent buffering.)
To add to user-friendliness, our server prompts the user for commands.