开发者

Program required for stopping the signal after certain repetitions

Can anybody provide help to stop the signal after certain repetions .Below is the sample code. I want that after 3 repetitions the signal should be stopped.But currently it is not stopping:

    #include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <errno.h>
#define INTERVAL 1
int g=0;
int howmany = 0;

void exit_func (int i)
{
    signal(SIGTERM,exit_func);
    printf("\nBye Bye!!!\n");
    exit(0);
}
void alarm_wakeup (int i)
{
   struct itimerval tout_val;

  g++;
printf("\n %d \n",g);
//   signal(SIGALRM,alarm_wakeup);

if(g==3)
{
printf("\n %d \n",g);
signal(SIGSTOP,exit_func);
printf("%s",strerror(errno));
} 
   howmany += INTERVAL;

   printf("\n%d sec up partner, Wakeup!!!\n",howmany);
   tout_val.it_interval.tv_sec = 0;
   tout_val.it_interval.tv_usec = 0;
   tout_val.it_val开发者_Python百科ue.tv_sec = INTERVAL; /* 10 seconds timer */
   tout_val.it_value.tv_usec = 0;

   setitimer(ITIMER_REAL, &tout_val,0);
}


int main ()
{
  struct itimerval tout_val;

  tout_val.it_interval.tv_sec = 0;
  tout_val.it_interval.tv_usec = 0;
  tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
  tout_val.it_value.tv_usec = 0;
  setitimer(ITIMER_REAL, &tout_val,0);

  signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
  signal(SIGINT,exit_func);

  while (1)
  {
//printf("Dd");
}
}


Do not raise SIGSTOP, call exit_func directly.

include the appropriate headers:

#include <stdlib.h>  //exit
#include <string.h>  //strerror

also, why do you try to set up a signal handler in your exit function? it doesn't make sense.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜