win32-process quirks

I had occasion to install the win32-process gem today, because my Watir tests needed to start a background agent. On my laptop the cookbook example in the gem’s documentation doesn’t work; I found:

  • The package to require is ‘win32/process’;
  • the block version of fork() causes the child process to execute the block and then continue with the parent code;
  • the module names in the examples are incorrect (and not needed); and
  • the return value from Process.create() is not a process ID, but a structure that contains a process ID.

Nothing major, but it wasted twenty minutes. Anyway, for completeness here are a couple of corrected example scripts showing the behaviour of win32-process. First, mimicking the old Unix API:

require "win32/process"

pid = Process.fork
if pid.nil?
  3.times {
    puts "In the child"
    sleep 1
  }
  exit
end

2.times {
  puts "In the parent"
  sleep 1
}

Process.wait

As expected, this produces

In the parent
In the child
In the parent
In the child
In the child

Now try passing a block to the child:

require "win32/process"

Process.fork do
  3.times {
    puts "In the child"
    sleep 1
  }
  exit
end

2.times {
  puts "In the parent"
  sleep 1
}

Process.wait

On my PC this produces:

In the parent
In the child
In the parent
In the child
In the child
In the parent
In the parent

Those extra prints from the parent show that the child process continued after completing the block!

Advertisements

One thought on “win32-process quirks

  1. Yep, the examples on RF are old and need fixing. I’ll update those asap and see if I can remember what’s happening with Process.fork’s block method.

    Regards,

    Dan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s