Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How to force a timeout on a system call?

by maverick (Curate)
on Jun 21, 2001 at 23:56 UTC ( [id://90514]=note: print w/replies, xml ) Need Help??


in reply to How to force a timeout on a system call?

Try this.
eval { local $SIG{'ALRM'} = sub { die "timed out\n" }; alarm(10); system("sleep 30; echo 'I made it'") && die "system call faile +d\n"; alarm(0); }; if ($@) { if ($@ eq "timed out\n") { print "I timed out\n"; } else { print "something else went boom\n"; } } else { print "I didn't time out\n"; }
In ten seconds you should get output that reads "I timed out". This is almost exactly the eval timeout out example from the 'perlfunc' perldoc page. I modified it a little to have print statements for what happened where.

/\/\averick

Updated:

bluto is quite right. here is my updated code. This seems to work fine under Linux. The only problem I have with Dominus solution is requiring the external program (you should be able to get it to work in one piece of code IMHO ;) )

-- first.pl -- #!/usr/bin/perl use strict; my $pid; eval { local $SIG{'ALRM'} = sub { die "timed out\n" }; alarm(10); $pid = fork; if ($pid == 0) { exec("./second.pl"); } else { wait; } alarm(0); }; if ($@) { if ($@ eq "timed out\n") { kill(15,$pid); print "I timed out\n"; } else { print "something else went boom\n"; } } else { print "I didn't time out\n"; } -- second.pl -- #!/usr/bin/perl while(1) { print "alive\n"; sleep(1); }
This produces this output on my system.
darkstar:~>./first.pl alive alive alive alive alive alive alive alive alive alive I timed out darkstar:~>

Replies are listed 'Best First'.
Re: Re: How to force a timeout on a system call?
by Xxaxx (Monk) on Jun 23, 2001 at 14:06 UTC
    Much thanks for the update.
    Claude
Re^2: How to force a timeout on a system call?
by Freezer (Sexton) on Aug 17, 2012 at 17:40 UTC
    It doesn't do that on my system :
    I didn't time out I didn't time out

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://90514]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found