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

The Waiting Game

The Waiting Game

A short presentation about the basics of working with processes in Ruby.

Tim Uruski

June 12, 2014
Tweet

More Decks by Tim Uruski

Other Decks in Programming

Transcript

  1. ...

  2. The Waiting Game

  3. Tim Uruski PetroFeed tim@petrofeed.com

  4. • Starting processes basics • Talking to processes • Starting

    process with libraries
  5. Built-in Ways to start a process

  6. backticks

  7. `whoami`

  8. %x(tail #{file})

  9. files = `grep -l puts *.rb`

  10. files = `grep -l puts *.rb` output command argv

  11. • blocks until process ends • captures and returns STDOUT

    • shared environment • shared STDIN and STDERR
  12. $ ruby -e 'puts `cat`.upcase' > hello^D HELLO

  13. system

  14. system("chmod +x #{file}")

  15. passed = system('rake spec')

  16. passed = system('rake spec') status command argv

  17. • blocks until process ends • returns success; true/false •

    shared STDIN, STDOUT, STDERR • shared environment
  18. $ ruby -e "p system('grep foo') > bar^D false

  19. fork

  20. if fork puts 'parent' else puts 'child' end

  21. • returns "twice" • parent gets a PID • child

    gets nil
  22. fork do puts 'child' end puts 'parent'

  23. • child executes block • parent jumps to end of

    block
  24. • complete copy of a process • preload an environment

    • memory management
  25. exec

  26. exec('ruby script/rails')

  27. • no turning back* • replaces the process • often

    combined with fork
  28. • checks for command first • raises Errno::ENOENT

  29. $ ruby -e "exec('date')" Tue 10 Jun 2014 18:15:44 MDT

    ! $ ruby -e "exec('dte')" -e:1:in `exec': No such file or directory - dte (Errno::ENOENT) ! $ ruby -e "exec('dte') rescue nil or puts 'oops'" oops
  30. • shared STDIN, STDOUT, STDERR

  31. $ ruby -e "STDOUT.puts STDIN.gets;\ > STDOUT.flush;\ > exec('cat')" >

    names alice bob ^D $ tail names alice bob
  32. • shared environment

  33. $ ruby -e "ENV['FOO'] = 'bar';\ > exec('echo \$FOO')" bar

  34. spawn

  35. spawn('rails server')

  36. Process.spawn('rails server')

  37. pid = spawn('rails server') command argv

  38. pid = spawn('rails' 'server') command argv

  39. $ ruby -e "spawn('wc'); Process.wait" hello world yes, this is

    dog ^D 2 6 29
  40. $ ruby -e "spawn('bc'); Process.wait" 1 + 1 2 x

    = 42 y = 3.14159 x * y 131.94678
  41. input_rd, input_wr = IO.pipe output_rd, output_wr = IO.pipe ! spawn('ruby',

    in: input_rd, out: output_wr) ! input_wr.puts "puts 'hello world'" input_wr.close ! output_wr.close puts output_rd.read #=> 'hello world'
  42. input_rd, input_wr = IO.pipe output_rd, output_wr = IO.pipe ! spawn('ruby',

    in: input_rd, out: output_wr) ! input_wr.puts "puts 'hello world'" input_wr.close ! output_wr.close puts output_rd.read #=> 'hello world'
  43. input_rd, input_wr = IO.pipe output_rd, output_wr = IO.pipe ! spawn('ruby',

    in: input_rd, out: output_wr) ! input_wr.puts "puts 'hello world'" input_wr.close ! output_wr.close puts output_rd.read #=> 'hello world'
  44. input_rd, input_wr = IO.pipe output_rd, output_wr = IO.pipe ! spawn('ruby',

    in: input_rd, out: output_wr) ! input_wr.puts "puts 'hello world'" input_wr.close ! output_wr.close puts output_rd.read #=> 'hello world'
  45. input_rd, input_wr = IO.pipe output_rd, output_wr = IO.pipe ! spawn('ruby',

    in: input_rd, out: output_wr) ! input_wr.puts "puts 'hello world'" input_wr.close ! output_wr.close puts output_rd.read #=> 'hello world'
  46. input_rd, input_wr = IO.pipe output_rd, output_wr = IO.pipe ! spawn('ruby',

    in: input_rd, out: output_wr) ! input_wr.puts "puts 'hello world'" input_wr.close ! output_wr.close puts output_rd.read #=> 'hello world'
  47. input_rd, input_wr = IO.pipe output_rd, output_wr = IO.pipe ! spawn('ruby',

    in: input_rd, out: output_wr) ! input_wr.puts "puts 'hello world'" input_wr.close ! output_wr.close puts output_rd.read #=> 'hello world'
  48. Command write read write read Pipe A Pipe B in

    out
  49. # plumbing

  50. IO.popen

  51. IO.popen('ruby', 'w+') do |io| io.puts "puts 'hello world'" io.close_write puts

    io.read end
  52. io = IO.popen('ruby', 'w+') ! io.puts "puts 'hello world'" io.close_write

    puts io.read
  53. Command write read write read Pipe A Pipe B IO.popen

  54. Talking to Processes

  55. wait

  56. Waits for a process to finish. Blocks your program.

  57. Process.wait(123) Process.waitpid(123) #=> returns status

  58. Process.wait2(123) Process.waitpid2(123) #=> returns [pid, status]

  59. Process.waitall #=> returns list of pids

  60. detach

  61. Process.detatch(123)

  62. kill

  63. Sends a signal

  64. Process.kill('INT', 123) Process.kill('SIGINT', 234) Process.kill(:TERM, 345) Process.kill(9, 456)

  65. $ man signal

  66. 1 SIGHUP terminate process terminal line hangup 2 SIGINT terminate

    process interrupt program 3 SIGQUIT create core image quit program 4 SIGILL create core image illegal instruction 5 SIGTRAP create core image trace trap 6 SIGABRT create core image abort program (formerly SIGIOT) 7 SIGEMT create core image emulate instruction executed 8 SIGFPE create core image floating-point exception 9 SIGKILL terminate process kill program 10 SIGBUS create core image bus error 11 SIGSEGV create core image segmentation violation 12 SIGSYS create core image non-existent system call invoked 13 SIGPIPE terminate process write on a pipe with no reader 14 SIGALRM terminate process real-time timer expired 15 SIGTERM terminate process software termination signal 16 SIGURG discard signal urgent condition present on socket 17 SIGSTOP stop process stop (cannot be caught or ignored) 18 SIGTSTP stop process stop signal generated from keyboard 19 SIGCONT discard signal continue after stop 20 SIGCHLD discard signal child status has changed 21 SIGTTIN stop process background read attempted from control terminal 22 SIGTTOU stop process background write attempted to control terminal 23 SIGIO discard signal I/O is possible on a descriptor (see fcntl(2)) 24 SIGXCPU terminate process cpu time limit exceeded (see setrlimit(2)) 25 SIGXFSZ terminate process file size limit exceeded (see setrlimit(2)) 26 SIGVTALRM terminate process virtual time alarm (see setitimer(2)) 27 SIGPROF terminate process profiling timer alarm (see setitimer(2)) 28 SIGWINCH discard signal Window size change 29 SIGINFO discard signal status request from keyboard 30 SIGUSR1 terminate process User defined signal 1 31 SIGUSR2 terminate process User defined signal 2
  67. $?

  68. system('false') and $? => #<Process::Status: pid 88459 exit 1> !

    system('true') and $? => #<Process::Status: pid 88467 exit 0>
  69. Extended Library ways to start a process

  70. Open3

  71. require 'open3' ! Open3.popen3('cat') do |stdin, stdout, stderr| stdin.puts 'hello'

    stdin.close puts stdout.read end
  72. PTY

  73. Pseudo terminals are used for...

  74. Shell

  75. require 'shell' ! Shell.def_system_command 'find', '/bin/find' ! sh = Shell.new

    files = sh.transact do find '.', '-name *.rb' end
  76. require 'shell' ! Shell.def_system_command 'find', '/bin/find' ! sh = Shell.new

    files = sh.transact do find '.', '-name *.rb' end
  77. require 'shell' ! Shell.def_system_command 'find', '/bin/find' ! sh = Shell.new

    files = sh.transact do find '.', '-name *.rb' end
  78. require 'shell' ! Shell.def_system_command 'find', '/bin/find' ! sh = Shell.new

    files = sh.transact do find '.', '-name *.rb' end
  79. Buggy, undocumented and not in active development. Too bad.

  80. # Process Basics - backticks - system - fork -

    exec - spawn - IO.popen
  81. # Process Communication - pids - stdio - signals

  82. # Extended Library - Open3 - PTY - Shell

  83. • IO blocking and buffering • process groups and sessions

    • working directories • permissions and umasks • process resource limiting • demonizing a process • pseudo-terminals
  84. Next Time Usage Examples Maybe

  85. Thank You