【问题标题】:Linux C ignore event for x secondsLinux C 忽略事件 x 秒
【发布时间】:2018-10-13 16:13:01
【问题描述】:

我有以下 arduino 代码:

uint32_t hold_time=600000;
uint32_t curr_time;
uint32_t last_event;
bool on_hold=false;


beginning of main loop

 curr_time = millis();

 if (on_hold && curr_time - last_event >= hold_time) {        
        on_hold = false;
    }

...

if (!on_hold)
  {
    run the function();
    on_hold = true; // Ignore this function for 1 minute
  }

这基本上会执行多次主循环,但 run_the_function();仅当它被解锁时,在此示例中每分钟一次。我想在适用于 BSD 的标准 POSIX C 中完成同样的工作。

【问题讨论】:

  • 好吧,如果可能的话,你应该尽量避免出现“疯狂的旋转循环”。我对 POSIX 了解不多,但也许你应该寻找一个内置计时器

标签: c linux time bsd


【解决方案1】:

既然你要POSIX,那我就给你POSIX。这是一个示例代码,它能够在不使用pthreads 的情况下运行计时器,但只能通过操作系统提供的计时器。它每 2 秒运行一次特定功能。如果您愿意,可以将其配置为每 60 秒运行一次。我已经对代码进行了彻底的注释,我希望它很容易理解:

#include <stdlib.h> // For declaration of exit
#include <stdio.h> // For printf function
#include <signal.h> // Will be used for the signal callbacks
#include <time.h> // Timer and current time stuff

#define TIMER_SECONDS 2 // To test more rapidly I will wait 
                        // only for 2 seconds instead of a minute...

int counter = 0; // Whe want to call the timer a limited number 
                 // of time for this example

// BEFORE READING THIS, READ THE MAIN:
// This function is your "run_the_function" callback. As for now, 
// it has no arguments and returns nothing. It asks to the system the current
// time and prints it, just to check if the timer works. It uses **printf**
// and this should be avoided in signal handlers!
static void run_the_function() {
  time_t rawtime;                    // This is the current time variable
  struct tm * timeinfo;              // This is a strut that contains the time 
                                     // broken down to its components
  time( &rawtime );                  // To get from the system the current time
  timeinfo = localtime ( &rawtime ); // For splitting the time in its components
  printf("Timer CALLED %d times :: %s", ++counter, asctime(timeinfo));
}

// BEFORE READING THIS, READ THE MAIN
// This is the signal handler, a function that is called when the timer
// signals that has finished to count
static void timer_callback(int sig,  siginfo_t *si, void *uc) {
  run_the_function();
}

int main() {
  timer_t timer_id;              // An unique identifier for the timer that you are creating
  struct itimerspec intervals;   // Specify the intervals for the timer that we are creating
  struct sigevent timer_event;   // The structure that handles the event generated by the timer
  struct sigaction timer_action; // The action for the timer event

  // First you need to implement the action to do when the timer reaches zero,
  // then you need to say that you want an event for a timer that reaches zero,
  // and only at the end you set the timer.
  // The function "sigaction" connects your timer event to the timer signal SIGRTMIN.
  // The timer_event.sigev_signo instructs to create an EVENT for the signal SIGRTMIN, and
  // for that event you prepared a custom action.
  // The timer sends the SIGRTMIN signal every time it reaches zero, and when you
  // create it, you connect it to the timer_event.

  // Now we define what is the action to perform
  timer_action.sa_flags = SA_SIGINFO;               // The action to perform is to run a callback
  timer_action.sa_sigaction = timer_callback;       // The callback is "timer_callback"
  sigemptyset(&timer_action.sa_mask);               // And we are initializing the event structure
  if (sigaction(SIGRTMIN, &timer_action, NULL) < 0) // We are binding this action 
    exit(1);                                        // to a timer event (SIGRTMIN)

  timer_event.sigev_notify = SIGEV_SIGNAL; // Instruct the event that it is related to
                                           // a signal. 
  timer_event.sigev_signo = SIGRTMIN;      // Instruct the event that the signal to track is SIGRTMIN

  // At this point we are ready to create the timer, that uses the REAL TIME CLOCK of your
  // system. When it reaches zero it raise a timer_event, and it also sets the id of the
  // created timer.
  if (timer_create(CLOCK_REALTIME, &timer_event, &timer_id) < 0)
    exit(1);

  // We now need to define the times for the timer. Intervals is composed by
  // two structure: it_value, that contains the current time (or the starting time
  // for the first run of your timer) and it_intervals, the time at which it will be
  // reset after completing one lap. If you set it_interval to zero, the timer runs only
  // one time. If you set it_value to zero, the timer does not run.
  intervals.it_value.tv_sec = TIMER_SECONDS;
  intervals.it_value.tv_nsec = 0;
  intervals.it_interval.tv_sec = TIMER_SECONDS;
  intervals.it_interval.tv_nsec = 0;

  // Let's setup the time and the interval of the timer, so that it starts...
  if (timer_settime(timer_id, 0, &intervals, NULL) < 0)
    exit(1);

  // And now we have only to wait for the timer... Easy, isn't it?
  printf("Let's go!\n");
  while(counter < 5) { /* Do your stuff here*/ };

  return 0;
}

你需要编译它:

gcc test.c -lrt -o test

然后运行它:

./test

Let's go!
Timer CALLED 1 times :: Thu May  3 15:48:29 2018
Timer CALLED 2 times :: Thu May  3 15:48:31 2018
Timer CALLED 3 times :: Thu May  3 15:48:33 2018
Timer CALLED 4 times :: Thu May  3 15:48:35 2018
Timer CALLED 5 times :: Thu May  3 15:48:37 2018

【讨论】:

  • C语言中没有定时器,所有定时器都是操作系统或框架提供的
猜你喜欢
  • 2022-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多