Reputation: 2812
Once the alarm(5)
or what ever seconds is started and running, after some time in the program if some actions happens I need to reset the alarm again to alarm(5)
. How can I achieve this?
Any alarm reset()
available?
int flag=0;
void alarmhand(int signal)
{
flag=1;
// printf("\n time out");
}
void main()
{
signal(SIGALRM,alarmhand);
alarm (5);
while(1)
{
event_status();
if(flag==1)
break;
}
}
void event_status()
{
int reset;
printf("reset 1=y/2=n :");
scanf("%d",&reset);
if(reset==1)
{
// alarm.reset(5);
// HOW DO I RESET THE ALARM AGAIN TO 5 HERE ??
}
}
Upvotes: 4
Views: 14319
Reputation: 1
Use sigaction()
system call and handle alarm signal in starting only.
Use ISR function to reset the alarm for every action when you want.
Upvotes: -1
Reputation: 881323
You can just set the new alarm and the old one will be cancelled:
alarm (5);
From the Linux alarm
man page, slightly paraphrased):
alarm()
arranges for aSIGALRM
signal to be delivered to the calling process in a specified number of seconds.If the specified number of seconds is zero, no new alarm is scheduled.
In any event any previously set alarm is canceled.
As noted, any current alarm is cancelled by a call to alarm()
before anything else. A new one won't be created if the argument to alarm()
is zero.
So alarm(0)
will cancel any currently active alarm, while alarm(5)
will either create a new alarm of, or reset a currently active alarm to, 5 seconds.
Upvotes: 10
Reputation: 753615
To cancel an outstanding alarm()
, use alarm(0)
. Its return value tells you how much time was unused from the last call to alarm()
. You can then freely set a new alarm, of course.
Upvotes: 4