sigtimedwait()在超时之前返回EAGAIN [英] sigtimedwait() returns EAGAIN before timeout

查看:449
本文介绍了sigtimedwait()在超时之前返回EAGAIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用sigtimedwait(),但是我发现它不是在等待超时完成.低于它,似乎返回EAGAIN的速度比应有的快4秒(每1分钟的超时速度快1秒):

I'm trying to learn how to use sigtimedwait(), but I find that it's not waiting for the timeout to complete. Below it seems to return EAGAIN 4s faster than it should (1s faster per 1min of timeout):

#include <signal.h>
#include <syslog.h>
#include <stdarg.h>
#include <stddef.h>
#include <errno.h>


int main(int argc, char* argv[]) {
    setlogmask(LOG_UPTO(LOG_NOTICE));
    openlog ("SIG_TIMED_WAITER", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
    syslog (LOG_NOTICE, "Started");

    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGUSR1);

    struct timespec to;
    to.tv_sec = 240;
    to.tv_nsec = 0;

    int ret = sigtimedwait(&set, NULL, &to);
    if(ret < 0) {
        if (errno == EAGAIN) {
            syslog (LOG_NOTICE, "EAGAIN: TimedWait complete...");
        } else {
            syslog (LOG_NOTICE, "ERROR!");
        }
    }   else {
        syslog (LOG_NOTICE, "Interrupted by signum: %d.", ret);
    }

    syslog (LOG_NOTICE, "Terminated.");
    closelog();
}

这是输出:

$ tail -f /var/log/syslog|grep "SIG_TIMED_WAITER"
Jan  7 15:39:41 localhost SIG_TIMED_WAITER[13275]: Started
Jan  7 15:43:36 localhost SIG_TIMED_WAITER[13275]: EAGAIN: TimedWait complete...
Jan  7 15:43:36 localhost SIG_TIMED_WAITER[13275]: Terminated.

我曾希望四秒钟后看到"EAGAIN:TimedWait完成..."记录.

I had expected to see "EAGAIN: TimedWait complete..." logged four seconds later.

我的代码是否有问题,或者是由于其他原因?请注意,我看不到它,例如,等待四分钟的select().

Is there something wrong with my code, or is this due to some other reason? Note that I do not see this with, for example, a select() that waits for four minutes.

推荐答案

LOG_LOCAL1是保留项. IE.不要使用

the LOG_LOCAL1 is a reserved item,. I.E. do not use it

代替使用LOG_USER

如果options参数也具有LOG_PERROR

It is easier to follow the action if the options parameter also has LOG_PERROR

然后,输出也将记录在stderr上.

Then the output will also be logged on stderr.

这是该程序的更正/工作版本.

Here is a corrected/working version of the program.

#define _GNU_SOURCE

#include <signal.h>
#include <syslog.h>
#include <stdarg.h>
#include <stddef.h>
#include <errno.h>


int main( void )
{
    setlogmask(LOG_UPTO(LOG_NOTICE));
    openlog ("SIG_TIMED_WAITER", LOG_CONS | LOG_PID | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
    syslog (LOG_NOTICE, "Started");

    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGUSR1);

    struct timespec to;
    to.tv_sec = 240;
    to.tv_nsec = 0;

    int ret = sigtimedwait(&set, NULL, &to);
    if(ret < 0)
    {
        if (errno == EAGAIN)
        {
            syslog (LOG_NOTICE, "EAGAIN: TimedWait complete...");
        }

        else
        {
            syslog (LOG_NOTICE, "ERROR!");
        }
    }

    else
    {
        syslog (LOG_NOTICE, "Interrupted by signum: %d.", ret);
    }

    syslog (LOG_NOTICE, "Terminated.");
    closelog();

    return 0;
} // end function: main

我正在运行ubuntu Linux 14.04

I'm running ubuntu linux 14.04

(在这种情况下,15212是运行程序的控制台/终端的PID.)

(in this case, the 15212 is the PID of the console /terminal where the program was run.)

从控制台/终端(从stderr输出)

from the console/terminal (from the stderr output)

SIG_TIMED_WAITER[15212]: Started
SIG_TIMED_WAITER[15212]: EAGAIN: TimedWait complete...
SIG_TIMED_WAITER[15212]: Terminated.

来自/var/log/syslog:

from /var/log/syslog:

Jan  7 05:50:07 rkwill-desktop SIG_TIMED_WAITER[15212]: Started
Jan  7 05:54:07 rkwill-desktop SIG_TIMED_WAITER[15212]: EAGAIN: TimedWait complete...
Jan  7 05:54:07 rkwill-desktop SIG_TIMED_WAITER[15212]: Terminated.

注意:初始输出和EAGAIN输出之间的时间为4分钟(240秒)

Notice: The time between the initial output and the EAGAIN output was 4 minutes (240 seconds)

这篇关于sigtimedwait()在超时之前返回EAGAIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆