Forked Ruby process inherit some resources from parent processes, on most platforms.

For example a File opened in a parent process is visible and shared in forked children:

file ="/dev/null")
puts "Parent: #{$$}: #{file.object_id}"
fork { puts "Child: #{$$}: #{file.object_id}" }
puts "Parent after fork: #{$$}: #{file.object_id}" { puts "Thread: #{file.object_id}" }

Sample Output:

Parent: 27282: 153210860
Child: 29777: 153210860
Parent after fork: 27282: 153210860
Thread: 153210860

Using ruby_is_forked:

require 'ruby_is_forked/process_callbacks'
file ="/dev/null")
Process.add_callback_in_child! lambda { | pid | file ="/dev/null") }
puts "Parent #{$$}: #{file.object_id}"
fork { puts "Child: #{$$}: #{file.object_id}" }
puts "Parent after fork: #{$$}: #{file.object_id}" { puts "Thread: #{file.object_id}" }

Sample Output:

Parent 31583: 90260550
Child: 31627: 152197900
Parent after fork: 31583: 90260550
Thread: 90260550


  • RubyIsForked::ForkCallback

** Callbacks in children (and parents) of Process.fork (and Kernel#fork).

  • RubyIsForked::Process

** Provides Process.current and Process.local variable abstraction.

See Also

Contributing to ruby_is_forked

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Kurt Stephens. See LICENSE.txt for further details.