简体   繁体   中英

Linux C ignore event for x seconds

I have the following arduino code:

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
  }

This basically will execute the main loop many times but the run_the_function(); only when it is unlocked so in this example once in every minute. I would like to accomplish the same in standard POSIX C which works on BSDs as well.

Since you asked for POSIX, I will give you POSIX. This is a sample code that is able to run a timer without using pthreads , but only through OS provided timers. It runs a specific function every 2 seconds. You can configure it to make it run every 60 seconds, if you prefer. I have comment thoroughly the code, and I hope it is easy enough to understand:

#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;
}

You need to compile it with:

gcc test.c -lrt -o test

and run it with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM