Linux struct itimerval用法
来源:互联网 发布:网络安全教育讲话稿 编辑:程序博客网 时间:2024/05/01 07:15
http://blog.csdn.net/hbuxiaofei/article/details/35569229
先看一段代码
- #include <stdio.h>
- #include <time.h>
- #include <sys/time.h>
- #include <stdlib.h>
- #include <signal.h>
- static int count = 0;
- void set_timer()
- {
- struct itimerval itv;
- itv.it_value.tv_sec = 3; //timer start after 3 seconds later
- itv.it_value.tv_usec = 0;
- itv.it_interval.tv_sec = 1;
- itv.it_interval.tv_usec = 0;
- setitimer(ITIMER_REAL,&itv,NULL);
- }
- void signal_handler(int m)
- {
- count ++;
- printf("%d\n",count);
- }
- int main()
- {
- signal(SIGALRM,signal_handler);
- set_timer();
- while(count < 10);
- exit(0);
- return 0;
- }
这段代码实现的功能:3秒钟后启动定时器,然后每隔1秒钟向终端打印count的递增值,当count到10时程序退出。
结构体原型:
- struct itimerval {
- struct timeval it_interval; /* next value */
- struct timeval it_value; /* current value */
- };
- struct timeval {
- long tv_sec; /* seconds */
- long tv_usec; /* microseconds */
- };
- struct itimerval {
- struct timeval it_interval; /* 计时器重启动的间歇值 */
- struct timeval it_value; /* 计时器安装后首先启动的初始值 */
- };
- struct timeval {
- long tv_sec; /* 秒 */
- long tv_usec; /* 微妙(1/1000000) */
- };
再来看这个函数:
int setitimer(int which, const struct itimerval *value,struct itimerval *ovalue);
setitimer()将value指向的结构体设为计时器的当前值,如果ovalue不是NULL,将返回计时器原有值。
which:间歇计时器类型,有三种选择
ITIMER_REAL //数值为0,计时器的值实时递减,发送的信号是SIGALRM。
ITIMER_VIRTUAL //数值为1,进程执行时递减计时器的值,发送的信号是SIGVTALRM。
ITIMER_PROF //数值为2,进程和系统执行时都递减计时器的值,发送的信号是SIGPROF。
返回说明:
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EFAULT:value或ovalue是不有效的指针
EINVAL:其值不是ITIMER_REAL,ITIMER_VIRTUAL 或 ITIMER_PROF之一
0 0
- Linux struct itimerval用法
- Linux struct itimerval用法
- Linux struct itimerval用法
- Linux struct itimerval用法
- 嵌入式 Linux struct itimerval用法
- Linux struct itimerval学习
- linux下定时器struct itimerval的使用
- struct 用法
- struct用法
- struct用法
- typedef struct 用法详解
- typedef struct的用法
- typedef struct用法详解
- 结构体(struct) 用法
- typedef struct 用法详解
- typedef struct 用法详解
- typedef struct 用法详解
- typedef struct 用法详解
- bzoj 1696 贪心
- C - The C Answer (2nd Edition) - Exercise 1-7
- java生成说明文档
- Hdu 5115 ---区间dp(2014北京现场赛D题)
- 数组-08. 字符串转换成十进制整数
- Linux struct itimerval用法
- 外部类监听事件的点击事件
- STM32W108无线射频模块串行通信接口编程实例
- Android控件详解之Textview
- JS面向对象的程序设计
- linux 共享内存
- db dw dd 与equ的区别
- 使用libxml解析HTML -- DTHTMLParser
- pandas的read_csv()函数读取的文件路径问题