avdi (owner)

Revisions

  • ab89d2 avdi Tue Jun 02 21:34:42 -0700 2009
gist: 122788 Download_button fork
public
Public Clone URL: git://gist.github.com/122788.git
ruby_subprocesses.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'rbconfig'
 
def hello(source, expect_input)
  puts "Hello from #{source}"
  if expect_input
    puts "Standard input contains: \"#{$stdin.read}\""
  else
    puts "No stdin, or stdin is same as parent's"
  end
  $stderr.puts "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)'`
  puts "output: #{output}"
  puts
 
  puts "2. Kernel.system"
  success = system(%Q<#{RUBY} -r#{THIS_FILE} -e 'hello("backticks", false)'>)
  puts "success: #{success}"
  puts
 
  puts "3. Kernel.fork"
  pid = fork do
    hello("fork()", false)
  end
  puts "pid: #{pid}"
  puts
 
  puts "4. Kernel.open with |"
  cmd = %Q<|#{RUBY} -r#{THIS_FILE} -e 'hello("open(|)", true)'>
  open(cmd, 'w+') do |subprocess|
    subprocess.write("hello from parent")
    subprocess.close_write
    puts "output: #{subprocess.read}"
    puts
  end
 
  puts "5. Kernel.open with |-"
  open("|-", "w+") do |subprocess|
    if subprocess.nil? # child
      hello("open(|-)", true)
      exit
    else # parent
      subprocess.write("hello from parent")
      subprocess.close_write
      puts "output: #{subprocess.read}"
      puts
    end
  end
 
  puts "6. IO.popen"
  cmd = %Q<#{RUBY} -r#{THIS_FILE} -e 'hello("popen", true)'>
  IO.popen(cmd, 'w+') do |subprocess|
    subprocess.write("hello from parent")
    subprocess.close_write
    puts "output: #{subprocess.read}"
    puts
  end
 
  puts "7. IO.popen with -"
  IO.popen("-", "w+") do |subprocess|
    if subprocess.nil? # child
      hello("popen(-)", true)
      exit
    else # parent
      subprocess.write("hello from parent")
      subprocess.close_write
      puts "output: #{subprocess.read}"
      puts
    end
  end
 
  puts "8. Open3.popen3"
  require 'open3'
  cmd = %Q<#{RUBY} -r#{THIS_FILE} -e 'hello("popen3", true)'>
  Open3.popen3(cmd) do |stdin, stdout, stderr|
    stdin.write("hello from parent")
    stdin.close
    puts "output: #{stdout.read}"
    puts "error output: #{stderr.read}"
    puts
  end
 
  puts "9. Shell"
  require 'shell'
  Shell.def_system_command 'ruby', RUBY
  shell = Shell.new
  args = ['-r', THIS_FILE, '-e', 'hello("shell", true)']
  shell.transact do
    output = ""
    output = echo("hello from parent") | ruby(*args)
    puts "output: #{output}"
    puts
  end
 
  puts "10. Open4"
  require 'rubygems'
  require 'open4'
  cmd = %Q<#{RUBY} -r#{THIS_FILE} -e 'hello("Open4", true)'>
  stdin = StringIO.new("Hello from parent")
  stdout = StringIO.new
  stderr = StringIO.new
  Open4.spawn(cmd,
    'stdin' => stdin,
    'stdout' => stdout,
    'stderr' => stderr)
  puts "output: #{stdout.string}"
  puts "error output: #{stderr.string}"
  puts
 
  puts "11. Rake FileUtils#ruby"
  require 'rubygems'
  require 'rake'
  include FileUtils
  args = ['-r', THIS_FILE, '-e', 'hello("rake", false)']
  ruby(*args)
  puts
 
  puts "12. Daemons"
  require 'rubygems'
  require 'daemons'
  daemon = Daemons.call do
    hello('daemons', false)
  end
  daemon.stop
  puts
 
end