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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:56:17+00:00 2026-06-17T13:56:17+00:00

I have written two perl scripts (parent.pl and child.pl), and their source codes are

  • 0

I have written two perl scripts (parent.pl and child.pl), and their source codes are as follows:

parent.pl:

# file parent.pl

$SIG{CHLD} = sub {
    while(waitpid(-1, WNOHANG) > 0) {
        print "child process exit\n";
    }   
};

my $pid = fork();
if($pid == 0) {
    system("perl child.pl");
    exit;
}
while(1) {
    open my $fh, "date |";                                                                                                                                            
    while(<$fh>) {
        print "parent: ".$_;
    }   
    close $fh;
    sleep(2);
}

child.pl

#file child.pl

while(1) {
   open my $fh, "date |";
   while(<$fh>) {
       print "  child: ".$_;                                                                                                                                          
    }   
   close $fh;
   sleep(2);
}

What I want is the parent process and the forked sub-process output the current date alternately. But when I run perl parent.pl, the output is like this:

$ perl parent.pl 
parent: Mon Jan 21 14:53:36 CST 2013
  child: Mon Jan 21 14:53:36 CST 2013
  child: Mon Jan 21 14:53:38 CST 2013
  child: Mon Jan 21 14:53:40 CST 2013
  child: Mon Jan 21 14:53:42 CST 2013
  child: Mon Jan 21 14:53:44 CST 2013

It seems that the parent process was blocked when opening pipe.

But if I remove the following operation for signal CHLD.

$SIG{CHLD} = sub {
        while(waitpid(-1, WNOHANG) > 0) {
            print "child process exit\n";
        }   
};

And run it again. It seems OK.

$ perl parent.pl 
parent: Mon Jan 21 14:57:57 CST 2013
  child: Mon Jan 21 14:57:57 CST 2013
parent: Mon Jan 21 14:57:59 CST 2013
  child: Mon Jan 21 14:57:59 CST 2013
parent: Mon Jan 21 14:58:01 CST 2013
  child: Mon Jan 21 14:58:01 CST 2013

But I still feel puzzling. Why the parent process was blocked when I tried to open a pipe?

I don’t think removing the SIG{CHLD} function is a good idea, becaue zombie processes should be retrieved.

Anyone can help me? Thank you very much!

==================================================================

Thank @Borodin to help me solve my puzzle. And I have tried to modify the parent.pl like this:

my $main_pid = $$;
$SIG{USR1} = sub {
        #sleep(1);
        while(waitpid(-1, WNOHANG) > 0) {
                print "child process exit\n";
        }
};

my $pid = fork();
if($pid == 0) {
    $SIG{USR1} = 'IGNORE';
    system("perl child.pl");
    kill USR1, $main_pid;
    exit;
}
while(1) {
    open my $fh, "date |";
    while(<$fh>) {
        print "parent: ".$_;
    }
    close $fh;
    sleep(2);
}

Since CHLD signal may be kicked off by open or system, I used another customized signal USR1. And it works well now.

========================================================================

The above modification still has problems. The forked sub-process send USR1 singal before exit. May be the parent process should sleep for a while before waitpid, because the sub-process hasn’t exit yet.

I don’t retrieve sub-process manually now, and set $SIG{$CHLD} = 'IGNORE'. Hope the sub-process can be retrieved by operation system when it exits.

  • 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-17T13:56:18+00:00Added an answer on June 17, 2026 at 1:56 pm

    This is being made much more complex because both open my $fh, "date |" and system("perl child.pl") are starting child processes, as well as the explicit fork.

    So fork starts a child process, which does system("perl child.pl") to start its own child process, which in turn does open my $fh, "date |", which opens yet another child, which is now a great-grandchild of the main parent process.

    Meanwhile the main process does its own open my $fh, "date |" which starts another child process. In the end, the main process has two children, a grandchild and a great-grandchild.

    Unfortunately the children that are started using open or system have an implcit wait attached to them, so they will kick off the CHLD signal when they complete but when the handler is executed there is nothing left to wait for so it will hang as you have seen.

    perldoc perlipc has this to say

    Be careful: qx(), system(), and some modules for calling external commands do a fork(), then wait() for the result. Thus, your signal handler will be called. Because wait() was already called by system() or qx(), the wait() in the signal handler will see no more zombies and will therefore block.

    You can get things going by keeping to just a single parent and a single child process, like this.

    use strict;
    use warnings;
    
    use POSIX ':sys_wait_h';
    
    STDOUT->autoflush;
    
    $SIG{CHLD} = sub {
      while(waitpid(-1, WNOHANG) > 0) {
        print "child process exit\n";
      }   
    };
    
    my $pid = fork();
    
    if ($pid == 0) {
      while(1) {
        printf " child: %s\n", scalar localtime;
        sleep(2);
      }
    }
    else {
      while(1) {
        printf "parent: %s\n", scalar localtime;
        sleep(2);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written two scripts where one script calls subprocess.Popen to run a terminal
I have written two scripts Write.py and Read.py . Write.py opens friends.txt in append
I have written two short tests and compiled both with g++ -S (gcc version
I have written two pieces of code which I intended to have identical outputs,
I have written an app in C which expects two lines at input. First
I have written a module similar to the following: module One class Two def
I have written a Python module, and I have two versions: a pure Python
Hi I have a program written in C++ in which one or two functions
I have two window form applications written in C, one holds a struct consisting
I have two sites one is written in asp.net webforms and the other one

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.