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 8049917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:55:19+00:00 2026-06-05T06:55:19+00:00

I have a script that spawns a set of children. The parent must wait

  • 0

I have a script that spawns a set of children. The parent must wait for each of the children to finish.

My script performs similar to the following perl script:

#! /usr/bin/perl
use strict;
use warnings;

print "I am the only process.\n";

my @children_pids;

for my $count (1..10){
        my $child_pid = fork();
        if ($child_pid) {  # If I have a child PID, then I must be the parent
                push @children_pids, $child_pid;
        }
        else { # I am the child
                my $wait_time = int(rand(30));
                sleep $wait_time;
                my $localtime = localtime;
                print "Child: Some child exited at $localtime\n";
                exit 0; # Exit the child
        }
}

foreach my $child (@children_pids) {
        print "Parent: Waiting on $child\n";
        waitpid($child, 0); 
        my $localtime = localtime;
        print "Parent: Child $child was reaped - $localtime.\n";
}

print "All done.\n";

Similar to the code I’ve provided above, each child may take a different time to finish.

The problem is when I try to reap the children by looping over the children PIDs, in that last foreach block, the parent waits for the children in the order that they are created.

Obviously the children do not finish in the order which they are spawned and so I’m left with a bunch of zombie processes for children that happen to finish early.

In my actual code, these children may finish days before one another and the number of zombie processes floating around can grow in the hundreds.

Is there a better way for me to reap a set of children?

  • 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-05T06:55:20+00:00Added an answer on June 5, 2026 at 6:55 am

    If your parent process doesn’t need to be aware of its children’s completion status then you can just set

    $SIG{CHLD} = 'IGNORE';
    

    which will automatically reap all children as they complete.

    If you do need to be informed of the children completing, then the signal handler needs to be set to reap all possible processes

    use POSIX ();
    
    $SIG{CHLD} = sub {
      while () {
        my $child = waitpid -1, POSIX::WNOHANG;
        last if $child <= 0;
        my $localtime = localtime;
        print "Parent: Child $child was reaped - $localtime.\n";
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that evokes a for loop with a set number of
the following script testssh.ksh proves that ssh have some problems when we try to
I have a web app that spawns off a script that runs a Nutch
A web crawler script that spawns at most 500 threads and each thread basically
I have a C application that spawns children using fork/exec. My spawn-process runs non-stop
I have a script that creates a random string and I want to turn
We have a script that saves some rows in a table and one of
I have a script that pulls data from a database, and based on the
I have a script that is supposed to read from the database and return
I have a script that I would like to convert to a module and

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.