Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Starting and stopping a child process in Windows

29 views
Skip to first unread message

Jon A. Lambert

unread,
Apr 27, 2005, 1:03:48 AM4/27/05
to
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

Jon A. Lambert

unread,
Apr 27, 2005, 1:53:21 AM4/27/05
to
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)

Berger, Daniel

unread,
Apr 27, 2005, 10:13:20 AM4/27/05
to
> -----Original Message-----
> From: Alexey Verkhovsky [mailto:al...@verk.info]
> Sent: Tuesday, April 26, 2005 8:46 PM
> To: ruby-talk ML
> Subject: 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.

require "win32/process"

Process.create or Process.fork
Process.kill

Regards,

Dan

nobu....@softhome.net

unread,
Apr 27, 2005, 10:46:31 AM4/27/05
to
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


Berger, Daniel

unread,
Apr 27, 2005, 10:53:52 AM4/27/05
to

Which of the many flavors of spawn are you using on Win32? And how does
it work on Unix?

Regards,

Dan

nobu....@softhome.net

unread,
Apr 27, 2005, 10:59:20 AM4/27/05
to
Hi,

At Wed, 27 Apr 2005 23:53:52 +0900,
Berger, Daniel wrote in [ruby-talk:140048]:


> > 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)>
>
> Which of the many flavors of spawn are you using on Win32? And how does
> it work on Unix?

Which? I just use built-in method, and it works on other
platforms almost too, except for DJGPP and a few.

--
Nobu Nakada


Berger, Daniel

unread,
Apr 27, 2005, 11:05:08 AM4/27/05
to

> -----Original Message-----
> From: nobu....@softhome.net [mailto:nobu....@softhome.net]
> Sent: Wednesday, April 27, 2005 8:59 AM
> To: ruby-talk ML
> Subject: Re: Starting and stopping a child process in Windows
>
>

_spawn, _spawnl, _spawnle, _spawnlpe, _spawnv, _spawnve, _spawnvp,
_spawnvpe are on Windows.

I don't even think I realized Unix had a spawn function (or I forgot).
Looks like it's just a combined fork + exec.

Regards,

Dan

nobu....@softhome.net

unread,
Apr 27, 2005, 11:27:56 AM4/27/05
to
Hi,

At Thu, 28 Apr 2005 00:05:08 +0900,
Berger, Daniel wrote in [ruby-talk:140051]:


> > Which? I just use built-in method, and it works on other
> > platforms almost too, except for DJGPP and a few.
>
> _spawn, _spawnl, _spawnle, _spawnlpe, _spawnv, _spawnve, _spawnvp,
> _spawnvpe are on Windows.

Ah, you're talking about the implementation? None of them.
See rb_w32_spawn() and CreateChild() functions in win32/win32.c
for detail.

> I don't even think I realized Unix had a spawn function (or I forgot).
> Looks like it's just a combined fork + exec.

Yes, it's implemented using those system calls. See
rb_spawn(), rb_fork() and rb_exec() functions in process.c.

--
Nobu Nakada


0 new messages