File Problem with pty and irb

Hi,

I have a C code that writes to a fifo file (a named pipe), and when I
just run it directly, it is fine:

Screen 1: ./mycode
          (running)

Screen 2: cat < namedpipe
          (a lot of outputs)

But when I try to run it via irb, it doesn’t give me the output:

Screen 1: irb -r cmd.rb
          irb(main):001:0>
          (the code is running in the background)

Screen 2: cat < namedpipe
          (nothing)

and cmd.rb is basically

require 'pty'
@rbOut, @rbIn, @pid = PTY.spawn('mycode')

It looks that the code is running, because I can give the command in
Screen 1 and the results are fine and indicate progress. Is there any
way to make mycode to output to namedpipe under irb? Thanks.

Best regards,

Bill

Hi Bill

You can use the backticks which just call sh -c

fifo = cat /tmp/fifo

or the c style popen which is the same:

fifo = IO.popen ‘cat /tmp/fifo’ do |p|
p.readlines
end
puts fifo

or use the File pointer syntax which removes the need to call the
shell and saves a kitten:

File.open ‘/tmp/fifo’, ‘r’ do |f|
f.readlines
end

You’ll also want to consider creating a trap to clean up the fifo if
an interupt signal is sent or when the program receives hup.

popen would also be appropriate for creating the mycode redirect to
pipe process and more importantly could be done without the named pipe
as well and done completely internally from within ruby’s IO and
Kernel methods. In fact ruby has IO.pipe which is probably what your
looking for.

You’re not waiting for the child process in cmd.rb so it’s killed when
the ruby process finishes. Try:

require 'pty'
@rbOut, @rbIn, @pid = PTY.spawn('mycode')
Process.waitpid(@pid)

Regards,
Sean

Stu wrote in post #1108165:

popen would also be appropriate for creating the mycode redirect to
pipe process and more importantly could be done without the named pipe
as well and done completely internally from within ruby’s IO and
Kernel methods. In fact ruby has IO.pipe which is probably what your
looking for.

Hi Stu,

Thanks for your suggestions. Actually I don’t want to use Ruby to deal
with the named pipe. In Screen 2, I just used cat to verify that the
named pipe is working, while in reality I have the real shell script
which uses gnuplot and some other C code to provide some processing. I
think by using Ruby to deal with named pipe, it will make the problem
more complicated.

It is just when I run mycode with pty and irb that the named pipe does
not work. (It uses the absolute path for the named pipe inside mycode.)

Best regards,

Bill

Instead of running it from irb, have you tried running it using the
ruby interpreter directly? E.g.

ruby cmd.rb

Sean O’halpin wrote in post #1108212:

You’re not waiting for the child process in cmd.rb so it’s killed when
the ruby process finishes. Try:

require 'pty'
@rbOut, @rbIn, @pid = PTY.spawn('mycode')
Process.waitpid(@pid)

Regards,
Sean

Hi Sean,

Thank your for your suggestion. Actually when I tried that, even I
could not put commands into irb anymore and the namedpipe still gave
nothing:

Screen 1: irb -r cmd.rb
          (no prompt from the irb)

Screen 2: cat < namedpipe
          (nothing)

mycode is a code that runs forever (a daemon, if you like).

Best regards,

Bill

Hi,

I also tried by invoking in irb

irb(main):001:0> `mycode`

and it turns out that the namedpipe produced some outputs. So this is
really a problem with TTY.spawn(‘mycode’).

Best regards,

Bill

Hi,

After further investigation, it turns out that “getlogin()” in mycode.c
is the one that does not work when it is spawned by PTY. It is working
now. Thanks for all of you who tried to help.

Best regards,

Bill

Sean O’halpin wrote in post #1108273:

Instead of running it from irb, have you tried running it using the
ruby interpreter directly? E.g.

ruby cmd.rb

Very good idea, Sean. I ran it again, with Process.waitpid(@pid) as you
indicated (because if not, Ruby and mycode immediately exit).

Yup, the namedpipe still does not produce anything, but now we know that
the problem is not irb. The problem is with Ruby, and more likely, the
PTY.

Best regards,

Bill