views:

49

answers:

3

Is it real that the TerminateProcess function in Windows could hang because the threads inside the process were stuck in a deadlock?

Example: Process A is running under Process B's control, now Process A gets into a deadlock and Process B detects this and decides to 'Kill' process A using TerminateProcess.

Would it be successful in killing the hung Process A?

A: 

Yes. So long as you have the right permissions, TerminateProcess will kill the other process dead, regardless of how well hung it is.

JSBangs
+3  A: 

Yes, all kernel objects held by the process will be released, including locks.

The main problem with TerminateProcess is that the process has no say in the matter: if it's holding on to any global state (files, shared memory, etc) then you have no guarantee those things are in a consistent state after the process is terminated.

Dean Harding
So what could cause it to hang at the time of Killing it and it's mother process to hang as well?
Tony
If it has pending I/O, the kernel waits for pending I/O to complete before terminating the process. If the process is in kernel-mode then it will also wait until that has finished (both of those are quite rare and will usually indicate a bad driver or bad hardware)
Dean Harding
A: 

TerminateProcess will kill each thread (as if TerminateThread had been used on each and every thread in the process).

But it won't kill threads that are stuck in the kernel (e.g. due to device driver bug).

Richard