From: "Berger, Daniel" Date: 2005-10-20T00:45:28+09:00 Subject: Re: Killing Threads & Processes on Windows > -----Original Message----- > From: Dave Burt [mailto:dave@burt.id.au] > Sent: Wednesday, October 19, 2005 8:07 AM > To: ruby-talk ML > Subject: Re: Killing Threads & Processes on Windows > > > I accidentally sent: > > nobu nakada wrote: > >> I know about TerminateProcess(), but think it is > considered critical > >> for general purpose, like as SIGKILL. > > > > You're probably right, it is severe, but rb_waitpid > shouldn't return > > with > > the process still running, should it? > > .... or just hang without closing the process? > > Would a call to TerminateProcess() go in wait() in win32/win32.c? > > Cheers, > Dave The win32/process package uses a CreateRemoteThread() + ExitProcess() approach (except for signal 9, which then uses TerminateProcess). This was recommended as a safer way to kill Windows processes (by Jeremy Richter, among others). Here's the C code (with a small bit elided out): // hProcess is a HANDLE acquired by OpenProcess() earlier if(hProcess){ hThread = CreateRemoteThread( hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)( GetProcAddress( GetModuleHandle("KERNEL32.DLL"), "ExitProcess" ) ), 0, 0, &dwThreadId ); if(hThread){ WaitForSingleObject(hThread, dwTimeout); CloseHandle(hProcess); ... } else{ CloseHandle(hProcess); // Raise an error } }