Perform function at every given time [closed]

2

Hello I would like to implement a function in my program that performs a task every time period having this code as an example something so I am wanting

void main(){
    while(1){
        if(passou 5 minutos){
            chama_funcao();
        }
    }
    
asked by anonymous 24.09.2017 / 00:01

2 answers

1

I thought of something as a counter, and setting the time according to the value that the a variable increases. The sleep(); function basically works as a timer, so this function checks every 1 second.

The code looks like this:

 void main(){
        int a = 0;
        while(1){
            a++;
            if(passou 5 minutos){
                chama_funcao();
            }
            sleep(1);
        }
    
24.09.2017 / 00:58
1

One of the ways to do this is with time() of time.h :

#include <time.h>

Code:

time_t tempoInicial;
time_t tempoAtual;
tempoInicial = time(NULL);
while (1){
    tempoAtual = time(NULL);
    if (tempoAtual / 60 > tempoInicial / 60 + 5){
        tempoInicial = time(NULL);
        //O que estiver aqui, será executado de 5 em 5 minutos
    }
}
    
24.09.2017 / 01:00