smackdab has asked for the wisdom of the Perl Monks concerning the following question:

I read the Ram book and tried to modify a sample...

I want to fork off some small number of programs and display the ouput in my main window. I tried using fork and `file.exe` together but it just hung. The example below works if you don't call an external program and just pass a "string".

I am running Win32 and using the "new" fork()...

use IO::Handle; pipe(READER, WRITER); WRITER->autoflush(1); if ($pid = fork) { close WRITER; my $buf = ""; while (<READER>) { $buf .= $_; } chomp($buf); print "Parent Pid $$ just read this:\n$buf\n"; close READER; waitpid($pid, 0); } else { die "cannot fork: $!" unless defined $pid; close READER; # my $out = `ping 555-1212`; print WRITER "Child Pid $$ is sending this\n"; print PARENT_WRT $out; close WRITER; exit; }
Is it because of Win32 or is there a more standard way of doing this?

thanks !!!

Replies are listed 'Best First'.
Re: Forked up
by tachyon (Chancellor) on Oct 09, 2001 at 07:09 UTC
      Thanks, I tried the examples and they exhibit the same problem...just add:

      $buf=`ping 127.0.0.1`;

      to the "#this is child" block...

      I don't know stdout/stdin...but wonder about it...

      thanks!

        Windows was not designed with forking in mind. I get the same problem using backticks - they work fine in the parent but hang in the child. Probably relates to the fork() implementation/emulation on Windows. Solution - don't use backtics in child. This works fine using the Net::Ping module.

        use Net::Ping; pipe (FROM_CHILD, TO_PARENT) or die "Oops Pipe: $!\n"; pipe (FROM_PARENT, TO_CHILD) or die "Oops Pipe: $!\n"; select((select(TO_CHILD), $|++ )[0]); # set autoflush select((select(TO_PARENT), $|++ )[0]); # set autoflush $|++; my $result; # let's fork if ( my $pid = fork ) { # this is parent print "Sending to kid\n"; sleep 1; print TO_CHILD "perlmonks.org\n"; chomp( my $child_says = <FROM_CHILD> ); print "Child says '$child_says' to parent\n"; $result = $child_says; waitpid( $pid, 0 ); } else { # this is child die "Can't fork: $!\n" unless defined $pid; print "Waiting to get info from parent\n"; chomp( my $parent_says = <FROM_PARENT> ); print "Parent says '$parent_says' to child\n"; my $p = Net::Ping->new(); my $ping = $p->ping($parent_says); print "Net::Ping in child returned: $ping\n"; $p->close(); print TO_PARENT "Child pid $$ replies:$ping\n"; exit; } close FROM_CHILD; close TO_CHILD; close FROM_PARENT; close TO_PARENT; print "Parent got $result\n";

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Forked up
by CubicSpline (Friar) on Oct 09, 2001 at 07:08 UTC
    I'm relatively new, but it doesn't seem to me that you want to be using fork. perldoc: func describes fork() as: "fork - create a new process just like this one".

    Perhaps what you want is perldoc: system which lets you call other executable code and wait for that code to complete.

      That's right. Do a fork, then do a system (or backticks) so that you can wait for the external program while still running the main program.