From: Antonio Moreno Date: 2006-11-22T00:13:51+09:00 Subject: Re: File descriptors Vidar Hokstad wrote: > > > Robert is half right and half wrong (on POSIX compatible platforms > anyway): > > readable_fd1 = IO.for_fd(1,"r") > STDERR.puts "FROM STDIN (fd 0): #{STDIN.read}" > STDERR.puts "FROM STDOUT (fd 1): #{readable_fd1.read}" > > You might argue that if someone provides a file on fd 1 it isn't really > stdout, but you can certainly open fd 1 (or any fd) both for reading or > writing if whatever is attached to them supports the mode you want - fd > 0, 1 and 2 aren't special in any way to the OS. > > You can test the above like this: > ruby in.rb 1 > The 1 descriptors specified. > > Vidar Thanks, Vidar! You solved half my problem. Now all I'd need to know is how to write on fd 1 of a process spawned from ruby with IO.popen. I tried the following without success: f = open("|-", "w+") if f == nil puts "This is Child" r_fd1 = IO.for_fd(1,"r+") STDERR.puts "FROM STDOUT (fd 1): #{r_fd1.read}" STDERR.puts "Hijo: #{STDIN.gets}" exit 69 else STDERR.puts "Running parent." f.puts "From parent\n" STDERR.puts "Got: #{f.gets}" STDIN.puts "To fd 1 of the child." pid = Process.wait STDERR.puts "Child terminated, pid = #{pid}, exit code = #{$? >> 8}" end If I commmented the lines with "readable_fd1", this piece of code would run. What I get is: fork.rb:4:in `for_fd': Invalid argument (Errno::EINVAL) from fork.rb:4 Running parent. fork.rb:10:in `write': Broken pipe (Errno::EPIPE) from fork.rb:10 Thanks again. -- Posted via http://www.ruby-forum.com/.