chaining processes, Process.waitpid

T

Thomas Hafner

Hello,

for chaining processes in a way, that the output of one process feeds
the input of another one like in that example ...

#\\
# transformation 'foo' -> 'fee' -> 'tee':
chain_processes(lambda { exec('echo', 'foo') },
lambda { exec('sed', 's|o|e|g') }) do
system('sed', 's|f|t|g')
end
#//

.... I came to that solution:

#\\
def chain_processes(*processes, &block)
saved_stdin = $stdin.clone
threads = []
this, *remaining = processes + (block ? [ block ] : [])
while [] != remaining
rd, wr = IO.pipe
child = fork
if !child
rd.close
$stdout.reopen(wr)
exit this[]
end
wr.close
$stdin.reopen(rd)
threads << Thread.new { Process.waitpid(child) }
this, *remaining = remaining
end

this[]
threads.each { |t| t.join }
$stdin.reopen(saved_stdin)
end
#//

That seems also to work without Process.waitpid(child). When is it
really necessary to wait for the children? How should an example look
like which breaks without using Process.waitpid or Process.wait?

Regards
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top