Starky has asked for the wisdom of the Perl Monks concerning the following question:
I would like to have a parent process spawn a child to perform some task every 5 seconds. I would also like to perform some cleanup every time a child exits.
Unfortunately it appears that sleep exits prematurely when a CHLD signal handler is triggered; for example,
results in#!/usr/bin/perl use strict; $|++; my $pid = fork(); if ($pid) { # the parent does not exit prematurely when the handler # is set to IGNORE $SIG{CHLD} = sub { print "$$: Reaping a child process in child + signal handler ...\n" }; print "$$: Parent sleeping 5 seconds at [".localtime()."] ...\ +n"; sleep 5; print "$$: That was refreshing. The time is now [".localtime( +)."].\n"; exit(0); } else { print "$$: \tChild sleeping for 2 seconds ...\n"; sleep 2; print "$$: \tChild exiting at [".localtime()."] ...\n"; exit(0); }
From the documentation for sleep, May be interrupted if the process receives a signal such as "SIGALRM".3978: Child sleeping for 2 seconds ... 3977: Parent sleeping 5 seconds at [Tue Jan 14 14:17:11 2003] ... 3978: Child exiting at [Tue Jan 14 14:17:13 2003] ... 3977: Reaping a child process in child signal handler ... 3977: That was refreshing. The time is now [Tue Jan 14 14:17:13 2003] +.
I can put the sleep in backticks, which seems to work albeit kludgily, but I am annoyed that putting it in backticks will result in a CHLD signal whenever the subprocess exits which means I have to worry about which child it was that exited when the child signal is caught.
My question to the monks is: How would those wiser in the ways of slumber do this?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Sleeping and reaping
by jwest (Friar) on Jan 14, 2003 at 22:35 UTC | |
Re: Sleeping and reaping
by Abigail-II (Bishop) on Jan 14, 2003 at 23:16 UTC | |
Re: Sleeping and reaping
by tall_man (Parson) on Jan 14, 2003 at 22:59 UTC |
Back to
Seekers of Perl Wisdom