Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alarm Bell Whistle 1

Status
Not open for further replies.

sleuth

Programmer
Jan 12, 2001
134
0
0
US
I've got a piece of socket code that sometimes takes forever to run, way beyond my attention span.

I looked into timeouts, also the timeout function for sockets is bad and pretty much doesn't do anything and is a waste of bytes, but I found that Alarm was a way to timeout a scetion of code, the documentation is so fuzzy on this and don't give any cut & paste type examples that I usually have more success with. Does anyone know how to use the alarm(); function to make sure a section of code doesn't run for more than say 2 seconds?

Thanks

Tony "Java?? Umm, I think that's the stuff I drink while I'm coding perl."
 
i got this from perldoc -f alarm(slightly modified in the translation):
[tt]
eval {
local $SIG{ALRM} = sub {die "alarm\n" };
#so you'll recognize it later
alarm $timeout; #start timer
$nread = sysread SOCKET, $buffer, $size;
#or whatever code you want to put a timer around
alarm 0; #cancels timer
};
if ($@)
{
die unless $@ eq "alarm\n";
#this means the eval died of something other than alarm

#if you get here, that means it timed out
}
else
{
#if you get here, that means it worked and didn't time out
}[/tt]


"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 

Thanks Stillflame, that looks perfect,

Tony "Java?? Umm, I think that's the stuff I drink while I'm coding perl."
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top