tags:

views:

63

answers:

1

I'm using ruby 1.8.7 patchlevel 302 and I'm working on a Windows xp system. I have to start an external process that needs to react on user input. The process doesn't react if I use threads, so I tried using fork. With fork the external process reacts on the user input but it executes more than just the fork block. For example

fork do
  puts 'child'
end
puts 'parent'
Process.wait
puts 'done'

produces the following output on my machine:

parent
child
parent
done
done

As you can see 'done' and 'parent' is printed twice. What can I do to make the child execute only its block and not more? (I can't switch to Ruby 1.9 because of some gems)

A: 

I found a little makeshift. It works like its supposed to be when I add Process.kill(1, 0) at the end of the child block. But I think it's not the best solution. So I'd still be happy if someone knows a real solution.

B27
@rogerdpack I'm using the win32-process gem that implements process methods that are not available in default ruby in Windows. I do not use cygwin. I must admit that I don't exactly know how fork works. I had a look at the sample code in a Ruby book and I'd hoped that the gem would work like the Linux Process.fork method.
B27
Makeshift, part II: Because the hole parent process is kind of copied, there are maybe parts at the beginning that shouldn't be run by the child. To find out if a child process is running, check the last argument (ARGV[ARGV.size-1] if ARGV.size > 0). If it is a child process, it is "child#x".
B27