Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation avec
Le langage C
Voir le programme détaillé

Fonction timer_gettime (POSIX)

Entête à inclure

#include <time.h>

Fonction timer_gettime

int timer_gettime( timer_t timerid, struct itimerspec *value );

Lit l'état courant d'un timer POSIX.

Cet élément est fourni par POSIX. Il peut nécessiter l'activation d'une macro de visibilité POSIX avant l'inclusion de l'entête.

Exemple de code

Cet exemple arme un timer monotone pour quelques secondes, puis relit aussitôt son état avec timer_gettime.

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
#define _POSIX_C_SOURCE 200809L

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

    timer_t timerid;
    struct sigevent event = { 0 };
    struct itimerspec spec = { 0 };
    struct itimerspec current;

    event.sigev_notify = SIGEV_NONE;
    spec.it_value.tv_sec = 5;

    if ( timer_create( CLOCK_MONOTONIC, &event, &timerid ) == -1 ) {
        perror( "timer_create" );
        return EXIT_FAILURE;
    }

    if ( timer_settime( timerid, 0, &spec, NULL ) == -1 ) {
        perror( "timer_settime" );
        timer_delete( timerid );
        return EXIT_FAILURE;
    }

    if ( timer_gettime( timerid, &current ) == -1 ) {
        perror( "timer_gettime" );
        timer_delete( timerid );
        return EXIT_FAILURE;
    }

    printf( "Temps restant : %ld s\n", (long) current.it_value.tv_sec );
    timer_delete( timerid );
    return EXIT_SUCCESS;
}
Exemple d'utilisation de la fonction timer_gettime

Sujets connexes

La librairie <time.h>


Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation avec
Le langage C
Voir le programme détaillé