Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:11:45+00:00 2026-06-14T19:11:45+00:00

I have a perl script which sends a lot of output to multiple subprocesses.

  • 0

I have a perl script which sends a lot of output to multiple subprocesses. I need to be able to close my end of all the pipes and then wait for the subprocesses to finish their work. So far I’ve only succeeded at closing each pipe and waiting for each subprocess to finish one by one.
More concretely, I’m doing something like this:

for ($i=0;$i<24;$i++) {
    my $fh;
    open $fh, "|externalprogram $i";
    $fhs{$i}=$fh;
}

#...now I can write output to the pipes
while (moreworktodo()) {
    $whichone, $data = do_some_work();
    print $fhs{$whichone} $data;
}
#Now I just need to wait for all the subprocesses to finish.  However, they
#need to do a lot of work that can only begin when they've finished reading input.  So I need to close my end of the pipe to indicate I'm finished.
for ($i=0;$i<24;$i++) {
    my $file = $fhs{$i};
    close $file;  #unfortunately, this blocks until process $i finishes
    #meanwhile all the other processes are waiting for EOF 
    #on their STDIN before they can proceed.  So I end up waiting
    #for 24 processes to finish one-at-a-time instead of all at once
}

One way to get all the subprocesses to finish promptly (closing their stdin) is simply to let my script exit without closing the (pipe) filehandles at all, but that’s no good because the script is part of a larger job that needs the subprocess’ work to actually be done before proceeding.

What is a simple way to close each subprocesses’ stdin (so that they can all finish working) and then wait for all of them to finish before proceeding? I’ve tried forking off a child to close each pipe but that doesn’t seem to work — only the parent’s “close” actually closes the stdin of the subprocess and waits for the subprocess to finish.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:11:45+00:00Added an answer on June 14, 2026 at 7:11 pm

    I would create the pipes myself and not use open(P, "|external-program").
    Then you can close the pipe and not wait for the child process to exit.

    Example of opening a pipe to a child process yourself:

    sub spawn {
      my ($cmd) = @_;
    
      pipe(my $rp, $wp) or die "pipe failed: $!";
    
      my $pid = fork();
      die "fork: $!" unless defined($pid);
      if ($pid) {
        # parent
        close($rp);
        return ($wp, $pid);
      } else {
        # child
        close($wp);
        open(STDIN, "<&", $rp);
        exec($cmd) or die "exec: $!";
      }
    }
    
    sub main {
      $| = 1;
      my ($wp, $pid) = spawn("./child");
      for (1..10) {
        print {$wp} "sending $_\n";
      }
      close($wp);
      print "done\n";
     }
    
     main();
    

    Here’s a sample child program to test that close() is NOT waiting for the child to exit:

    # file: ./child
    while (<STDIN>) {
      print "got: $_";
      sleep(2);
    }
    

    The last piece of the puzzle is to asynchronously wait for the child processes to exit.
    This can be done with a $SIG{CHLD} handler, or, alternatively, here is a simplistic “join_children” function:

    my @child_ids = (1..24); # or whatever ids you want to use
    my %pipe;                # hash map from child_id -> pipe handle
    
    sub join_children {
      for my $id (@child_ids) {
        close( $pipe{$id} );
      }
    
      my $count = scalar(@child_ids);
      while ($count > 0) {
        wait;
        $count--;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is: I have a perl script which uses lot of memory (expected
I have a Perl script which maps two drives, and then proceeds to copy
I have a Perl script which nests foreach loops as seen below. It takes
I have a Perl script which will allow for a file to be uploaded
I have a Perl script which calls another script. The Perl script should be
I have a Perl script which I want to run every 4 hours through
A little background: I have a perl script which is performing a number of
gurus. I have a perl script which needs to run in an infinite loop
I have a perl script URL which gives me a ZIP file, it processes
I have a simple Perl script which runs as a Linux daemon using an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.