# How to timeout a system call

**URL:** <https://www.ruby-forum.com/t/how-to-timeout-a-system-call/108339>\
**Category:** Ruby\
**Created:** [July 2, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/how-to-timeout-a-system-call/108339 "2007-07-02T14:13:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jordi](https://www.ruby-forum.com/u/jordi)\
**Post date:** [July 2, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/how-to-timeout-a-system-call/108339/1 "2007-07-02T14:13:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jordi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jordi](https://www.ruby-forum.com/u/jordi)\
**Post date:** [July 3, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/how-to-timeout-a-system-call/108339/2 "2007-07-03T13:26:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jordi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jordi](https://www.ruby-forum.com/u/jordi)\
**Post date:** [July 13, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/how-to-timeout-a-system-call/108339/3 "2007-07-13T17:40:15Z")

</div>

This is even nicer:  
[http://www.jpaisley.com/software/ruby/open4.rb](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
