• # Re: Alarme (boucle)

    Posté par . En réponse au message Alarme (boucle). Évalué à 3.

    Je cherche à lancer une alarme toutes les 5 secondes.

    #include <signal.h>
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h> // EXIT_*
    #include <unistd.h> // pause()

    #define ALRM_DATA 42

    typedef struct sigevent sigevent_t;
    typedef struct itimerspec itimerspec_t;

    void sig_hdler(union sigval value)
    {
    if (value.sival_int == ALRM_DATA)
    {
    puts("Alarm received");
    }
    }

    int main()
    {
    int res;
    timer_t timerid;
    sigevent_t evp;
    const itimerspec_t timer_value =
    {
    { 5, 0 },
    { 5, 0 }
    };

    evp.sigev_notify = SIGEV_THREAD;
    evp.sigev_signo = SIGALRM;
    evp.sigev_value.sival_int = ALRM_DATA;
    evp.sigev_notify_function = sig_hdler;
    evp.sigev_notify_attributes = NULL;

    res = timer_create(CLOCK_REALTIME, &evp, &timerid);
    if (res)
    {
    perror("timer_create() failed");
    return EXIT_FAILURE;
    }

    timer_settime(timerid, TIMER_ABSTIME, &timer_value, NULL);

    pause();

    return EXIT_SUCCESS;
    }



    À compiler avec -lrt.