需要,介绍了GNU C使用的SetTimer和报警功能的程序 [英] need programs that illustrate use of settimer and alarm functions in GNU C

查看:224
本文介绍了需要,介绍了GNU C使用的SetTimer和报警功能的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能说明了GNU C使用的SetTimer或报警功能,具有一定的程序示例吗?

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("\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;

}

但好像在定时器是我不能做任何事情..
我应该怎么修改以满足我的需要? PL建议..
[/编辑]

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函数()结果在OS调度 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.

请注意:如果你希望计时器只触发一次,你将不得不调用报警() 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.\n");

}

这篇关于需要,介绍了GNU C使用的SetTimer和报警功能的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆