Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

63 lines (44 sloc) 2.093 kb

ruby_is_forked

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 = File.open("/dev/null")
puts "Parent: #{$$}: #{file.object_id}"
fork { puts "Child: #{$$}: #{file.object_id}" }
puts "Parent after fork: #{$$}: #{file.object_id}"
Thread.new { 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 = File.open("/dev/null")
Process.add_callback_in_child! lambda { | pid | file = File.open("/dev/null") }
puts "Parent #{$$}: #{file.object_id}"
fork { puts "Child: #{$$}: #{file.object_id}" }
puts "Parent after fork: #{$$}: #{file.object_id}"
Thread.new { puts "Thread: #{file.object_id}" }

Sample Output:

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

Functionality

  • 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

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

Jump to Line
Something went wrong with that request. Please try again.