Created
November 2, 2014 15:56
-
-
Save gorsuch/6f11e07144cc6fb54c42 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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