How to timeout a system call

J

Jordi Planes

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
 
J

Jordi Planes

Jordi said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top