Index: /gsdl/trunk/perllib/downloaders/WgetDownload.pm
===================================================================
--- /gsdl/trunk/perllib/downloaders/WgetDownload.pm	(revision 17353)
+++ /gsdl/trunk/perllib/downloaders/WgetDownload.pm	(revision 17354)
@@ -75,4 +75,37 @@
 
 
+# Declaring variables related to the wget child process so that the termination
+# signal handler for SIGTERM can close the streams and tidy up before ending
+# the child process.
+my $childpid;
+my($chld_out, $chld_in);
+
+# Handler called when this process is killed or abruptly ends due to receiving
+# one of the terminating signals that this handler is registered to deal with.
+sub abrupt_end_handler {
+    my $termination_signal = shift (@_);
+    {
+	if(defined $childpid) {
+	    close($chld_out);
+	    close($chld_in);
+	
+	    # Send TERM signal to child process to terminate it. Sending the INT signal didn't work
+	    # See http://perldoc.perl.org/perlipc.html#Signals 
+	    # Warning on using kill at http://perldoc.perl.org/perlfork.html
+	    kill("TERM", $childpid); 
+	}
+    }
+    exit(0);
+}
+
+# Registering a handler for when termination signals SIGINT and SIGTERM are received to stop
+# the wget child process. SIGTERM--generated by Java's Process.destroy()--is the default kill
+# signal (kill -15) on Linux, while SIGINT is generated upon Ctrl-C (also on Windows). 
+# Note that SIGKILL can't be handled as the handler won't get called for it. More information: 
+# http://affy.blogspot.com/p5be/ch13.htm
+# http://perldoc.perl.org/perlipc.html#Signals
+$SIG{'INT'} = \&abrupt_end_handler;
+$SIG{'TERM'} = \&abrupt_end_handler;
+
 sub new {
     my ($class) = shift (@_);
@@ -147,9 +180,12 @@
     }
     my $wget_file_path = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
-    $command = "\"$wget_file_path\" $cmdWget |";
-
-    open(*WIN,$command) || die "wget request failed: $!\n";
-
-    while (defined($strLine=<WIN>))
+    #$command = "\"$wget_file_path\" $cmdWget |";    #open(*WIN,$command) || die "wget request failed: $!\n";
+    #open(*WIN,$command) || die "wget request failed: $!\n";    
+
+
+    $command = "\"$wget_file_path\" $cmdWget";
+    $childpid = open2($chld_out, $chld_in, $command) || die "wget request failed: $!\n";
+
+    while (defined($strLine=<$chld_out>)) # we're reading in from child process' stdout
     {
 	if($blnShow)
@@ -160,6 +196,15 @@
 	$strReadIn .= $strLine;
     }
-
-    close(WIN);
+    
+    close($chld_in);
+    close($chld_out);
+    
+    # Program terminates only when the following line is included 
+    # http://perldoc.perl.org/IPC/Open2.html explains why this is necessary
+    # it prevents the child from turning into a "zombie process".
+    # While the wget process terminates without it, this perl script does not: 
+    # the DOS prompt is not returned without it.
+    waitpid $childpid, 0;
+
     if ($changed_dir) {
 	chdir $current_dir;
@@ -182,9 +227,10 @@
     }
     my $wget_file_path = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
-    my $command = "\"$wget_file_path\" $cmdWget 2>&1 |";
-
+    #my $command = "\"$wget_file_path\" $cmdWget 2>&1 |"; #open(*WIN,$command) || die "wget request failed: $!\n";
 ###    print STDERR "**** wget cmd = $command\n";
-
-    open(*WIN,$command) || die "wget request failed: $!\n";
+    #open(*WIN,$command) || die "wget request failed: $!\n";
+
+    my $command = "\"$wget_file_path\" $cmdWget";
+    $childpid = open2($chld_out, $chld_in, $command) || die "wget request failed: $!\n";
 
     my $full_text = "";
@@ -193,5 +239,5 @@
     my $line;
 
-    while (defined($line=<WIN>))
+    while (defined($line=<$chld_out>)) # we're reading in from child process' stdout
     {
 	if((defined $blnShow) && $blnShow)
@@ -212,5 +258,13 @@
     }
 
-    close(WIN);
+    close($chld_in);
+    close($chld_out);
+
+    # Program terminates only when the following line is included 
+    # http://perldoc.perl.org/IPC/Open2.html explains why this is necessary
+    # it prevents the child from turning into a "zombie process".
+    # While the wget process terminates without it, this perl script does not: 
+    # the DOS prompt is not returned without it.
+    waitpid $childpid, 0;
 
     my $command_status = $?;
