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

···

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

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

···

On 3/31/2011 09:18, Ethan Huo 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?

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

···

On Thu, Mar 31, 2011 at 8:18 AM, Ethan Huo <firewall888@hotmail.com> 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?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

--
Posted via http://www.ruby-forum.com/\.

--
Tony Arcieri
Medioh! Kudelski

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

···

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

Jeremy Bopp 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:)

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On 31 March 2011 16:49, Ethan Huo <firewall888@hotmail.com> wrote:

Jeremy Bopp 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:)

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 Klauer

···

On Thu, Mar 31, 2011 at 09:49, Ethan Huo <firewall888@hotmail.com> wrote:

Jeremy Bopp 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:)

--
Posted via http://www.ruby-forum.com/\.

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

···

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>

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!

···

--
Posted via http://www.ruby-forum.com/\.