Slide 1

Slide 1 text

Grow Your Unix Beard With Ruby Jesse Storimer

Slide 2

Slide 2 text

@jstorimer

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Grow Your Unix Beard With Ruby

Slide 6

Slide 6 text

Unix Ruby

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

http://tomayko.com/writings/unicorn-is-unix

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

:o http://www.flickr.com/photos/69er/463302758/

Slide 11

Slide 11 text

http://whiteboardunicorns.com

Slide 12

Slide 12 text

Rack HTTP server for fast clients and Unix

Slide 13

Slide 13 text

Rack HTTP server for fast clients and Unix

Slide 14

Slide 14 text

Pre-forking

Slide 15

Slide 15 text

forking

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

fork() creates a new process

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

fork() returns twice

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

fork() returns twice

Slide 30

Slide 30 text

fork() returns twice once, but in two processes

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

fork() creates an exact copy of the calling process

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Pre-forking

Slide 41

Slide 41 text

Pre-forking

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

http://whiteboardunicorns.com

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

copy on write: share memory until it’s modified

Slide 49

Slide 49 text

CoW Friendly Rubies •MRI 2.0 •Ruby Enterprise Edition •MRI 1.9.3-p194-perf

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

replace an instance of itself without losing any connections

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

http://whiteboardunicorns.com

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

exec() transforms the calling process into a new process

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

http://unicorn.bogomips.org/

Slide 66

Slide 66 text

fork + exec

Slide 67

Slide 67 text

fork + exec Process.spawn('time cat') system('time cat') `time cat` IO.popen('time cat') Open3.popen3('time cat')

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

The original Unix beards

Slide 74

Slide 74 text

“[..] the Unix beard is really an extension of the philosopher's beard, and the academic's beard.”

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Thanks! Tweet @jstorimer Email [email protected] Books http://workingwithcode.com Use RUBYCONF to save $10

Slide 78

Slide 78 text

“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

Slide 79

Slide 79 text

Refs •http://www.colourlovers.com/palette/1938182/coconut_lips_RC •http://www.colourlovers.com/palette/1111659/Ninja_Rainbow •http://www.colourlovers.com/palette/1936237/Keynote_2 •http://whiteboardunicorns.com/ •http://www.flickr.com/photos/amyvdh/6003141750/sizes/o/in/photostream/ •http://www.twoideas.org/2009/07/unix-beards/ •http://www.flickr.com/photos/69er/463302758/