Skip to content

Instantly share code, notes, and snippets.

@avdi
Created June 29, 2009 17:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save avdi/137705 to your computer and use it in GitHub Desktop.
Save avdi/137705 to your computer and use it in GitHub Desktop.
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