sigtimedwait() returns EAGAIN before timeout

狂风中的少年 提交于 2019-12-04 10:23:49

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

Instead use LOG_USER

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

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

I'm running ubuntu linux 14.04

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

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.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!