Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Grow Your Unix Beard Using Ruby

jstorimer
November 02, 2012

Grow Your Unix Beard Using Ruby

This talk will be a safari tour through some unique features and internals of Unicorn. The Unicorn codebase is the best, most comprehensive example of Unix systems programming in Ruby that I've come across.

jstorimer

November 02, 2012
Tweet

More Decks by jstorimer

Other Decks in Programming

Transcript

  1. @hits = Array(1..5) pid = Process.fork do @hits.delete_at(1) puts "Child

    hits: #@hits" end Process.wait(pid) puts "Parent hits: #@hits"
  2. $hits = Array(1..5) pid = Process.fork do $hits.delete_at(1) puts "Child

    hits: #$hits" end Process.wait(pid) puts "Parent hits: #$hits"
  3. def start open_listener_socket load_rack_app 2.times do fork { worker_loop }

    end end def worker_loop loop do connection = listener_socket.accept process_client(connection) end end
  4. def reexec fork do cmd = [$0] cmd << ARGV.dup

    ENV['LISTENERS'] = sockets.map(&:fileno) exec(*cmd) # exec("unicorn", "-c", "unicorn_config.rb") end end
  5. def reexec fork do cmd = [$0] cmd << ARGV.dup

    ENV['LISTENERS'] = sockets.map(&:fileno) exec(*cmd) # exec("unicorn", "-c", "unicorn_config.rb") end end
  6. module Process def self.spawn(*args) # .. IO.pipe do |read, write|

    pid = Process.fork do read.close write.close_on_exec = true begin Rubinius::Spawn.exec(env, prog, argv, redirects, options) rescue => e write.write Marshal.dump(e) exit! 1 end end # .. end end end fork + exec
  7. module Process def self.spawn(*args) # .. IO.pipe do |read, write|

    pid = Process.fork do read.close write.close_on_exec = true begin Rubinius::Spawn.exec(env, prog, argv, redirects, options) rescue => e write.write Marshal.dump(e) exit! 1 end end # .. end end end fork + exec
  8. fork + exec $ curl google.com > google.html pid =

    fork do $stdout.reopen('google.html') exec('curl', 'google.com') end Process.wait(pid)
  9. fork + exec $ curl google.com > google.html pid =

    fork do $stdout.reopen('google.html') exec('curl', 'google.com') end Process.wait(pid)
  10. “[..] the Unix beard is really an extension of the

    philosopher's beard, and the academic's beard.”
  11. “Thirty years from now, there will still be a fork(2)

    and a pipe(2) and a exec(2) and smart people will still be using them to solve hard problems reliably and predictably, just like they were thirty years ago.” ~ Ryan Tomayko