in reply to Re^2: child process termination on Windows
in thread child process termination on Windows

How 'bout this:
use strict; use warnings; use Win32::Process; use Win32::IPC 'wait_any'; use Win32; use threads; my @progs = ( 'C:\windows\system32\calc.exe', 'C:\windows\system32\notepad.exe', ); my @proclist; my %proctable; for (@progs) { my $ProcessObj; Win32::Process::Create($ProcessObj, $_, "", 0, NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport(); $proctable{$ProcessObj->GetProcessID}{progname} = $_; print "process created for $_\n"; push @proclist, $ProcessObj; }; $| = 1; sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); } sub wait_procs { while (@proclist) { my $x = wait_any(@proclist, INFINITE); if ($x) { my $proc = splice(@proclist, abs($x) - 1, 1); my $id = $proc->GetProcessID; my $program = $proctable{$id}{progname}; print "process finished x=$x id=$id program=$program\n"; } } print "thread exit\n"; } my $thr = threads->create('wait_procs'); print "waiting for input\n"; $_ = <>; print "input is $_\n"; $thr->join;

Replies are listed 'Best First'.
Re^4: child process termination on Windows
by morgon (Priest) on Nov 22, 2009 at 21:02 UTC
    Thanks a lot, I like this.

    I have two more question though:

    According to the the documentation wait any returns negative values when the object is "is an abandoned mutex" (whatever that is). As you are waiting on process-objects I think this can not happen - so do you really neeed the abs()?

    And strictly speaking wait_any does not wait for process termination but for the process to become "signalled" (so Windows does have some sort of signals :-). Are there any circumstances that a process can become "signalled" (i.e. wait_any returns) but does not terminate?

    Many thanks!

      I don't know the answers to either of your questions, sorry. I wondered the same things as I read the docs.