c - struct itimerspec 作为 timer_create 的参数无效参数

标签 c timer posix

我正在尝试将 POSIX 计时器与 POSIX 信号处理结合起来。 当我尝试执行您可以在此处找到的代码时,我得到:

timer_settime 错误:参数无效

在基于高级 Linux 编程和 Unix 网络编程的 GAPIL 书中,我读到当您在 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;
}

最佳答案

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

您可以在 Linux 上传递给 timer_create() 的可用时钟 ID 是:

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;

关于c - struct itimerspec 作为 timer_create 的参数无效参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8973079/

相关文章:

c++ - 如何确定网络堆栈何时准备好再次打开到同一主机/端口的套接字?

c - POSIX 进程组

c - Mac OS X 上 exuberant ctags 的问题

c 预处理器宏在扩展后连接参数

javascript - 在新游戏开始前将计时器和得分值重置为 0

linux - 我如何判断 SIGILL 是来自非法指令还是来自 kill -ILL?

java - JNA:如何处理未知结构?

使用 .C() 从 R 调用已编译的 C

c - stm32g483 基于 SysTick 的计时器的奇怪行为

javascript - 如何在函数执行之前等待一秒钟