1

ualarm()未在 Android 版本的 libc、bionic 中实现(检查Bionic unistd.h)。此外,ualarm() 已过时。

我在 Android NDK 上移植一个应用程序,所以我需要一个等效的ualarm(999999,999999),即会定期(每秒)发送一个SIGALRM的东西。

也许与timer_create()?它似乎在 Bionic 中实现。但是手册页中的示例确实不是微不足道的......

我愿意移植到 Android (NDK) 的代码:

/* 
 * Set a recurring 1 second timer that will call sigalrm() and switch to 
 * a new channel.
 */

    act.sa_handler = sigalrm_handler;
    sigaction (SIGALRM, &act, 0);
    ualarm(CHANNEL_INTERVAL, CHANNEL_INTERVAL);
    change_channel(1);

/* Whenever a SIGALRM is thrown, go to the next 802.11 channel */
void sigalrm_handler(int x)
{
    next_channel();
}
4

1 回答 1

0

好的,create_timer 是关键。太糟糕了,没有人及时举例回答。

我通过简化这个答案来构建这个代码。

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>

// Handler that will be called every its.it_value.tv_sec seconds + its.it_value.tv_nsec nanoseconds until you kill the timer
static void handler(int sig, siginfo_t *si, void *uc)
{
        printf("Caught signal %d from timer\n", sig);
}

int main(int argc, char *argv[])
{
    struct sigevent sev;
    struct itimerspec its;
    sigset_t mask;
    struct sigaction sa;
    timer_t timerid;

    // Define sigaction: handler
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
    sigaction(SIGUSR1, &sa, NULL);

    // Define sigevent
    sev.sigev_notify = SIGEV_SIGNAL;
    sev.sigev_signo = SIGUSR1;

    // Create the timer
    timer_create(CLOCK_REALTIME, &sev, &timerid);
    its.it_value.tv_sec = 1;
    its.it_value.tv_nsec = 0;
    its.it_interval.tv_sec = its.it_value.tv_sec;
    its.it_interval.tv_nsec = its.it_value.tv_nsec;
    timer_settime(timerid, 0, &its, NULL);

    // Sleeps (100sec) is aborted: its default behavior is to wake up when a signal is received (1sec)
    // So we sleep again to properly see the next signal in this example.
    // That's just for the example so that the process doesn't terminate.
    sleep(100); sleep(100); sleep(100); sleep(100); sleep(100); sleep(100);
    exit(EXIT_SUCCESS);
}

使用 ualarm(0, 0) 禁用周期性定时器。无需在参数中传递计时器。但是使用 time_delete,我们需要这个计时器引用。因此,必须修改代码以记住计时器引用以禁用它。

因此,如果需要,通过引用传递计时器:

some_function(..., &timerid);
void some_function(..., timer_t *timerid)

并禁用计时器:

timer_delete(timerid);

另请注意,ualarm 使用 SIGALRM 信号。在这段代码中,我们改用 SIGUSR1。

SIGALRM、SIGVTALRM 和 SIGPROF SIGALRM、SIGVTALRM 和 SIGPROF 信号在调用前面的警报设置函数(例如 setitimer)中指定的时间限制过去时发送到进程。SIGALRM 在实际时间或时钟时间过去时发送。当进程使用的 CPU 时间过去时发送 SIGVTALRM。当进程和系统代表进程使用的 CPU 时间过去时,会发送 SIGPROF。

SIGUSR1 和 SIGUSR2 SIGUSR1 和 SIGUSR2 信号被发送到进程以指示用户定义的条件。

如果您使用多个都使用 SIGUSR1 的计时器,则可能存在计时器冲突。检查原始答案以处理所有使用 SIGUSR1 的多个计时器。

于 2014-05-03T18:28:24.380 回答