《linux下定时器的使用1.pdf》由会员分享,可在线阅读,更多相关《linux下定时器的使用1.pdf(2页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、通知方式为信号的处理方式:1.#include 2.#include 3.#include 4.#include 5.#include 6.#include 7.8.#define CLOCKID CLOCK_REALTIME9.10.voidvoid sig_handler(intint signo)11.12.printf(timer_signal function!%dn,signo);13.14.15.intint main()16.17./XXX int sigaction(int signum,const struct sigaction*act,struct sigaction*
2、oldact);18./signum-指定的信号编号,可以指定 SIGKILL 和 SIGSTOP 以外的所有信号编号19./act 结构体-设置信号编号为 signum 的处理方式20./oldact 结构体-保存上次的处理方式21./22./struct sigaction23./24./void(*sa_handler)(int);/信号响应函数地址25./void(*sa_sigaction)(int,siginfo_t*,void*);/但 sa_flags 为SASIGINFO 时才使用26./sigset_t sa_mask;/说明一个信号集在调用捕捉函数之前,会加入进程的屏蔽中
3、,当捕捉函数返回时,还原27./int sa_flags;28./void(*sa_restorer)(void);/未用29./;30./31.timer_t timerid;32.structstruct sigevent evp;33.34.structstruct sigaction act;35.memset(&act,0,sizeofsizeof(act);36.act.sa_handler=sig_handler;37.act.sa_flags=0;38.39./XXX int sigaddset(sigset_t*set,int signum);/将 signum 指定的信号加
4、入 set 信号集40./XXX int sigemptyset(sigset_t*set);/初始化信号集41.42.sigemptyset(&act.sa_mask);43.44.ifif(sigaction(SIGUSR1,&act,NULL)=-1)45.46.perror(fail to sigaction);47.exit(-1);48.49.50.memset(&evp,0,sizeofsizeof(structstruct sigevent);51.evp.sigev_signo=SIGUSR1;52.evp.sigev_notify=SIGEV_SIGNAL;53.ifif(
5、timer_create(CLOCK_REALTIME,&evp,&timerid)=-1)54.55.perror(fail to timer_create);56.exit(-1);57.58.59.structstruct itimerspec it;60.it.it_interval.tv_sec=2;61.it.it_interval.tv_nsec=0;62.it.it_value.tv_sec=1;63.it.it_value.tv_nsec=0;64.ifif(timer_settime(timerid,0,&it,0)=-1)65.66.perror(fail to timer_settime);67.exit(-1);68.69.70.pause();71.72.returnreturn 0;73.