需要说明在 GNU C 中使用 settimer 和警报功能的程序

编程入门 行业动态 更新时间:2024-10-24 10:25:52
本文介绍了需要说明在 GNU C 中使用 settimer 和警报功能的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

谁能用一些程序示例来说明gnu C中settimer或alarm函数的使用?

Can anyone illustrate the use of settimer or alarm function in gnu C , with some program examples ,please ?

我有一个程序可以连续处理一些数据,我需要设置一个每 t 秒响一次的计时器/警报,作为响应,我需要将处理后的数据存储到一个文件中.此文件写入必须是异步的 <即数据处理和文件写入不能互相等待 > .我浏览了 GNU C 库页面,但我不太明白..

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..

我得到了这个程序:

#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(" %d sec up partner, Wakeup!!! ",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(" Bye Bye!!! "); 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]

推荐答案

这里有一个来自 此处 使用 setitimer() 定期调用 DoStuff().

Here's an example from here which uses setitimer() to periodically call DoStuff().

这里的关键是调用 setitimer() 会导致操作系统调度 SIGALRM 在指定的时间过去后发送到您的进程,并且它已启动到您的程序来处理该信号.您可以通过为信号类型(在本例中为 DoStufF())注册一个信号处理函数来处理信号,之后操作系统将知道在计时器到期时调用该函数.

The key here is that calling setitimer() results in the OS scheduling a SIGALRM 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.

您可以阅读 setitimer() 手册页找出参数是什么以及如何取消计时器.

You can read the setitimer() man page to figure out what the arguments are and how to cancel a timer.

注意:如果你想让定时器只触发一次,你必须调用 alarm() 或 ualarm() 而不是 setitimer().

Note: if you want the timer to trigger only once, you will have to call alarm() or ualarm() instead of setitimer().

/* * setitimer.c - simple use of the interval timer */ #include <sys/time.h> /* for setitimer */ #include <unistd.h> /* for pause */ #include <signal.h> /* for signal */ #define INTERVAL 500 /* number of milliseconds to go off */ /* function prototype */ void DoStuff(void); int main(int argc, char *argv[]) { struct itimerval it_val; /* for setting itimer */ /* Upon SIGALRM, call DoStuff(). * Set interval timer. We want frequency in ms, * but the setitimer call needs seconds and useconds. */ if (signal(SIGALRM, (void (*)(int)) DoStuff) == SIG_ERR) { perror("Unable to catch SIGALRM"); exit(1); } it_val.it_value.tv_sec = INTERVAL/1000; it_val.it_value.tv_usec = (INTERVAL*1000) % 1000000; it_val.it_interval = it_val.it_value; if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) { perror("error calling setitimer()"); exit(1); } while (1) pause(); } /* * DoStuff */ void DoStuff(void) { printf("Timer went off. "); }

更多推荐

需要说明在 GNU C 中使用 settimer 和警报功能的程序

本文发布于:2023-11-27 15:57:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638653.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   功能   程序   GNU   settimer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!