in reply to Communicate with child process via stdin, stdout

I've tried with IPC::Open2 , but it doesn't work

Are you on Microsoft Windows? Try this example if you are on a unix style system.

#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; #interface to "bc" calculator my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter expression for bc, i.e. 2 + 2\n"; chomp(my $query = <STDIN>); #send query to bc print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Communicate with child process via stdin, stdout
by Eliya (Vicar) on Nov 20, 2011 at 13:26 UTC
    waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    As an aside, waitpid at the end of a script (as you have it here) is not important or necessary. Nothing bad will happen without it, as the OS will automatically reap any zombies when their parent process has gone away. In other words, zombies can only exist as long as the parent is still alive.

      Dunno...   I have encountered zombies with a parent-PID of “1.”   Dunno why it happens and I don’t think it should, but, “there they were.”   I have only seen it in a FastCGI process (owned by Plack), but I have seen it nonetheless.   It probably is a good idea, then, to clean up after oneself explicitly.   If you rendezvous with every child-process that you created, and show them to their graves, then everything is nice and neat and predictable.

        The OS re-parents a process to the init process (PID 1) when a child process is still running at the time the parent has gone away. This is entirely different from the situation above, because when you're dead you yourself can no longer wait for your children anyway.