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