On 19 Jan., 11:42, Robert Klemme <shortcut... / googlemail.com> wrote:
> On 18.01.2008 21:18, Stephen Lewis wrote:
>
> > On Fri, Jan 18, 2008 at 07:56:34PM +0900, Robert Klemme wrote:
> >> 2008/1/18, Deadolus <deado... / gmail.com>:
> >>> ...
> >>> I read about multithreading, IO.popen would be perfect, but how can I
> >>> kill iperf when I want to quit, before iperf ran out of time?
> >> ...
>
> >> About the killing I am not sure.  With a quick check I did not find
> >> a proper way togetthe PID of the child process with IO.popen.  If
> >> ...
>
> >It'sa little hard to find until you already know whereitis, but
> > IO#pid might come in handy here :)
>
> Thanks!  I use #popen too infrequently and when I useitI do not
> normally fiddle with the process directly.
>
> Kind regards
>
>         robert

I wasn't really happy about my solution, so I continued
experimenting.
I now have something, which I'm quite happy about, I'll post it here,
maybe some people have the same problems as I had.
I expanded the normal IO class:

#-------------------File
OwnIO.rb--------------------------------------
module OwnIO

  def killed? #nil if not killed, pid of IO if killed
    #I think we do not need this, please correct me, if I am wrong:
    #if(self.class != IO) then raise "Wrong type of Input variable, is
#{self.class} but should be IO!" end
    begin

      Process.waitpid(self.pid,Process::WNOHANG)
    rescue Errno::ECHILD #rescue if process is already terminated
      self.pid
    end
  end

  def kill #kill a IO (you still have to close it)
  Process.kill("KILL",self.pid)
  end

end

class IO #add my own IO module to the normal IO class
  include OwnIO
end

#---------EOF----------------------------------------------

And here is a short example script (how I'll implement it in my GUI
more or less):

#-------------------test.rb---------------------------------
require "OwnIO.rb"
STDOUT.sync = true
old = nil #old output
i = 0
io =  IO.popen("ping google.ch")
io.sync = true

t = Thread.new() {while !io.killed? do Thread.current["line"] =
io.readline end}#first doesn't output all lines (Thread creation too
slow?)
while !io.killed? do
  if t["line"]!=old then#only react to changes of the line
    old = t["line"]
    puts t["line"]#puts the current line
    if i==20 then ##kill the io after 20 lines have been received
      io.kill
    end
    i+=1;
  end
end
puts "Killed"
#----------------------------
EOF----------------------------------------

The two functions "killed?" and "kill" are self explanatory, and seem
to work nice.
But at least "killed?" could have some improvements...
So if anybody has some improvements/suggestions to the code: go ahead!

Regards, Deadolus