Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reset alarm() in c linux

Tags:

c

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 ??
  }
}
like image 913
Naggappan Ramukannan Avatar asked Dec 15 '22 12:12

Naggappan Ramukannan


1 Answers

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 a SIGALRM 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.

like image 183
paxdiablo Avatar answered Jan 02 '23 05:01

paxdiablo