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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

respond to INT during sleep 1

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
0
0
IL
Hi,

At the beginning of my script I set an interrupt handling:

$SIG{INT} = \&KILL_MON;

. . .
sleep 300;
. . .

sub KILL_MON
{
close LOG;
exit 1;
}

The problem is that if the cntrl-C is received during the long sleep,the sub is not activated till the end of 300 sec.
Is there any way to get an immediate respond to the INT even during sleep ?
Thanks

Long live king Moshiach !
 
sleep for smaller intervals to allow keystrokes to 'break in'??
Code:
for ($i=1;$i<10;$i++) {
   sleep 30;
}


Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
You might make it more efficient by making your own function call.

Code:
use subs "pause";

sub pause {
   my $seconds = shift;

   my $expire = time() + $seconds; # when to quit waiting
   while (time() < $expire) {
      select (undef,undef,undef,0.01); # sleep 1/10th of a sec
   }
}

pause 300;

That would make your script responsive to INT at virtually any millisecond. PaulTEG's method would make your script only be responsive to INT once every 30 seconds. This one loops rapidly until the wait time has come, and during the loop it sleeps a fraction of a second at a time.

(I should also note, you don't want to just have a while() loop with no sleeping inside of it... the loop will run so quickly it'll actually take up a lot of CPU, although theoretically it would make your script responsive to INT at even more frequent intervals of time, but at this point it's not even that important).

-------------
Cuvou.com | The NEW Kirsle.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top