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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:53:29+00:00 2026-06-17T20:53:29+00:00

So I have a program where i spawn off some children who do some

  • 0

So I have a program where i spawn off some children who do some useful tasks. I then spawn off another child who needs to wait for the first children to stop before doing its work. The parent program then continues running and at the end waits for the last forked child to stop.

I’m getting an issue where the child who needs to wait on the others doesn’t.

use strict;
use warnings;
use diagnostics;

my $pid1;
my $child1 = fork();
if ($child1) {
    # parent
    #print "pid is $pid, parent $$\n";
    $pid1 = $child1;
} elsif ($child1 == 0) {
        # child1
        # do something
        sleep 20;
        print "Child1\n";

        exit 0;
} else {
        die "couldnt fork: $!\n";
}

my $pid2;
my $child2 = fork();
if ($child2) {
    # parent
    #print "pid is $pid, parent $$\n";
    $pid2 = $child2;
} elsif ($child2 == 0) {
        # child2
        # wait for child1 to finish
        my $tmp = waitpid($pid1, 0);

        # do something else
        print "Child2\n";

        exit 0;
} else {
        die "couldnt fork: $!\n";
}

# do more stuff

# wait for child2 to finish
my $tmp = waitpid($pid2, 0);

Is there an easy way to do this? Possibly without having to wrap the first child in the second?

  • 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-17T20:53:31+00:00Added an answer on June 17, 2026 at 8:53 pm

    The easy way to do this is with Forks::Super.

    use Forks::Super;
    my $child1 = fork();
    if ($child1 != 0) {
        # ... parent code ...
    } else {
        # ... child code ...
        exit;
    }
    
    my $child2 = fork { 
        depend_on => $child1, 
        on_busy => 'queue',
        sub => sub {
           # ... code to execute in 2nd child ...
        }
    };
    # ... more code to run in the parent ...
    # ... and at the end of the program:
    waitall;
    

    In Forks::Super, waitpid is still called in the parent (behind the scenes). But when the first child is finished, Forks::Super will know it is time to start launch the second child process in the background.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java program which uses Runtime.exec() to spawn a child process, but
I have a C# program that needs to spawn a .BAT command file during
I have a program which creates JButtons which are then added to a JPanel
I have a program that saves an image in a local directory and then
I'm running gnome and have a program that spawns off a large number of
I have written the following program for automating scp command in perl. #!/usr/bin/expect spawn
i have loop and in loop my program spawn new process and new process
In gEDA , we have a helper program that needs to create a subprocess
I have a program that spawns another process from within one of the loaded
I want to replace my Node.js program with another one. Do I have to

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.