in reply to Re: break thread with system(...) call
in thread break thread with system(...) call

great! it seems to be the most progressive... I see, you understand the problem as for complex scripts and so cmd.exe ... but I think that it will be some kind of process tree for windows, I'm right?
perl->cmd.exe->prog.exe

maybe there is a way to follow the process tree or kill cmd.exe in such a way that all children will be also killed? Thaanks)
  • Comment on Re^2: break thread with system(...) call

Replies are listed 'Best First'.
Re^3: break thread with system(...) call
by BrowserUk (Patriarch) on Jul 16, 2008 at 20:39 UTC
    maybe there is a way to follow the process tree or kill cmd.exe in such a way that all children will be also killed?

    Yes. Use Win32::CreateProcess and the include CREATE_NEW_PROCESS_GROUP. Use it to start cmd.exe and then supply your complex command line.

    When you kill cmd.exe, any child processes it has created will be killed also.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Some further info on this:

      1. You must use perl's built in kill, with a signal number 21 (SIGBREAK).

        Win32::Process::Kill() only gets the immediate child, not the group.

      2. Only processes that share the same console will be killed.

        That means that if all the processes in your complex commands are simple command line apps, then all children and their grandchildren etc. will be killed as a group.

        But if any of the processes allocate their own console, or do not use a console (eg. notepad.exe), then they will survive. I do not have a simple solution for these.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: break thread with system(...) call
by BrowserUk (Patriarch) on Jul 17, 2008 at 14:13 UTC
      Thanks a lot! I've thought so much and determined to do like this (taskkill.exe) is windows native program (I've recently read about it.. yes, also at perlmonks.org)):
      my $pid = system (1, 'FOR /R C:\ %G IN (*.*) DO type %G'); sleep(2); print `taskkill.exe /PID $pid /T /F`;
      To detect the state of process, I use this:
      #return (kill 0,$pid); - don't know but it doesn't work.. my $throbj; if (Win32::Process::Open($throbj,$pid,0)) { my $ecode; $throbj->GetExitCode ($ecode); if ($ecode==Win32::Process::STILL_ACTIVE) { return 1; } #STILL ACTIVE return 0; }
      I'm a bit sad that I've read too late about Win32::Job and Proc-Background but my resulting solution seems to work well...

      Guys, thanks once again! ) you helped me very very.