Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Redirect the process group stdout to parent

by Anonymous Monk
on May 27, 2010 at 12:29 UTC ( #841910=perlquestion: print w/replies, xml ) Need Help??

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

run.pl

use strict; use warnings; use IPC::Run qw(run); my($wtr, $rdr, $err); my @cmd = qw(script); my $pid = fork; my $timvariable = 7; unless($pid) { setpgrp; run \@cmd, '>>', "out.txt", '2>>', "err.txt"; } print "Child process id:$pid\n"; for(my $i =0;$i <= $timvariable ; $i++) { sleep(1); print "Parent is waiting to obtain the file which was created by c +hild process\n"; } kill 9,"-$pid"; kill 9,"$pid";

script

#!/usr/bin/perl use POSIX; while(1) { sleep(1); print "I am running\n"; my $pid = fork; print "Process group Id:",getpgrp $pid,"\n"; unless($pid) { while(1) { sleep(2); print "Process group Id in child:",getpgrp getpid(),"\n"; print "I am child and I'am running\n"; } } }

In the above code out.txt contains only the string that is printed by its child but it is not printing the strings that was printed by child of its child.

refer out.txt

I am running
Process group Id:15697
I am running
Process group Id:15697
I am running
Process group Id:15697
I am running

Here I want to print the characters "I am child and I'am running". How to solve this issue?. Thanks in advance

Replies are listed 'Best First'.
Re: Redirect the process group stdout to parent
by almut (Canon) on May 27, 2010 at 13:14 UTC

    Looks like you're Suffering from Buffering.  When I add $|=1; to your script, I get all the output (including from the grandchildren) in out.txt:

    I am running Process group Id:12773 Process group Id:12773 I am running Process group Id:12773 Process group Id:12773 I am running Process group Id:12773 Process group Id:12773 Process group Id in child:12773 I am child and I'am running I am running Process group Id in child:12773 I am child and I'am running Process group Id:12773 Process group Id:12773 Process group Id in child:12773 I am child and I'am running Process group Id in child:12773 I am child and I'am running I am running Process group Id:12773 Process group Id:12773 Process group Id in child:12773 I am child and I'am running Process group Id in child:12773 I am child and I'am running I am running Process group Id:12773 Process group Id:12773 Process group Id in child:12773 I am child and I'am running Process group Id in child:12773 I am child and I'am running Process group Id in child:12773 I am child and I'am running I am running Process group Id:12773 Process group Id:12773
      Ya it is working now. Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://841910]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-01-19 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (55 votes). Check out past polls.

    Notices?