zoukankan      html  css  js  c++  java
  • linux下的C语言开发(定时器)

        定时器是我们需要经常处理的一种资源。那Linux下面的定时器又是怎么一回事呢?其实,在linux里面有一种进程中信息传递的方法,那就是信号。这里的定时器就相当于系统每隔一段时间给进程发一个定时信号,我们所要做的就是定义一个信号处理函数。

    [cpp] view plain copy
     
    1. #include <stdio.h>  
    2. #include <time.h>  
    3. #include <sys/time.h>  
    4. #include <stdlib.h>  
    5. #include <signal.h>  
    6.   
    7. static int count = 0;  
    8. static struct itimerval oldtv;  
    9.   
    10. void set_timer()  
    11. {  
    12.     struct itimerval itv;  
    13.     itv.it_interval.tv_sec = 1;  
    14.     itv.it_interval.tv_usec = 0;  
    15.     itv.it_value.tv_sec = 1;  
    16.     itv.it_value.tv_usec = 0;  
    17.     setitimer(ITIMER_REAL, &itv, &oldtv);  
    18. }  
    19.   
    20. void signal_handler(int m)  
    21. {  
    22.     count ++;  
    23.     printf("%d ", count);  
    24. }  
    25.   
    26. int main()  
    27. {  
    28.     signal(SIGALRM, signal_handler);  
    29.     set_timer();  
    30.     while(count < 10000);  
    31.     exit(0);  
    32.     return 1;  
    33. }  
  • 相关阅读:
    Redis 简介
    图片懒加载、selenium和PhantomJS
    Python网络爬虫之三种数据解析方式
    Scrapy框架之CrawlSpider
    Scrapy 框架
    python 网络爬虫概念与HTTP(s)协议
    Mysql概念及基本操作
    Python re 模块
    线程与进程应用场景
    全局解释器锁 GIL
  • 原文地址:https://www.cnblogs.com/jiangzhaowei/p/7372373.html
Copyright © 2011-2022 走看看