本文介绍了结构体itimerspec为timer_create的参数无效的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图POSIX计时器togheter与POSIX信号处理。
当我尝试excecute的code,你可以找到downhere,我得到:

Errore timer_settime:无效的参数

在GAPIL书,那是基于高级Linux编程和Unix网络编程,我读到这可能发生new_value.value您指定一个负的时间值或纳秒数比999999999更高内部时。
但我认为,我已经使用的参数是可以的...

 的#include<&string.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&;&time.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; ARPA / inet.h>
#包括LT&; netinet / in.h中>
#包括LT&;&netdb.h中GT;
#包括LT&; SYS / fcntl.h>
#包括LT&; SYS / wait.h>
#包括LT&;&stdbool.h GT;无效termination_handler(INT正负号)
{
    的printf(计时器斯卡杜托\\ n);
}诠释的main()
{
    timer_t定时器1;
    结构的sigevent sigeventStruct;
    sigeventStruct.sigev_notify = SIGEV_SIGNAL;
    sigeventStruct.sigev_signo = 10;
    如果(timer_create(_POSIX_MONOTONIC_CLOCK,&安培; sigeventStruct,&安培;定时器1)== -1)
    {
        的printf(Errore timer_create:%S \\ n字符串错误(错误));
    }
    的printf(timer_create eseguito \\ n);
    结构itimerspec tempoIniziale;
    tempoIniziale.it_value.tv_nsec = 0;    结构itimerspec tempoFinale;
    tempoFinale.it_value.tv_nsec =千万;    如果(timer_settime(定时器1,0,&安培; tempoIniziale,&安培; tempoFinale)== -1)
    {
        的printf(Errore timer_settime:%S \\ n字符串错误(错误));
    }    结构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);
    如果(oldSigAzione.sa_handler!= SIG_IGN)
    {
        //的sigaction(SIGEV_SIGNAL,newSigAzione,NULL);
    }
    / *的sigaction(SIGINT,NULL,&安培; oldSigAzione);
    如果(oldSigAzione.sa_handler!= SIG_IGN)
      的sigaction(SIGINT,和放大器; newSigAzione,NULL);
    的sigaction(SIGHUP,NULL,&安培; oldSigAzione);
    如果(oldSigAzione.sa_handler!= SIG_IGN)
      的sigaction(SIGHUP,&安培; newSigAzione,NULL);
    的sigaction(SIGTERM,NULL,&安培; oldSigAzione);
    如果(oldSigAzione.sa_handler!= SIG_IGN)
      的sigaction(SIGTERM,&安培; newSigAzione,NULL); * /    / *的sigaction(SIGTERM,&安培; newSigAzione,NULL); * /    返回0;
}


解决方案

_POSIX_MONOTONIC_CLOCK是一个功能测试宏告诉你单调时钟是否可用在系统上。

可用的时钟IDS你可以通过在Linux上timer_create()是:


CLOCK_REALTIME
  全系统实时时钟。设置此时钟要求相应的权限。
CLOCK_MONOTONIC
  不能设置时钟和再presents因为一些不确定的起点单调的时间。
CLOCK_PROCESS_CPUTIME_ID
  高分辨率每进程计时器从CPU。
CLOCK_THREAD_CPUTIME_ID
  线程特定的CPU时钟。

您还必须初始化所有在结构的sigevent 成员和结构itimerspec
例如。您没有设置 .tv_sec 在structitimer_spec,只有 .tv_nsec ,这导致在这些成员垃圾值。

  ...
memset的(安培; sigeventStruct,0,sizeof的sigeventStruct);
...

结构itimerspec tempoFinale;
memset的(安培; tempoFinale,0,sizeof的tempoFinale);
tempoFinale.it_value.tv_nsec =千万;

I was trying POSIX timers togheter with POSIX signals handling.When I try to excecute the code you can find downhere, I get:

Errore timer_settime: Invalid argument

On GAPIL book, that is based upon Advanced Linux Programming and Unix network programming, I read that this can happen when inside new_value.value you specified a negative time value or a number of nanoseconds higher than 999999999.But I think that parameters I have used are okay...

#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;
}
解决方案

_POSIX_MONOTONIC_CLOCK is a feature test macro that tells you whether monotonic clocks are available on the system.

The available clock ids you can pass to timer_create() on Linux are:

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.

You must also initialize all the members in struct sigevent and struct itimerspec. E.g. you don't set .tv_sec in the structitimer_spec, only .tv_nsec , which results in garbage values in those members.

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

这篇关于结构体itimerspec为timer_create的参数无效的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 01:18