Hi gurus,

   Some time ago I've been coding some tools in C/C++ which communicated with
fork/kill. Now I'd like to do similar thing in Perl/Win98, but can't get it to
work, can you help me figure out how to do it under Perl/Win98? In child part
there's "if( 0 ){ /final example/ } else { /simple example/ }" - neither simple
nor final example is working.

   Here's my perl code:

------------------- 8< ---------------------------------------
#perl
use strict;
use warnings;
use diagnostics;

my ( $aPid, $chldDone, $line, $bRead );

sub catch_int;

$chldDone = 0;
$| = 1;
$aPid = fork ;
$SIG{INT} = \&catch_int;

if( ! defined $aPid ){
        print "Cannot fork any children: $! ($^E)\n";
        exit -1;
}

if( $aPid != 0 ){ # main (parent) process
        printf "Parent: my child pid is \$aPid = %x\n", $aPid;

        for( 0..3 ){
                print "Parent: process is alive ($_)\n";
                sleep 1;
        }
        printf "Parent: killing its child (%x)...\n", $aPid;
        kill 'INT', $aPid;
        sleep 3;
        print "Parent: I'll be back\n";

} else { # child process

        print "Child: I want your input\n";

        do {
                if( 0 ){ # to easily switch between two examples
                        eval {
                                $bRead = sysread STDIN, $line, 128;
                        };
                        print "Child: After eval: \$chldDone==$chldDone\n";
                        print "\$@ == [EMAIL PROTECTED]" if( $@ );

                        if( !defined $bRead ){
                                print "Child: sysread error: $! ($^E)\n";
                                last;
                        }
                        if( $bRead==0 ){
                                print "Child: sysread - that's all\n";
                                last;
                        }
                        if( $chldDone==1 ){
                                last;
                        }
                        chomp $line;
                        print "Child: You entered: $line \n";
                } else {
                        eval {
                                sleep 1;
                                print "Child: \$chldDone==$chldDone\n";
                        };
                        print "Child: After eval: \$chldDone==$chldDone\n";
                        print "Child: \$@ == [EMAIL PROTECTED]" if( $@ );
                }
        } while( $chldDone==0 );

        print "Child: going to die, \$chldDone = $chldDone\n";

}

exit 0;

sub catch_int {
        my $signame = shift;
        $chldDone = 1;
        $SIG{INT} = \&catch_int;
        die "\ncatch_int: got SIG$signame";
}
------------------- 8< ---------------------------------------

and here's output I got while running in "simple example":

------------------- 8< ---------------------------------------
Parent: aPid = fff15857
Parent: process is alive (0)
Child: I want your input
Parent: process is alive (1)
Child: $chldDone==0
Child: After eval: $chldDone==0
Parent: process is alive (2)
Child: $chldDone==0
Child: After eval: $chldDone==0
Parent: process is alive (3)
Child: $chldDone==0
Child: After eval: $chldDone==0
Parent: killing its child (fff15857)...
Parent: I'll be back
------------------- 8< ---------------------------------------

   Or maybe my approach is wrong for what I'm trying to do: I have perl app
which talks through socket and sysread/syswrite. Now it's text-mode app and I'd
like to make it GUI app (wxWindows), so I thought that: if sysread is blocking,
then I have to do make it child process of GUI. If GUI receives "File/Exit",
it'll send SIGINT to it's sysread child. Then the child will have to terminate
sysread, figure out that it received SIGINT and exit gracefuly. Another thing
is that I'd like enable child to report what it is doing to GUI parent
(open2?). Is there a chance that I'll make it running under Win98?

   Could you Perl gurus help me with that? Or point me to more appriopriate
wizards?

Greetings,
---
 Grzegorz Hayduk            AGH - University of Science and Technology
  Phone +48(12)617-28-83    mailto:[EMAIL PROTECTED]
  Cell  +48(600)314-463     http://www.kaniup.agh.edu.pl/~hayduk
  
"GUIs normally make it simple to accomplish simple actions and 
impossible to accomplish complex actions."  --Doug Gwyn

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to