Skip to content

Instantly share code, notes, and snippets.

@gorsuch
Created November 2, 2014 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorsuch/6f11e07144cc6fb54c42 to your computer and use it in GitHub Desktop.
Save gorsuch/6f11e07144cc6fb54c42 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# build our pipe to be shared between processes
# remember that pipes are unidirectional
r,w = IO.pipe
if fork
# if we're here, we are the parent
# close our copy of the read end because we don't use it
r.close
# send some data to across the pipe
10.times do |n|
w.puts n
end
# important to close all write ends of a pipe,
# otherwise the read end will never EOF
w.close
# we wait for the child process to shutdown
Process.wait
else
# if we're here, we are the child
# important to close all write ends of a pipe,
# otherwise the read end will never EOF
w.close
# read off of the pipe until EOF
while data = r.gets
puts data
end
# close the reader
r.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment