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

  • SEARCH
  • Home
  • 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 4040164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:43:01+00:00 2026-05-20T12:43:01+00:00

I would like to maintain a pidlist array of children I have forked and

  • 0

I would like to maintain a pidlist array of children I have forked and then remove them as they exit (to restrict how many forked processes i have at any given time). I thought I might be clever and do this by using @main::pid_list in either a delete or splice, but no joy. I can successfully pop an element, but obviously it is not going to remove the correct pid. Any ideas how to handle this or would I be better doing this some entirely different way?

#!/usr/bin/perl -w
use POSIX ":sys_wait_h";
use Data::Dumper;

# Only allow 5 processes running at a time

sub REAPER {
    my $child = shift;
    while (($child = waitpid(-1, WNOHANG)) > 0) {
        # Need to remove child from pidlist here
        #pop(@main::pid_list);                     #This works
        #delete($main::pid_list[$child]);          #This does not

    }
    $SIG{CHLD} = \&REAPER;
}

@pid_list = ();
@files = (1 .. 20);

foreach my $file (@files) {
    my $processed = 'false';
    while ($processed eq 'false') {

        print "Working on file: $file\n";
        $SIG{CHLD} = \&REAPER;
        if (scalar(@pid_list) < 5) {
            $pid = fork();
            if ( $pid == 0 ) {
                print "$$: Child Processing file #" . $file . "\n";
                sleep(10);
                print "$$: Child done processing file #" . $file . "\n";
                exit(0);
            }
            push(@pid_list, $pid);
            print Dumper(@pid_list);
            $processed = 'true';
        }
        sleep(1);
    }
}

# Since we are at the end we need to wait for the last process to end
print "PID: $$ End of parent program\n";

exit 0;
  • 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-05-20T12:43:02+00:00Added an answer on May 20, 2026 at 12:43 pm

    Use a hash table instead of an array.

    sub REAPER {
        my $child = shift;
        while (($child = waitpid(-1, WNOHANG)) > 0) {
            # Need to remove child from pidlist here
            delete $main::pid_list{$child};
        }
        $SIG{CHLD} = \&REAPER;
    }
    
    ...
    
    if ((scalar keys %main::pid_list) < 5) {
        ...
        if ($pid != 0) {
            ...
           exit(0);
        }
        $main::pid_list{$pid}++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lot of different UserControls and would like to maintain consistent UI
Would like to maintain a map/hash of DOM objects. Can they serve as key
I have a coding project that I would like to maintain at GitHub. I
I would like to know how I can maintain the scroll position when I
I would like to create an auto increment function that allows me to maintain
I would like to know if it is possible to maintain an authentication (like
I would like to maintain interoperability between every other application on the planet (including
I would like to maintain a list of all of the strings entered into
I would like to maintain a list of objects that is distributed between N
I would like to maintain two forks of a project: one for internal use

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.