[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to timeout a system call

Jordi Planes

7/2/2007 12:13:00 PM


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

--
Posted via http://www.ruby-....

3 Answers

mrpink

7/2/2007 4:50:00 PM

0

why don't you use the kill syscall for the process?

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
>


--
greets

one must still have chaos in oneself to be able to
give birth to a dancing star

Jordi Planes

7/3/2007 11:26:00 AM

0

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


--
Posted via http://www.ruby-....

Jordi Planes

7/13/2007 3:41:00 PM

0


This is even nicer:
http://www.jpaisley.com/software/rub...

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


--
Posted via http://www.ruby-....