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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:13:36+00:00 2026-06-03T23:13:36+00:00

I’m using proc::queue to manage a pile of child processes, and I fire them

  • 0

I’m using proc::queue to manage a pile of child processes, and I fire them off like this:

if(Proc::Queue::running_now() < $kids)
  {
    logger("Starting another server process...");
    my $newprocessid = Proc::Queue::run_back(\&serverprocess);
    logger("New child process id: $newprocessid");
    $childprocessids{$newprocessid} = 1;
  }

So now I have a hash of the child process IDs that I can kill when the parent process is sent a kill SIGTERM.

But what if the child process is killed externally? The parent process knows to fire up another child to make up for the lost one, and the new child PID ends up in my hash, but how can the parent find out the PID of the child that died to remove it from the hash so I don’t try and kill it later?

I can set $SIG{CHLD} but I don’t see how to get the child PID to remove it from my hash.

  • 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-03T23:13:37+00:00Added an answer on June 3, 2026 at 11:13 pm

    You might place the parent and its children in their own process group and kill the whole family by sending a signal to the parent.

    Depending on the nature of your problem, you may be willing to kill away (Mr. McManus!) and live with the failures due to each attempted kill on a child processes that is already dead.

    If the parent process does nothing but track the kids, a simple waitpid loop is all you need.

    while ((my $kid = waitpid -1, 0) > 0) {
      warn "$0: [$$] reaped $kid\n";
      delete $kid{$kid};
    }
    

    If you have other processing in the parent process, setting the WNOHANG bit in the second argument to waitpid tells the system call not to block. This allows you to periodically reap zombie children and then go back to other processing.

    For demonstration purposes, say we start up a bunch of sleepy children.

    #! /usr/bin/env perl
    
    use strict;
    use warnings;
    
    use 5.10.0;  # for defined-or
    
    my %kid;
    
    for (1 .. 5) {
      my $pid = fork // die "$0: fork: $!";  # / fix SO hilighting
      if ($pid == 0) {
        warn "$0: [$$] sleeping...\n";
        sleep 10_000;
        exit 0;
      }
      else {
        $kid{$pid} = 1;
        warn "$0: [$$] forked $pid\n";
      }
    }
    

    Then to simulate kills from outside, we fork another child to pick off the rest of its siblings at random.

    my $pid = fork // die "$0: fork: $!";
    if ($pid == 0) {
      warn "$0: [$$] The killer awoke before dawn.\n";
      while (keys %kid) {
        my $pid = (keys %kid)[rand keys %kid];
        warn "$0: [$$] killing $pid...\n";
        kill TERM => $pid or warn "$0: [$$] kill $pid: $!";
        delete $kid{$pid};
        sleep 1;
      }
      exit 0;
    }
    

    Now the loop from above reads the obituaries.

    while ((my $kid = waitpid -1, 0) > 0) {
      warn "$0: [$$] reaped $kid\n";
      delete $kid{$kid};
    }
    

    Double-check that no one made it out alive.

    if (keys %kid) {
      my $es = keys %kid == 1 ? "" : "es";
      die "$0: unkilled process$es:\n",
          map "  - $_\n", keys %kid;
    }
    

    Output:

    ./waitpid-demo: [1948] forked 7976
    ./waitpid-demo: [7976] sleeping...
    ./waitpid-demo: [1948] forked 7244
    ./waitpid-demo: [7244] sleeping...
    ./waitpid-demo: [1948] forked 4776
    ./waitpid-demo: [4776] sleeping...
    ./waitpid-demo: [1948] forked 4304
    ./waitpid-demo: [4304] sleeping...
    ./waitpid-demo: [1948] forked 7908
    ./waitpid-demo: [7908] sleeping...
    ./waitpid-demo: [5144] The killer awoke before dawn.
    ./waitpid-demo: [5144] killing 7908...
    ./waitpid-demo: [1948] reaped 7908
    ./waitpid-demo: [5144] killing 7976...
    ./waitpid-demo: [1948] reaped 7976
    ./waitpid-demo: [5144] killing 4776...
    ./waitpid-demo: [1948] reaped 4776
    ./waitpid-demo: [5144] killing 4304...
    ./waitpid-demo: [1948] reaped 4304
    ./waitpid-demo: [5144] killing 7244...
    ./waitpid-demo: [1948] reaped 7244
    ./waitpid-demo: [1948] reaped 5144
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.