2012-01-23 2 views
0

Я пытался использовать таймеры POSIX для обработки сигналов POSIX. Когда я пытаюсь Выполнение выражений кода, который вы можете найти downhere, я получаю:struct itimerspec как параметр timer_create недействительный аргумент

Errore timer_settime: Недопустимый аргумент

На GAPIL книги, которая основана на Advanced Linux Programming и сетевое программирование Unix, я читал, что это может произойдет, когда внутри new_value.value вы определили отрицательное значение времени или количество наносекунд выше 999999999. Но я думаю, что параметры, которые я использовал в порядке ...

#include <string.h> 
#include <stdio.h> 
#include <fcntl.h> 
#include <time.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <unistd.h> 
#include <signal.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
#include <netdb.h> 
#include <sys/fcntl.h> 
#include <sys/wait.h> 
#include <stdbool.h> 

void termination_handler(int signum) 
{ 
    printf("Timer scaduto\n"); 
} 

int main() 
{ 
    timer_t timer1; 
    struct sigevent sigeventStruct; 
    sigeventStruct.sigev_notify = SIGEV_SIGNAL; 
    sigeventStruct.sigev_signo = 10; 
    if(timer_create(_POSIX_MONOTONIC_CLOCK, &sigeventStruct, &timer1) == -1) 
    { 
     printf("Errore timer_create: %s\n", strerror(errno)); 
    } 
    printf("timer_create eseguito\n"); 
    struct itimerspec tempoIniziale; 
    tempoIniziale.it_value.tv_nsec = 0; 

    struct itimerspec tempoFinale; 
    tempoFinale.it_value.tv_nsec = 10000000; 

    if(timer_settime(timer1, 0, &tempoIniziale, &tempoFinale) == -1) 
    { 
     printf("Errore timer_settime: %s\n", strerror(errno)); 
    } 





    struct sigaction newSigAzione, oldSigAzione; 


    newSigAzione.sa_handler = termination_handler; 
    //oldSigAzione.sa_handler = termination_handler; 
    sigemptyset (&newSigAzione.sa_mask); 

    newSigAzione.sa_flags = 0; 


    sigaction (SIGEV_SIGNAL, NULL, &oldSigAzione); 
    if(oldSigAzione.sa_handler != SIG_IGN) 
    { 
     //sigaction (SIGEV_SIGNAL, newSigAzione, NULL); 
    } 
    /*sigaction (SIGINT, NULL, &oldSigAzione); 
    if (oldSigAzione.sa_handler != SIG_IGN) 
     sigaction (SIGINT, &newSigAzione, NULL); 
    sigaction (SIGHUP, NULL, &oldSigAzione); 
    if (oldSigAzione.sa_handler != SIG_IGN) 
     sigaction (SIGHUP, &newSigAzione, NULL); 
    sigaction (SIGTERM, NULL, &oldSigAzione); 
    if (oldSigAzione.sa_handler != SIG_IGN) 
     sigaction (SIGTERM, &newSigAzione, NULL);*/ 

    /*sigaction (SIGTERM, &newSigAzione, NULL);*/ 

    return 0; 
} 
+0

Что такое 10? Какой сигнал в вашей системе? –

+0

Это произвольное число, которое я установил для однозначной идентификации таймера. Это потому, что в будущем я мог бы использовать много таймеров togheter –

+0

Я думаю, вам нужно 'exit()' если 'timer_create()' терпит неудачу. –

ответ

3

_POSIX_MONOTONIC_CLOCK особенность теста макрос, который говорит вы ли монотонные часы ar e доступно в системе.

Доступные часы Идентификаторы вы можете передать в timer_create() на Linux являются:

 
CLOCK_REALTIME 
    System-wide realtime clock. Setting this clock requires appropriate privileges. 
CLOCK_MONOTONIC 
    Clock that cannot be set and represents monotonic time since some unspecified starting point. 
CLOCK_PROCESS_CPUTIME_ID 
    High-resolution per-process timer from the CPU. 
CLOCK_THREAD_CPUTIME_ID 
    Thread-specific CPU-time clock. 

Вы также должны инициализировать все члены в struct sigevent и struct itimerspec. . вы не устанавливаете .tv_sec в structitimer_spec, только .tv_nsec, что приводит к значениям мусора в этих членах.

... 
memset(&sigeventStruct, 0, sizeof sigeventStruct); 
... 
and 
struct itimerspec tempoFinale; 
memset(&tempoFinale, 0, sizeof tempoFinale); 
tempoFinale.it_value.tv_nsec = 10000000; 
+0

Отличный, замечательный –

Смежные вопросы