in reply to Re: Testing whether a process has finished
in thread Testing whether a process has finished

Those docs are just a bit confusing and I can see why rovf is concerned that waitpid would suspend his program.

First, the docs say that non-blocking wait is only available for "machines supporting either the waitpid(2) or wait4(2) system calls". Perhaps rovf's machine is one of the machines that doesn't?

Second, it isn't entirely clear how one would write a program to use a non-blocking wait, assuming there is one. The docs give an example of a while loop but the ellipsis in the example are placed above the loop, not within the loop. I'm guessing that one is expected to use a polling pattern, but if that were to be non-blocking, one would have to place one's application code inside the polling loop rather than above it. To avoid suspending your program I'm guessing the code should look something like this?

use POSIX ":sys_wait_h"; #... do { # ... do some stuff while waiting ... # check in to see if the process has ended ... $kid = waitpid(-1, WNOHANG); } while $kid > 0;

Best, beth

Replies are listed 'Best First'.
Re^3: Testing whether a process has finished
by ikegami (Patriarch) on Sep 10, 2009 at 16:03 UTC

    Perhaps rovf's machine is one of the machines that doesn't?

    He has Windows, and it works for Windows too.

    >perl -MPOSIX=:sys_wait_h -le"my $pid = system 1, $^X=>(-e=>'sleep 5') +; while (waitpid($pid, WNOHANG) == 0) { print 'waiting'; sleep 1; } p +rint 'done'" waiting waiting waiting waiting waiting waiting done

    To avoid suspending your program I'm guessing the code should look something like this?

    use POSIX qw( WNOHANG ); # Returns false if alive. # Returns true if dead. (Reaps and sets $?) # Throws expection on error. (Sets $!) sub is_child_dead { my ($pid) = @_; my $rv = waitpid($pid, WNOHANG); die("waitpid: $!\n") if $rv == -1; return $rv; }
      Thanks a lot! Now after seen your solution (which, of course, works), I also understand the docs for waitpid better....

      -- 
      Ronald Fischer <ynnor@mm.st>
Re^3: Testing whether a process has finished
by rovf (Priest) on Sep 11, 2009 at 09:41 UTC
    Perhaps rovf's machine is one of the machines that doesn't?
    It's Windows 2000, but the code is also supposed to run on Windows XP.
    Second, it isn't entirely clear how one would write a program to use a non-blocking wait, assuming there is one.
    Indeed I see from the perldocs only that waitpid accepts flags, but there is no description of what the possible value of these flags might be...

    -- 
    Ronald Fischer <ynnor@mm.st>

      but there is no description of what the possible value of these flags might be...

      From the docs (emphasis mine),

      If you say "$kid = waitpid(-1, WNOHANG);", then you can do a non-blocking wait