setitimer函数
2016-02-05 10:50
501 查看
#include<stdio.h> #include<time.h> #include<sys/time.h> #include<unistd.h> #include<signal.h> #include<stdlib.h> static void ElsfTimer(int signo ) { struct timeval tp;
/* timerval 结构体 头文件 #include<sys/time.h> struct timeval { time_t tv_sec; /* 秒 */ suseconds_t tv_usec; /* 毫秒 */ }; */
10struct tm *tm;
/* 1 struct tm { int tm_sec; /* 秒–取值区间为[0,59] */ int tm_min; /* 分 - 取值区间为[0,59] */ int tm_hour; /* 时 - 取值区间为[0,23] */ int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ int tm_mon; /* 月份(从一月开始,0代表一月) - <span style="color:#cc0000;">取值区间为[0,11]</span> */ int tm_year; /* 年份,其值从1900开始 */ int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */ int tm_yday; /* 从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */ int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/ long int tm_gmtoff; /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/ const char *tm_zone; /*当前时区的名字(与环境变量TZ有关)*/ }; */
gettimeofday(&tp,NULL);
/*gettimeofday函数 作用:gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中 头文件#include<sys/time.h> 函数int gettimeofday(struct timeval *tv,struct timezone *tz ) 一般将tz设置为NULL,然后tv去当前系统时间 */
tm=localtime(&tp.tv_sec);
/*localtime函数 struct tm *localtime(const time_t *clock); 功 能: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为日历时间结构体。 */
printf("Sec=%ld\t",tp.tv_sec); printf("Usec=%ld\t",tp.tv_usec); printf("%d-%d-%d %d:%d:%d\n",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec); } static void InitTime(int tv_sec,int tv_usec)
{ struct itimerval value; signal(SIGALRM,ElsfTimer);//注册SIGALRM函数 value.it_value.tv_sec=tv_sec; value.it_value.tv_usec=tv_usec; value.it_interval.tv_sec=tv_sec; value.it_interval.tv_usec=tv_usec; setitimer(ITIMER_REAL,&value,NULL);
/* setitimer函数和alarm函数一样,是用来在某一时间发出信号的 其函数原型为 #include<sys/time.h> int setitimer(int which,const struct itimerval *value,struct itimerval *oldvalum); 若成功,返回0;若出错,返回-1 which 取值 定时器类型 发送信号 ITIMER_REAL 设定绝对时间,即根据系统时间 SIGALRM ITIMER_VIRTUAL 设定程序执行时间(用户模式下) SIGVTALRM ITIMER_PROF 从进程开始后开始计时 SIGPROF struct itimerval { struct timerval it_interval;//计数器重启动的间隔时间 struct timerval it_value;//计数器安装后首先启动的初始值 } struct timerval { long tv_sec;//时间的秒部分 long tv_usec;//时间的微秒部分 } */
} int main() { InitTime(5,0);//初始值为5s,间隔5s while(1) { } return 1; }
程序运行结果如下
Sec=1454635995 Usec=673091 2016-2-5 9:33:15 Sec=1454636000 Usec=673273 2016-2-5 9:33:20 Sec=1454636005 Usec=673461 2016-2-5 9:33:25 Sec=1454636010 Usec=673671 2016-2-5 9:33:30 Sec=1454636015 Usec=673832 2016-2-5 9:33:35 Sec=1454636020 Usec=674001 2016-2-5 9:33:40 Sec=1454636025 Usec=673166 2016-2-5 9:33:45 Sec=1454636030 Usec=673344 2016-2-5 9:33:50 ^C
相关文章推荐
- 关于spark运行FP-growth算法报错com.esotericsoftware.kryo.KryoException
- 订餐系统——Gridview、Repeater和DataList 区别
- Item 12: 把重写函数声明为“override”的
- Java [Leetcode 160]Intersection of Two Linked Lists
- 静态和动态断言调试
- Android缓存文件的存放
- 超棒的 15 款 Bootstrap UI 编辑器
- [0] CMake综述
- 欢迎使用CSDN-markdown编辑器
- 【树】Construct Binary Tree from Preorder and Inorder Traversal
- Java对象的浅克隆和深克隆
- Android百度地图poi范围搜索
- AngularJS+BootStrap的一些插件
- C++lambda详解~读书笔记
- Item 11: 比起private undefined function优先使用deleted function
- Java序列化与反序列化学习(三):序列化机制与原理
- android camera2配置流程
- Item 10: 比起unscoped enum更偏爱scoped enum
- Java序列化与反序列化学习(二):序列化接口说明
- 图(并查集) 5