How to refine the code to avoid using fork on windows?

here is the thing, i need to move a previous ruby program from Linux to
Windows, but Win doesn’t support fork, how can i refine the code to make
it work on Win?
i googled a lot but still didn’t get it, can anyone help me here?i paste
the code below:

def exec_cmd(cmd, toen=false, tmout=3600)
return false if !$AveSubPID.nil?
ret = fork
if ret.nil? then
exec “#{cmd}”
end
$AveSubPID = ret
succ=true
if (toen) then
begin
timeout(tmout) {
Process.wait(ret)
$AveSubPID = nil
succ=$?.success?
$AveErrno = $?.exitstatus
}
rescue Timeout::Error => e
echo "(timeout) "
kill_sub_process(ret)
$AveSubPID = nil
succ = false
$AveErrno = $?.exitstatus
end
else
Process.wait(ret)
$AveSubPID = nil
succ=$?.success?
$AveErrno = $?.exitstatus
end
return succ
end

On 3/31/2011 09:18, Ethan H. wrote:

here is the thing, i need to move a previous ruby program from Linux to
Windows, but Win doesn’t support fork, how can i refine the code to make
it work on Win?
i googled a lot but still didn’t get it, can anyone help me here?

Have you considered using Cygwin (http://cygwin.com) and the ruby
package it provides? That will give you an emulated fork. While Cygwin
is not a panacea for porting Unix applications to Windows, it may work
for you.

-Jeremy

Jeremy B. wrote in post #990214:

Have you considered using Cygwin (http://cygwin.com) and the ruby
package it provides? That will give you an emulated fork. While Cygwin
is not a panacea for porting Unix applications to Windows, it may work
for you.

-Jeremy

Hi, Jeremy, i knew cywin works for it, but cygwin has a memory limit for
our usage, that’s why we won’t use cygwin any more.
what i need now it to move it into Mingw and get it work…
thanks for your quick reply, i appreciate it:)

On 31 March 2011 16:49, Ethan H. [email protected] wrote:

what i need now it to move it into Mingw and get it work…
thanks for your quick reply, i appreciate it:)

You can also use the system() function.

Thread.new { system “dd”, “if=/dev/zero”, “of=/dev/null” }
=> #<Thread:0x7f81cb7337b0 sleep>

system “killall”, “-USR1”, “dd”
317912807+0 records in
317912807+0 records out
162771357184 bytes (163 GB) copied, 139.204 s, 1.2 GB/s
=> true

HTH

Michal

It might help to forward this on to the RubyInstaller group, since Luis
Lavena is maintaining the Ruby installer for Windows, he might know how
to
handle it (or how to get DevKit to work with it):

http://groups.google.com/group/rubyinstaller

-Nick K.

Am 31.03.2011 17:15, schrieb Michal S.:

You can also use the system() function.

Thread.new { system “dd”, “if=/dev/zero”, “of=/dev/null” }
=> #<Thread:0x7f81cb7337b0 sleep>

A better way IMO is to use the #spawn method which calls the external
process and doesn’t wait for it to complete. It immediately returns the
PID of the spawned process. Since Ruby 1.9 this works even on Windows.

Vale,
Marvin

Check out the childprocess gem: GitHub - enkessler/childprocess: Cross-platform Ruby library for managing child processes.

i tried using win32-process to emulate #fork, although the code need no
changes, the behavior of win32-process’ fork is different from Linux
fork, finally i used #spawn, and it seems work well

Marvin Gülker wrote in post #990237:

A better way IMO is to use the #spawn method which calls the external
process and doesn’t wait for it to complete. It immediately returns the
PID of the spawned process. Since Ruby 1.9 this works even on Windows.

Vale,
Marvin

Hi, Marvin
is #spawn’s behavior just similar to fork+exec?
i am not very clear how to change the code to use #spawn instead, could
you please give me some sample code to show how to using #spawn instead
of fork+exec? thanks!