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

E

Ethan Huo

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
 
Q

Quintus

Am 31.03.2011 17:15, schrieb Michal Suchanek:
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
 
E

Ethan Huo

Marvin G=C3=BClker 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!

-- =

Posted via http://www.ruby-forum.com/.=
 
E

Ethan Huo

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
 

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

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top