Starting and stopping a child process in Windows

What is the best / most reliable / most obvious way to start and kill a child process under Windows?
This child process happens to be a Webrick application, and I don't care about being platform-dependent in this case.

···

--
Best regards,

Alexey Verkhovsky

Ruby Forum: http://ruby-forum.org (moderator)
RForum: http://rforum.andreas-s.net (co-author)
Instiki: http://instiki.org (maintainer)

Alexey Verkhovsky wrote:

What is the best / most reliable / most obvious way to start and kill
a child process under Windows? This child process happens to be a Webrick application, and I don't
care about being platform-dependent in this case.

You might use CreateProcess from Win32api.
The PID is returned in the last parameter, the process_information structure.
Then use that PID in calling OpenProcess to get the Handle to use
when calling TerminateProcess.

···

--
J Lambert

Hi,

At Wed, 27 Apr 2005 11:46:11 +0900,
Alexey Verkhovsky wrote in [ruby-talk:139984]:

What is the best / most reliable / most obvious way to start and kill a child process under Windows?
This child process happens to be a Webrick application, and I don't care about being platform-dependent in this case.

With 1.9 feature:

  $ ruby -v -e 'pid = spawn("cmd.exe"); sleep 3; Process.kill("TERM", pid); puts; p Process.waitpid(pid); p $?'
  ruby 1.9.0 (2005-04-22) [i386-cygwin]
  Microsoft Windows 2000 [Version 5.00.2195]
  (C) Copyright 1985-2000 Microsoft Corp.

  c:\ruby>
  2140
  #<Process::Status: pid=2140,signaled(SIGTERM=15)>

···

--
Nobu Nakada

Jon A. Lambert wrote:

You might use CreateProcess from Win32api.
The PID is returned in the last parameter, the process_information
structure.
Then use that PID in calling OpenProcess to get the Handle to use
when calling TerminateProcess.

Here's a bit of code that partly works. That is it does get the process id.
You'd have to look up all the actual values in the windows headers
for the options you want.

require 'Win32API'

createProcess = Win32API.new("kernel32.dll","CreateProcess",'pplllllppp','L')
startupinfo = [ 68 ].pack("lx64")
procinfo = [ 0,0,0,0 ].pack("llll")
createProcess.call(nil,"write.exe", 0, 0, 1, 0, 0, "c:\\apps", startupinfo, procinfo)

processid = procinfo.unpack("llll")[2]

puts processid

# works up to here
openProcess = Win32API.new("kernel32.dll","OpenProcess",'lll','L')
handle = openProcess.call(0, 0, processid)

puts handle

terminateProcess = Win32API.new("kernel32.dll","TerminateProcess",'ll','L')
terminateProcess.call(handle, 0)

Jon A. Lambert wrote:

Jon A. Lambert wrote:

You might use CreateProcess from Win32api.
The PID is returned in the last parameter, the process_information
structure.
Then use that PID in calling OpenProcess to get the Handle to use
when calling TerminateProcess.

Jon, thank you very much. Your post pointed me at the right direction.

http://dev.instiki.org/file/instiki/trunk/test/watir/e2e.rb (E2EInstikiTest::startup and E2EInstikiTest::shutdown) contain a working implementation of starting / stopping Webrick process via Win32 API calls. Although it's somewhat ugly and needs proper error handling, but at least it works in a fair weather.

P.S. Ten lines of interesting stories to start / stop a process?... sigh...

···

--
Best regards,

Alexey Verkhovsky

Ruby Forum: http://ruby-forum.org (moderator)
RForum: http://rforum.andreas-s.net (co-author)
Instiki: http://instiki.org (maintainer)