Retourner au contenu associé (entrée de forum : Alarme (boucle))
Posté par Vincent ORDY le 12 octobre 2008 à 19:00. En réponse au message Alarme (boucle). Évalué à 3.
#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; }
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
# Re: Alarme (boucle)
Posté par Vincent ORDY . En réponse au message Alarme (boucle). Évalué à 3.
#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.