zoukankan      html  css  js  c++  java
  • 定时器与休眠

    #include<sys/time.h>
    int setitimer(int which,const struct itimerval *new_value,struct itimerval *old_value); //创建一个间隔式定时器(interval timer),在未来某个时间点到期,并于此后每隔一段时间到期一次,which的值如下
    ITIMER_REAL //创建以真是时间倒计时的定时器,到期会产生SIGALARM信号发送给进程
    ITIMER_VIRTUAL      //创建以进程虚拟时间(用户模式下的cpu时间)倒计时的定时器,到期会产生和发送信号SIGVTALRM
    ITIMER_PROF //创建一个profiling定时器,以进程时间(用户态和内核态cpu时间的总和)倒计时,到期会产生SIGPROF信号
    struct itimerval{
    struct timeval it_interval;//是否为周期性定时器,如果其两个字段值均为0,那么该定时器为一次性定时器,如果其中一个非0,那么每次到期后,定时器重置为在指定时隔后再次到期
    struct timeval it_value;//距离定时器到期的延迟时间
    };
    struct timeval{
    time_t tv_sec;
    suseconds_t tv_usec;//微秒
    };
    
    int getitimer(int which ,struct itimerval *curr_value); //了解定时器的当前状态以及距离下次到期的剩余时间
    
    #include<unistd.h>
    unsigned int alarm(unsigned int seconds);//创建一次性实时定时器,seconds表示定时器到期的秒数,到期时,向调用进程发送SIGALRM信号。调用alarm()会覆盖对定时器的前一个设置,调用alarm(0)可以屏蔽现有计时器
    
    unsigned int sleep(unsigned int seconds);   //暂停调用进程seconds秒
    #include<time.h>
    int nanosleep(const struct timespec *request,struct timespec *remain);//以更高分辨率来设定休眠间隔时间
    struct timespec{
    time_t tv_sec;
    long tv_nsec;//纳秒
    };
    
    //POSIX时钟,编译时需要使用-lrt选项进行编译,从而与libtr函数库链接
    #include<time.h>
    int clock_gettime(clockid_t clockid,struct timespec *tp); //针对clockid所指定的时钟返回时间,把返回的时间置于tp指针
    
    int clock_getres(clockid_t clockid,struct timespec *res);
    clockid的值:
    CLOCK_REALTIME      //可设定的系统级实时时钟
    CLOCK_WONOTONIC     //不可设定的恒定态时钟
    CLOCK_PROCESS_CPUTIME_ID    //每进程cpu时间的时钟
    CLOCK_THREAD_CPUTIME_ID     //每线程cpu时间的时钟
    
    int clock_settime(clockid_t clockid,const struct timespec *tp);//利用tp设置clockid指定的时钟
    
    int clock_getcpuclockid(pid_t pid,clockid_t *clockid);  //获取特定进程的时钟id,将pid进程的cpu时间时钟的标识符置于clockid指针所指向的缓冲区
    
    int pthread_getcpuclockid(pthread_t thread,clockid_t *clockid); //获取指定线程消耗的cpu时间
    
    int clock_nanosleep(clockid_t clockid,int flags,const struct timespec *request,struct timespec *remain);//用来暂停调用进程,知道一段指定时间间隔后,或者收到信号才恢复运行
    
    #include<signal.h>
    #include<time.h>
    int timer_create(clockid_t clockid,struct sigevent *evp,timer_t *timerid);//创建一个新定时器
    union sigval{
    int sival_int;
    void *sival_ptr;
    };
    struct sigevent{
    int sigev_notify;
    int sigev_signo;
    union sigval sigev_value;
    union{
        pid_t _tid;
        struct {
            void(*_function)(union sigval);
        void *_attribute;
        }_sigev_thread;
         }_sigev_un;
    };
    sigevent结构中sigev_notify的值:
    SIGEV_NONE  //不通知
    SIGEV_SIGNAL    //发送sigev_signo信号给进程
    SIGEV_THREAD    //调用sigev_notify_function作为新进程的启动函数
    SIGEV_THREAD_ID //发送sigev_signo信号给sigev_notify_thread_id所标识的线程
    
    int timer_settime(timer_t timerid,int flags,const struct itimerspec *value,struct itimerspec *old_value);//配备和解除定时器,value包含定时器的新设置old_value用于发怒i定时器的前一设置(不需要可设置为NULL)
    struct itimerspec{
    struct timespec it_interval;//非0代表是周期性定时器
    struct timespec it_value;//定时器首次到期时间
    };
    struct timespec{
    time_t tv_sec;
    long tv_nsec;
    };
    
    int timer_gettime(timer_t timerid,stuct itimerspec *curr_value);//返回有timerid指定POSIX定时器的间隔和剩余时间
    
    int timer_delete(timer_t timerid);//删除定时器,timerid是之前调用timer_create()时返回的句柄
    
    int timer_getoverrun(timer_t timerid);//返回参数timerid指定定时器的溢出值
    
    int timerfd_create(int clockid,int flags);
    //会返回一个文件描述符fd,clockid的值可为CLOCK_REALTIME或者CLOCK_MONOTONIC
    //flags的值可以为TFD_CLOEXEC(为新的文件描述符设置运行时关闭),TFD_NONBLOCK(为底层的打开文件描述设置O_NONBLOCK设置,随后的读操作将是非阻塞的)
    
    int timerfd_settime(int fd,int flags,const struct itimerspec *new_value,struct itimerspec *old_value);//配备(启动)或者解除(停止)由文件描述符fd指代的定时器
    
    #include<sys/timerfd.h>
    int timerfd_gettime(int fd,struct itimerspec *curr_value);//返回文件描述符fd所标识定时器的间隔和剩余时间
  • 相关阅读:
    利用SoapHeader验证web service调用的合法性
    没有文件扩展“.js”的脚本引擎解决办法
    C#实现仿msn提示消息在登录后只弹出一次的效果
    一个 NHibernate 的映射生成器
    NHibernate概览
    Spring.NET 中的 AOP
    jQuery UI Accordion 手风琴组件的使用
    台湾出了繁体版《ASP.NET 本质论》
    Spring.NET 中配置 ashx 的映射,以及其它各种映射的处理 的特殊情况
    Spring.Net 中对测试的支持
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730610.html
Copyright © 2011-2022 走看看