How to timeout a system call

Hi,

I would like to stop a system call after a timeout, but doing so with
the Timeout library does not kill the process, and the process keeps
running. I think the problem is the system call does a Process.fork and
no way to kill it, no matter if the Thread is stoped.

Is there any way to kill the process? May be getting its pid?

Thanks,

Jordi

Jordi Planes wrote:

Hi,

I would like to stop a system call after a timeout, but doing so with
the Timeout library does not kill the process, and the process keeps
running. I think the problem is the system call does a Process.fork and
no way to kill it, no matter if the Thread is stoped.

Is there any way to kill the process? May be getting its pid?

Thanks,

Jordi

I finally come up with my own timeout, using Process instead of Thread.
In case somebody is interested, here it is:

module Timeout

class Error<Interrupt
end

def timeout( sec, exception=Error )
return yield if sec == nil or sec.zero?
pid_execution = Process.fork { begin yield ensure exit! end }
pid_sleep = Process.fork { begin sleep sec ensure exit! end }
if Process.wait == pid_sleep then
Process.kill( ‘INT’, pid_execution )
raise exception, “execution expired”
else
Process.kill( ‘ALRM’, pid_sleep )
end
end

module_function :system_call, :timeout

end

This is even nicer:
http://www.jpaisley.com/software/ruby/open4.rb

Good job Jonathan!

Jordi Planes wrote:

Jordi Planes wrote:

Hi,

I would like to stop a system call after a timeout, but doing so with
the Timeout library does not kill the process, and the process keeps
running. I think the problem is the system call does a Process.fork and
no way to kill it, no matter if the Thread is stoped.

Is there any way to kill the process? May be getting its pid?

Thanks,

Jordi

I finally come up with my own timeout, using Process instead of Thread.
In case somebody is interested, here it is:

module Timeout

class Error<Interrupt
end

def timeout( sec, exception=Error )
return yield if sec == nil or sec.zero?
pid_execution = Process.fork { begin yield ensure exit! end }
pid_sleep = Process.fork { begin sleep sec ensure exit! end }
if Process.wait == pid_sleep then
Process.kill( ‘INT’, pid_execution )
raise exception, “execution expired”
else
Process.kill( ‘ALRM’, pid_sleep )
end
end

module_function :system_call, :timeout

end