#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
void my_handler (timer_t timerid)
{
printf("\nHello World\n");
}
int main()
{
// declare some variables
timer_t timerid;
struct itimerspec itime;
struct sigevent sevp;
// initalize the sigevent variable
// the memset is necessary to avoid segmentation faults
memset (&sevp, 0, sizeof (struct sigevent));
sevp.sigev_notify=SIGEV_THREAD;
sevp.sigev_notify_function=(void *)my_handler;
timer_create (CLOCK_REALTIME, &sevp, &timerid);
// initialize the timer specification
// currently the offset and the period is 5 seconds // you have to adjust this to your actual needs
itime.it_value.tv_sec = 5;
itime.it_value.tv_nsec = 0;
itime.it_interval.tv_sec = 5;
itime.it_interval.tv_nsec = 0;
// create the timer
timer_settime (timerid, 0, &itime, NULL);
// this loop has to be adapted, so that the program ends after some time while(1){
// sleep(100);
}
ОШИБКИ:Как решить эти семантические ошибки?
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/new.d" -MT"src/new.d" -o "src/new.o" In 'CLOCK_REALTIME' undeclared (first use in this function)
Field 'tv_nsec' could not be resolved Semantic Error
Field 'tv_nsec' could not be resolved Semantic Error
Field 'tv_sec' could not be resolved Semantic Error
Field 'tv_sec' could not be resolved Semantic Error
storage size of 'itime' isn't known C/C++ Problem
Я использую операционную систему Linux с Eclipse IDE.
Читали [страница руководства] (http://man7.org/linux/man-pages/man2/timer_create.2.html)? Вам не хватает файла заголовка, указанного на странице руководства. –
Кто-то ответил как: успешно работает с -std = gnu99 ?? где это было? – user3169825
Добавьте его как один из аргументов в 'gcc'. – timrau