Created
June 29, 2009 17:13
-
-
Save avdi/137705 to your computer and use it in GitHub Desktop.
Companion code for http://devver.net/blog/2009/06/a-dozen-or-so-ways-to-start-sub-processes-in-ruby-part-1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rbconfig' | |
$stdout.sync = true | |
def hello(source, expect_input) | |
puts "[child] Hello from #{source}" | |
if expect_input | |
puts "[child] Standard input contains: \"#{$stdin.readline.chomp}\"" | |
else | |
puts "[child] No stdin, or stdin is same as parent's" | |
end | |
$stderr.puts "[child] Hello, standard error" | |
end | |
THIS_FILE = File.expand_path(__FILE__) | |
RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']) | |
if $PROGRAM_NAME == __FILE__ | |
puts "1. Backtick operator" | |
output = `#{RUBY} -r#{THIS_FILE} -e'hello("backticks", false)'` | |
output.split("\n").each do |line| | |
puts "[parent] output: #{line}" | |
end | |
puts | |
puts "2. Kernel#system" | |
success = system(RUBY, "-r", THIS_FILE, "-e", 'hello("system()", false)') | |
puts "[parent] success: #{success}" | |
puts | |
puts "3. Kernel#fork" | |
pid = fork do | |
hello("fork()", false) | |
end | |
Process.wait(pid) | |
puts "[parent] pid: #{pid}" | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment