linux下简单的timer实现

linux下,定时器的实现方式有多种,以下的较为简单的一种。

/* add timer to print flow-rate */

struct itimerval tv;
tv.it_interval.tv_usec = 0;
tv.it_interval.tv_sec = 1;
tv.it_value.tv_usec = 0;
tv.it_value.tv_sec = 1;
if(setitimer(ITIMER_REAL, &tv, NULL) < 0)
{
 printf("set timer error..\n");
exit(0);
}
signal(SIGALRM, timer_func );

/* add timer to print flow-rate */



void timer_func(void)
{
  pthread_mutex_lock(&mutex_lock);
  float gbps = (float)completions * send_pkt_size * 8 / 1e9;


  printf("Transfered %d %d-byte packets: %f gbps\n",
         completions, send_pkt_size, gbps);
completions = 0;
  pthread_mutex_unlock(&mutex_lock);
}

你可能感兴趣的:(linux下简单的timer实现)