From: Caleb Clausen Date: 2010-06-04T04:03:25+09:00 Subject: Re: redirecting stdin for 3. party program On 6/3/10, Thomas Secret wrote: > OS: Debian Lenny > Ruby version: 1.8.7.72-3lenny1 > > ==Code== > > stdin_read,stdin_write=IO.pipe > pid=fork > if !pid > #$stdin.reopen("/home/admin/.bashrc") > stdin_write.close > $stdin.reopen(stdin_read) > > a=gets > puts "ruby child: got '#{a}'" > system("echo bash; mawk '{print \"bash child: got \"$0;exit}'") > exit! > end > > stdin_read.close > sleep 1 > stdin_write.puts "word1" > sleep 1 > stdin_write.puts "word2" > sleep 1 > puts "parent: before wait" > Process.wait(pid) > puts "parent: after wait" > > ==/Code== > > =Expected result:= > > ruby child: got 'ruby word1 > ' > bash > bash child: got 'ruby word2 > ' > parent: before wait > parent: after wait > > =Actual result:= > ruby child: got 'word1 > ' > bash > parent: before wait > > ->after the line 'parent: before wait' has been printed, the program > hangs. I've deliberately forgotten all the awk I ever knew, so I modified your program to use ruby where you had mawk, and it appears to work. I trust that I have correctly translated the intention of your original mawk script... Basically, I replaced the system line with: system("echo bash; ruby -e 'print \"bash child: got \"+readline;exit'") And I see this output: ruby child: got 'word1 ' bash bash child: got word2 parent: before wait parent: after wait With your original mawk line, I see the same behavior you do. I'd suspect a buffering problem, but inserting a flush after puts of word2 doesn't change anything. However, when I close the handle stdin_write after writing word2, it does work, even with mawk. Perhaps mawk is waiting for its stdin to close before it does anything?