2

I have a perl script with multiple child processes generated with fork. The parent handles SIGINT and child processes ignore SIGINT ($SIG{INT} = 'IGNORE').

$SIG{'INT'} = sub {
  print "Caught Ctrl+C by $$\n";
  # while($result != -1){
      # $result = wait();
  #}
    die "Stopping...";
}; 

a) If include the commented lines the script waits for the children to complete and then exits.

b) But if I comment the "wait" lines I assume it works like this - the script exits and the parent process is terminated - the children continue running in the background and display some message as expected before exiting.

I understand the a) case but how does the b) case work, please explain why doesn't this generate zombie processes ?

This question extends/is related to Wait for children to complete if parent intercepts SIGINT

Community
  • 1
  • 1
Natasha
  • 1,470
  • 5
  • 18
  • 24

1 Answers1

2

This has nothing specific to do with perl.

When a process exits, any children of the process are "reparented" to init -- their ppid is reset to 1, which is the pid of the init process that starts when the machine boots.

The init process, after it has initiialized the machine, goes into a loop waiting for (and reaping) children. So any child that is reparented to init will never leave a zombie -- init will immediately clean it up.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • So , it seems in my case the children become "orphan processes" (not zombies) and are adopted by init. If so , what would create a zombie ? (I tried NOT ignoring the signal by children but this still seems ok). .. I read some info but still can't clearly define when exactly I get a zombie vs orphan (in the context of intercepting SIGINT in parent and child) – Natasha Nov 15 '15 at 20:45
  • 3
    A "zombie" is a process that has exited, but its parent has not yet called `wait` to get its exit status. Whenever the parent calls `wait` to get the exit or termination status, the zombie will be cleaned up. – Chris Dodd Nov 15 '15 at 21:27