Reassigning STDOUT for a forked process

Håkon Hægland

I am trying to assign STDOUT of a forked process to a pipe. For example

use warnings;
use strict;
use feature qw(say);
use IO::Select;

my $cmd='sleep 2; echo Hello';
pipe(PREAD, PWRITE);

my $pid=fork();
if ($pid==0) {
  *STDOUT=*PWRITE;
  exec $cmd;
  die "Could not run command \"$cmd\"";
}
my $sel = IO::Select->new( \*PREAD );
say "Waiting for background process..";
while (1) {
  my @ready = $sel->can_read;
  last if (! @ready);
  for my $fh (@ready) {
    say "Processing file descriptor ".($fh->fileno());
    my $line=<$fh>;
    if (! defined $line) {
      say "EOF";
      $sel->remove($fh);
      next;
    }
    chomp($line);
    say "Got line: \"$line\"..";
  }
}
waitpid ($pid,0);
say "Done.";

But nothing appears in the pipe PREAD; the child still outputs to STDOUT and not to the pipe.

Steffen Ullrich

*STDOUT=*PWRITE;

this will only assign the perl filehandle PWRITE to the perl filehandle STDOUT, but not change the underlying file descriptor of the OS. To do this you would need to duplicate the underlying file descriptor from PWRITE to fd 1 (STDOUT):

open(STDOUT,">&PWRITE");

After you've do this PWRITE is duplicated and is now accessible as PWRITE and STDOUT. A good style is to close(PWRITE) then to make STDOUT the only file handle for writing into the pipe.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check status of a forked process?

From Dev

Confusion with forked process?

From Dev

Linux, forked process hangs immediately

From Dev

Store the forked process' id in file

From Dev

Piping stdout of Process to another stdout

From Dev

Piping stdout of Process to another stdout

From Java

Why doesn't address change in forked process?

From Dev

Should I be terminating a forked child process with exit()?

From Dev

Messaging a forked child_process with just the pid

From Dev

Perl: pipe a serialized hash to a forked process

From Dev

Process forked from WCF Service is becoming idle

From Dev

How to use mutexes in a shared memory forked process?

From Dev

Redirect output of forked python process to pipe

From Dev

IPC Message queue not works with forked process

From Dev

Should I be terminating a forked child process with exit()?

From Dev

How to communicate/interface with a forked background process?

From Dev

Poco AsyncChannel does not exit on forked process exit

From Dev

Messaging a forked child_process with just the pid

From Dev

Linux / Perl - What happens when a process is forked?

From Dev

How to get a reference to a previously forked process in a module?

From Dev

C Sharedmemory only 1024 int in forked process

From Dev

Child process: The stdout parameter

From Dev

Waiting for a process or stdout on Windows

From Dev

What happens to a detached thread inside a forked process when the process dies?

From Dev

Python process forked by NodeJS - Alternative to process.send() for Python?

From Dev

Python process forked by NodeJS - Alternative to process.send() for Python?

From Dev

What happens to a detached thread inside a forked process when the process dies?

From Dev

Perl's odd behavior when reassigning a filehandle variable from STDOUT to a file without undef()

From Dev

Perl's odd behavior when reassigning a filehandle variable from STDOUT to a file without undef()

Related Related

  1. 1

    Check status of a forked process?

  2. 2

    Confusion with forked process?

  3. 3

    Linux, forked process hangs immediately

  4. 4

    Store the forked process' id in file

  5. 5

    Piping stdout of Process to another stdout

  6. 6

    Piping stdout of Process to another stdout

  7. 7

    Why doesn't address change in forked process?

  8. 8

    Should I be terminating a forked child process with exit()?

  9. 9

    Messaging a forked child_process with just the pid

  10. 10

    Perl: pipe a serialized hash to a forked process

  11. 11

    Process forked from WCF Service is becoming idle

  12. 12

    How to use mutexes in a shared memory forked process?

  13. 13

    Redirect output of forked python process to pipe

  14. 14

    IPC Message queue not works with forked process

  15. 15

    Should I be terminating a forked child process with exit()?

  16. 16

    How to communicate/interface with a forked background process?

  17. 17

    Poco AsyncChannel does not exit on forked process exit

  18. 18

    Messaging a forked child_process with just the pid

  19. 19

    Linux / Perl - What happens when a process is forked?

  20. 20

    How to get a reference to a previously forked process in a module?

  21. 21

    C Sharedmemory only 1024 int in forked process

  22. 22

    Child process: The stdout parameter

  23. 23

    Waiting for a process or stdout on Windows

  24. 24

    What happens to a detached thread inside a forked process when the process dies?

  25. 25

    Python process forked by NodeJS - Alternative to process.send() for Python?

  26. 26

    Python process forked by NodeJS - Alternative to process.send() for Python?

  27. 27

    What happens to a detached thread inside a forked process when the process dies?

  28. 28

    Perl's odd behavior when reassigning a filehandle variable from STDOUT to a file without undef()

  29. 29

    Perl's odd behavior when reassigning a filehandle variable from STDOUT to a file without undef()

HotTag

Archive