Creating child processes in Windows?

S

Sonny Chee

Hey Guys,

Since fork is not implimented in Windows, how do you spawn subprocesses
in Windows? Does anyone have a good (Cross Platform) solution besides
Ruby Threads?

Sonny.
 
K

khaines

Since fork is not implimented in Windows, how do you spawn subprocesses
in Windows? Does anyone have a good (Cross Platform) solution besides
Ruby Threads?

Here's something that I use as part of my test support package for IOWA.
It's used primarily to provide a cross platform method for doing fork &
execs of processes for testing, but could, with a little TLC, be made more
general.

It depends on win32/process (By Daniel Berger, available on rubyforge or
via "gem install win32-process") to create processes on Windows.


module IWATestSupport
def self.create_process(args)
@fork_ok = true unless @fork_ok == false
pid = nil
begin
raise NotImplementedError unless @fork_ok
unless pid = fork
Dir.chdir args[:dir]
exec(*args[:cmd])
end
rescue NotImplementedError
@fork_ok = false
begin
require 'rubygems'
rescue Exception
end

begin
require 'win32/process'
rescue LoadError
raise "Please install win32-process."
end
cwd = Dir.pwd
Dir.chdir args[:dir]
pid = Process.create:)app_name => args[:cmd].join(' '))
Dir.chdir cwd
end
pid
end
end



Kirk Haines
 
J

Joel VanderWerf

Sonny said:
Hey Guys,

Since fork is not implimented in Windows, how do you spawn subprocesses
in Windows? Does anyone have a good (Cross Platform) solution besides
Ruby Threads?

It can be as simple as this:

Thread.new do
system "myprogram.exe arg arg"
end

This doesn't block your ruby process, and the thread lives until the
program finishes. If you want the program to start and keep running
(possibly outliving the ruby process), just use this:

system "start myprogram.exe"

or, to open a file in an application:

system "start msword-file.doc"

(Hope I'm remembering that right, I'm not on windows now to test.)

This is not cross platform, but you can wrap it in something to make it so.

There is also a kinda-sorta fork in one of the win32 utils.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top