B27
B27

Reputation: 11

fork with Ruby 1.8 and Windows

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)

Upvotes: 1

Views: 3097

Answers (3)

mkmurray
mkmurray

Reputation: 2444

I think you just need Process.exit! before you end your fork.

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

Upvotes: 0

DeTeam
DeTeam

Reputation: 21

Like that:

if child = fork
  puts 'parent'
  Process.wait
  puts 'done'
else
  puts 'child'
end

Upvotes: 2

user1602568
user1602568

Reputation: 11

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.

Upvotes: 0

Related Questions