in reply to SIGCHLD and sleep()

If your main concern is the sleep(60) getting interuppted too soon, just put it in a loop, eg (untested)
my $start = time; while ( (my $diff = time - $start) < 60) { sleep(60-$diff) }

Dave.

Replies are listed 'Best First'.
Re^2: SIGCHLD and sleep()
by overbyte (Initiate) on Aug 09, 2005 at 18:44 UTC
    Thanks, I was making the process of maintaining the actual time slept versus how much time had elapsed during a particular iteration of the loop entirely too complicated. The simple loop does ensure the daemon sleeps for the correct amount of time.

    However, this does leave defunct processes sitting in the process table until the daemon interates again and I reap the processes. Which I can live with, but ideally I'd like to know if it is possible to avoid.

    Is there a way to create a thread to handle the signals immediately and store the exit statuses in a shared hash? I don't know if I should be mixing ithreads and forking to begin with, but that would be the perfect solution to my problems.

    Thanks again,
    overbyte

      You should be able to the reaping in a SIGCHLD handler, which upon exit, returns you to the sleep loop. Or include the reaping within the timing loop, eg
      while (1) { while (remaining) { sleep(remaining); while (($pid = wait) != -1) { ... do reaping stuff } } ... check for new jobs and spawn them ... }

      Dave.

        Thanks again for your help. This is actually what I ended up doing (reaping within the timing loop), and was just returning to post about. It seems to work well, without cluttering the process table with defunct processes. It would've only been ~1 min before they got reaped, but it's still ugly to see those zombies in the process table.

        overbyte