- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将 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/
我正在尝试使用timerfd_create() 和timer_settime() 用C 语言编写一个计时器。我正在使用 CLOCK_MONOTONIC。但是尽管我包含了这些头文件, #include
我有这个问题:我使用 itimerspec 结构创建了一个结构。 itimerspec 结构有两个字段: struct timespec { time_t tv_sec;
我正在填充 timespec 结构。目的是,用户将始终以秒为单位输入值(也可以是 0.01 秒),因此我们使用以下方法将秒转换为纳秒:lt_leak_start = atoll(getenv("LT_
几年前,我问了一个关于 how to initialize a timespec struct 的问题使用最少的代码行到归零状态(纳秒和秒等于零)。 许多回复告诉我不要使用 memset,因为这不是跨
我正在尝试将 POSIX 计时器与 POSIX 信号处理结合起来。当我尝试执行您可以在此处找到的代码时,我得到: timer_settime 错误:参数无效 在基于高级 Linux 编程和 Unix
因此,直到最近我们决定将其移至 Lubuntu 12.04 系统时,该代码才能正常运行。对 timer_settime 的调用返回 EINVAL,并在 gdb 下运行它我已经确认 ts 的所有字段在调
我是一名优秀的程序员,十分优秀!