in reply to Killing a process on Windows (Win32::Process question)

The remarks from the underlying system call say

The TerminateProcess function is used to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.

TerminateProcess initiates termination and returns immediately. This stops execution of all threads within the process and requests cancellation of all pending I/O. The terminated process cannot exit until all pending I/O has been completed or canceled.

A process cannot prevent itself from being terminated.

  1. Killing a process (on any OS) is never 'safe'--for the process being killed--in that it may cause data loss.

    If you are creating the process you are killing, and have taken no extraordinary measures to raise its priviledges, or drop your own, there should be no circumstances in which the kill attempt will fail.

  2. Kill returns immediately to the caller. It may take a short while for the killed process to go away due to pending IO.
  3. Generally no.

    However, on later versions of the OS, it is possible for processes to be added to a Job Object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE which can cause other processes that are part of the same job object to be terminated automatically. But the creating process has to cooperate for that to happen.

One point to note. You are passing '9' to the kill call--presumably based upon the *nix kill 9, pid meme. The value passed to the Kill method does not control how the process is terminated. Ie. it is not a signal value. It is simply the exit code that is returned to anyone calling GetExitCode() for the process you've killed. As such, you should chose a value that makes sense in the environment you are running.

For example 9, translates to The storage control block address is invalid. which doesn't make a lot of sense in this circumstance.


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Killing a process on Windows (Win32::Process question)
by rovf (Priest) on May 13, 2009 at 15:25 UTC

    Thanks a lot for the elaborate and extremely helpful answer!

    For example 9, translates to The storage control block address is invalid.

    I didn't know that there are exit codes with predefined meaning. Do you happen to know where I find a list of these codes? In my case, the reason would be "Killing because of Timeout" (the process runs for too long time).

    -- 
    Ronald Fischer <ynnor@mm.st>

      16000 and counting.

      You can look through to find something very specific for your purposes--which would be a damn sight easier if they would put them all on one searchable page.

      A couple that might be suitable that I already encountered before are:

      ERROR_CONTROL_C_EXIT 572 (0x23C) ERROR_FATAL_APP_EXIT 713 (0x2C9)

      Generic enough that they aren't likely to be confused with any "expected values"--specific enough to suggest something abnormal happened.


      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.
      winerror.h
      The closest I can find is ERROR_TIMEOUT which has a value of 1460. Unfortunately the Windows implementation of Perl does not allow return codes > 255 from a process (because of Perl's UNIX history).
        Perl does not allow return codes > 255 from a process

        Perl screws up dealing with return codes internally, but it does allow perl processes to return values greater than 255:

        C:\test>perl -e"exit(511)" & echo %errorlevel% 511

        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.