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."

In reply to Re: Killing a process on Windows (Win32::Process question) by BrowserUk
in thread Killing a process on Windows (Win32::Process question) by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.