如何使用setitimer来定时
来源:互联网 发布:创意 书 知乎 编辑:程序博客网 时间:2024/06/06 12:35
对于下面的演示代码,我们完全可以使用sighandler_t signal(int signum, sighandler_t handler);来替代sigaction。
#include <iostream>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
using namespace std;
volatile static long counter = 0;
static void notify( int signum )
{
assert( signum == SIGALRM );
++ counter;
}
static void wait( long timeout_ms )
{
struct timespec spec;
spec.tv_sec = timeout_ms / 1000;
spec.tv_nsec = (timeout_ms % 1000) * 1000000;
nanosleep( &spec, NULL );
}
int main( void )
{
struct itimerval tim_ticks;
struct sigaction act;
struct sigaction oldact;
tim_ticks.it_value.tv_sec = 0;
tim_ticks.it_value.tv_usec = 100;
tim_ticks.it_interval.tv_sec = 0;
tim_ticks.it_interval.tv_usec = 100;
sigemptyset( &act.sa_mask );
act.sa_flags = 0;
act.sa_handler = notify;
int res = sigaction( SIGALRM, &act, &oldact );
if ( res )
{
perror( "Fail to install handle: " );
return -1;
}
res = setitimer( ITIMER_REAL, &tim_ticks, 0 );
if ( res )
{
perror( "Fail to set timer: " );
sigaction( SIGALRM, &oldact, 0 );
return -2;
}
for ( ; ; )
{
cout << "counter = " << counter << endl;
wait( 10 );
}
return 0;
}
#include <iostream>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
using namespace std;
volatile static long counter = 0;
static void notify( int signum )
{
assert( signum == SIGALRM );
++ counter;
}
static void wait( long timeout_ms )
{
struct timespec spec;
spec.tv_sec = timeout_ms / 1000;
spec.tv_nsec = (timeout_ms % 1000) * 1000000;
nanosleep( &spec, NULL );
}
int main( void )
{
struct itimerval tim_ticks;
struct sigaction act;
struct sigaction oldact;
tim_ticks.it_value.tv_sec = 0;
tim_ticks.it_value.tv_usec = 100;
tim_ticks.it_interval.tv_sec = 0;
tim_ticks.it_interval.tv_usec = 100;
sigemptyset( &act.sa_mask );
act.sa_flags = 0;
act.sa_handler = notify;
int res = sigaction( SIGALRM, &act, &oldact );
if ( res )
{
perror( "Fail to install handle: " );
return -1;
}
res = setitimer( ITIMER_REAL, &tim_ticks, 0 );
if ( res )
{
perror( "Fail to set timer: " );
sigaction( SIGALRM, &oldact, 0 );
return -2;
}
for ( ; ; )
{
cout << "counter = " << counter << endl;
wait( 10 );
}
return 0;
}
- 如何使用setitimer来定时
- Linux下定时器的使用--alarm()&setitimer()及Linux下如何实现秒以下精确定时与休眠
- Linux setitimer参数设定如何使用程序
- linux上定时函数 setitimer 的使用介绍
- linux上定时函数 setitimer 的使用介绍
- setitimer() 定时函数
- setitimer() 定时函数
- setitimer定时函数
- 定时函数setitimer()
- 定时函数setitimer
- Linux下定时函数setitimer
- Linux下定时函数setitimer
- Linux下定时函数setitimer
- linux中定时信号setitimer和alarm
- setitimer
- 【linux 开发】定时器使用setitimer
- setitimer定时器的简单使用
- 使用setitimer逐行打印文件
- Asp.Net从零开始学-8
- 自动磁盘碎片整理
- 网页中META标签的使用
- 什么是WEB标准
- java产生随机数的两种途径
- 如何使用setitimer来定时
- 通过了Sun的系统架构师考试(SCEA),在此分享成功与失败的一点经验
- AJAX产品评论功能
- ASP.NET 调味品:AJAX
- 兼容IE/FF的类title提示效果
- oracle里的常用命令(三):表
- 一个简单的JSF例子
- .Net中通过DataSet直接更新数据库相关问题和解决方法
- HTML中小meta的大作用