Chinaunix首页 | 论坛 | 博客
  • 博客访问: 64939
  • 博文数量: 72
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-04 23:21
文章分类
文章存档

2015年(72)

我的朋友

分类: 嵌入式

2015-04-15 10:48:32

原文地址:timer 信号中断 作者:lingyunwmr

#include     // for printf()
#include    // for pause()
#include    // for signal()
#include    // for memset()
#include // struct itimeral. setitimer()
#include
void printMsg(int);
int main() {
  // Get system call result to determine successful or failed
  int res = 0;
  pthread_t a;
  // Register printMsg to SIGALRM
  signal(SIGALRM, pthread_create(&a,NULL,(void*)printMsg,NULL));
 
  struct itimerval tick;
  // Initialize struct
  memset(&tick, 0, sizeof(tick));
  // Timeout to run function first time
  tick.it_value.tv_sec = 1;  // sec
  tick.it_value.tv_usec =0 ; // micro sec.
  // Interval time to run function
  tick.it_interval.tv_sec = 1;
  tick.it_interval.tv_usec =0 ;
  // Set timer, ITIMER_REAL : real-time to decrease timer,
  //                          send SIGALRM when timeout
  res = setitimer(ITIMER_REAL, &tick, NULL);
  if (res) {
    printf("Set timer failed!!\n");
  }
  // Always sleep to catch SIGALRM signal
  while(1) {
     pause();
  }
  return 0 ; 
}
void printMsg(int num) {
  printf("%s","Hello World!!\n");
}
 
阅读(394) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~