# Creating child processes in Windows?

**URL:** <https://www.ruby-forum.com/t/creating-child-processes-in-windows/84376>\
**Category:** Ruby\
**Created:** [February 3, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/creating-child-processes-in-windows/84376 "2007-02-03T19:44:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [February 3, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/creating-child-processes-in-windows/84376/1 "2007-02-03T19:44:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [February 3, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/creating-child-processes-in-windows/84376/2 "2007-02-03T20:04:08Z")

</div>

On Sun, 4 Feb 2007, Sonny C. wrote:

> 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 B., 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 H.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [February 3, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/creating-child-processes-in-windows/84376/3 "2007-02-03T22:13:16Z")

</div>

On Sun, 4 Feb 2007, Sonny C. wrote:

> 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.

gem install systemu

[http://codeforpeople.com/lib/ruby/systemu/](http://codeforpeople.com/lib/ruby/systemu/)  
[http://codeforpeople.com/lib/ruby/systemu/systemu-1.0.0/README](http://codeforpeople.com/lib/ruby/systemu/systemu-1.0.0/README)

-a

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [February 6, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/creating-child-processes-in-windows/84376/4 "2007-02-06T20:07:56Z")

</div>

Sonny C. wrote:

> 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.
