Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > chaining processes, Process.waitpid

Reply
Thread Tools

chaining processes, Process.waitpid

 
 
Thomas Hafner
Guest
Posts: n/a
 
      04-13-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
daisy-chaining 3548 and 3560 switches halbert@bbn.com Cisco 1 04-11-2006 09:39 PM
Chaining WiFi Router To Single PC's Firewall Router? (PeteCresswell) Wireless Networking 4 11-25-2005 06:01 PM
Problem Daisy-Chaining 2950 Using Copper Giga Aharon Schkolnik Cisco 5 09-25-2005 08:37 AM
Is it possible to predetermine the order of HttpModule calls or implement filter chaining in ASP.Net Joe Bloggs ASP .Net 1 05-11-2005 02:59 PM
Re: A style question re "chaining" methods Matt Humphrey Java 1 07-10-2003 05:49 AM



Advertisments
 



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