in reply to child process termination on Windows

You can use threads. For example:
use strict; use warnings; use Win32::Process; use Win32; use threads; my $ProcessObj; Win32::Process::Create($ProcessObj, "C:\\windows\\system32\\calc.exe", "", 0, NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport(); print "process created\n"; $| = 1; sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); } sub wait_proc { $ProcessObj->Wait(INFINITE); print "process finished\n"; } my $thr = threads->create('wait_proc'); print "waiting for input\n"; $_ = <>; print "input is $_\n"; $thr->join;
Depending on what you're doing in the main thread, the child thread could notify it (e.g. via Windows message or socket).

Replies are listed 'Best First'.
Re^2: child process termination on Windows
by morgon (Priest) on Nov 19, 2009 at 10:05 UTC
    Thanks, that's a good idea, but íf I want to fork a number of childs I would need one thread per child for your approach which I think is costly.
      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;
        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!