需要说明 GNU C 中设置定时器和警报功能的使用的程序
任何人都可以通过一些程序示例来说明 gnu C 中 settimer 或闹钟功能的使用吗?
我有一个程序可以连续处理一些数据,我需要设置一个每隔 t 秒发出一次的计时器/警报,作为响应,我需要将处理后的数据存储到文件中。该文件写入必须是异步的<即数据处理和文件写入不能互相等待> 。我浏览了 GNU C 库页面,但我不太明白..
[编辑]
我得到了这个程序:
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#define INTERVAL 1
int howmany = 0;
void alarm_wakeup (int i)
{
struct itimerval tout_val;
signal(SIGALRM,alarm_wakeup);
howmany += INTERVAL;
printf("\n%d sec up partner, Wakeup!!!\n",howmany);
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val,0);
}
void exit_func (int i)
{
signal(SIGINT,exit_func);
printf("\nBye Bye!!!\n");
exit(0);
}
int main ()
{
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val,0);
signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
signal(SIGINT,exit_func);
while (1)
{
//printf("!");
}
return 0;
}
但似乎在计时器打开时我无法做任何事情.. 我应该修改什么以满足我的需要?请建议.. [/编辑]
Can anyone illustrate the use of settimer or alarm function in gnu C , with some program examples ,please ?
I have a program that continuously processes some data , and i need to set a timer / alarm that goes off every t seconds , in response to which , i need to store the processed data into a file. This file writing has to be asynchronous < i.e. the data processing and file writing must not wait for each other > . I went through the GNU C Library pages , but i couldn't understand much..
[EDIT]
I got this program :
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#define INTERVAL 1
int howmany = 0;
void alarm_wakeup (int i)
{
struct itimerval tout_val;
signal(SIGALRM,alarm_wakeup);
howmany += INTERVAL;
printf("\n%d sec up partner, Wakeup!!!\n",howmany);
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val,0);
}
void exit_func (int i)
{
signal(SIGINT,exit_func);
printf("\nBye Bye!!!\n");
exit(0);
}
int main ()
{
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val,0);
signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
signal(SIGINT,exit_func);
while (1)
{
//printf("!");
}
return 0;
}
But seems like i cannot do anything while the timer is on..
What should i modify to suit my needs ? Pl suggest..
[/EDIT]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是此处的示例,它使用 < code>setitimer() 定期调用
DoStuff()
。这里的关键是,调用
setitimer()
会导致操作系统在指定时间过后调度一个SIGALRM
发送到您的进程,这取决于您的程序当信号到来时处理该信号。您可以通过注册信号类型的信号处理函数(在本例中为 DoStufF())来处理信号,之后操作系统将知道在计时器到期时调用该函数。您可以阅读
setitimer()
手册页 来弄清楚参数是什么以及如何取消计时器。注意:如果您希望计时器仅触发一次,则必须调用
alarm()
或ualarm()
而不是setitimer()
。Here's an example from here which uses
setitimer()
to periodically callDoStuff()
.The key here is that calling
setitimer()
results in the OS scheduling aSIGALRM
to be sent to your process after the specified time has elapsed, and it is up to your program to handle that signal when it comes. You handle the signal by registering a signal handler function for the signal type (DoStufF()
in this case) after which the OS will know to call that function when the timer expires.You can read the
setitimer()
man page to figure out what the arguments are and how to cancel a timer.Note: if you want the timer to trigger only once, you will have to call
alarm()
orualarm()
instead ofsetitimer()
.