zoukankan      html  css  js  c++  java
  • 如何使用Unix提供的间隔时钟器?(tricker_demo)

    / * ticker_demo.c
    *演示了使用间隔计时器来生成reqular
    *信号,这些信号又被捕获并用于倒计时
    * /

    #include <stdio.h>
    #include <sys/time.h>
    #include <signal.h>

    int main()
    {
    void countdown(int);

    signal(SIGALRM,countdown);
    if(set_ticker(500)==-1)
    perror("set_ticker");
    else
    while(1) pause();
    return 0;
    }

    void countdown(int signum)
    {
    static int num=10;
    printf("%d..",num--);
    fflush(stdout);
    if(num<0){
    printf("DONE! ");
    exit(0);
    }
    }

    / * [来自set_ticker.c]
    * set_ticker(number_of_milliseconds)
    *安排间隔计时器定期发布SIGALRM
    *错误时返回-1,确定为0
    * arg(以毫秒为单位),转换为整秒和微秒
    * /

    int set_ticker( int n_msecs )
    {
    struct itimerval new_timeset;
    long n_sec, n_usecs;

    n_sec = n_msecs / 1000 ; /* int part */
    n_usecs = ( n_msecs % 1000 ) * 1000L ; /* remainder */

    new_timeset.it_interval.tv_sec = n_sec; /* set reload */
    new_timeset.it_interval.tv_usec = n_usecs; /* new ticker value */
    new_timeset.it_value.tv_sec = n_sec ; /* store this */
    new_timeset.it_value.tv_usec = n_usecs ; /* and this */

    return setitimer(ITIMER_REAL, &new_timeset, NULL);
    }

  • 相关阅读:
    mybatis 使用缓存策略
    mybatis 使用事务处理
    mybatis 使用接口绑定
    mybatis 配置文件全解
    mybatis mapper映射文件全解
    mybatis中使用log4j
    初次使用Mybatis
    Servlet 实现文件上传与下载
    log4j v2版本的配置和使用
    Servlet 转发请求与重定向,以及路径问题
  • 原文地址:https://www.cnblogs.com/shiheyuanfang/p/14530453.html
Copyright © 2011-2022 走看看