gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:35:59 25 4
gpt4 key购买 nike

我正在尝试将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com