"setitimer()" 启动的计时器会重新启动吗?

will the timer started by "setitimer()" be restarted?

每个 task_struct in linux kernel has a field named "real timer", which is a struct hrtimer (high resolution timer). When we set a timer using setitimer, it sets the "real timer" in the process to be expired by the given value. When it is expired, the function named it_real_fn is called. Here is the source code in Linux kernel 2.6.39.4:

/*
 * The timer is automagically restarted, when interval != 0
 */
enum hrtimer_restart it_real_fn(struct hrtimer *timer)
{
    struct signal_struct *sig =
        container_of(timer, struct signal_struct, real_timer);

    trace_itimer_expire(ITIMER_REAL, sig->leader_pid, 0);
    kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);

    return HRTIMER_NORESTART;
}

我发现是returnsHRTIMER_NORESTART,意思是不应该重启。但是,如果我们在调用setitimer的时候赋了一个时间间隔值,意思是我们想在每个时间间隔触发定时器,那么"real timer"应该从哪里重启呢?

你说 it_real_fn 函数不会重新启动实时计时器是正确的,但它不是使用 setitimer 时计时器到期时调用的函数。

setitimer 函数是 POSIX 计时器的一部分,它们的 Linux 实现在 posix-timers.c, In this file, the function posix_timer_fn, which may return both HRTIMER_RESTART and HRTIMER_NORESTART is defined and assigned to the it_real_fn of the struct hrtimer (the code which sets this function as the timer callback is in common_timer_set 中,由 POSIX 计时器初始化调用).