--- loncom/lonmaxima 2006/03/08 12:57:30 1.13 +++ loncom/lonmaxima 2006/03/08 14:22:14 1.14 @@ -3,7 +3,7 @@ # The LearningOnline Network with CAPA # Connect to MAXIMA CAS # -# $Id: lonmaxima,v 1.13 2006/03/08 12:57:30 www Exp $ +# $Id: lonmaxima,v 1.14 2006/03/08 14:22:14 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -65,6 +65,7 @@ sub REAPER { # ta $SIG{CHLD} = \&REAPER; my $pid = wait; $children--; + &logthis("Child $pid for port or process $children{$pid} died"); delete($usedmaximaports{$children{$pid}}); delete($children{$pid}); } @@ -232,10 +233,13 @@ while (1) { for (my $i = $children; $i < $PREFORK; $i++) { &status('Parent process, starting child'); my $newport; - foreach $newport ($STARTPORT .. $STARTPORT+$PREFORK-1) { - if (!defined($usedmaximaports{$newport})) { last; } + &logthis("Current pool: ".join(', ',keys %usedmaximaports)); + foreach my $testport ($STARTPORT .. $STARTPORT+$PREFORK-1) { + if (!$usedmaximaports{$testport}) { $newport=$testport; } + } + if ($newport) { + &make_new_child($server,$newport); # top up the child pool } - &make_new_child($server,$newport); # top up the child pool } } @@ -255,8 +259,10 @@ sub make_new_child { or die("Can't unblock SIGINT for fork: $!\n"); $children{$pid} = $maximaport; $children++; + $usedmaximaports{$maximaport}=1; return; } else { + &logthis("Starting child on port $maximaport"); # Child can *not* return from this subroutine. $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before @@ -273,11 +279,12 @@ sub make_new_child { or die "making socket: $@\n"; &nonblock($maximaserver); my $maximaselect=IO::Select->new($maximaserver); + sleep(2); # open MAXIMA to talk to that port my ($cmd_in, $cmd_out, $cmd_err); my $maximapid = open3($cmd_in, $cmd_out, $cmd_err, "maxima -s $maximaport"); - $children{$maximapid} = 1; + $children{$maximapid} = "Maxima $maximapid port $maximaport"; my $prompt=<$cmd_out>; &logthis("Maxima $maximapid: $prompt");