【问题标题】:c check signals every n secondsc 每 n 秒检查一次信号
【发布时间】:2018-12-10 18:25:37
【问题描述】:

我很想写一个程序,每 5 秒检查一次接收到的所有信号,并根据它们改变其输出。 我的主要问题是让代码实际上等待 5 秒,因为一旦处理信号,函数 sleep() 就会被中断。 我试图做的是将程序分成两个进程,只有孩子处理信号,并通过管道进行通信。 问题是,由于管道阻​​塞,在前 5 秒之后,父母只会尝试在那里阅读,基本上使睡眠无用。

理论上,我应该能够仅使用我包含的库(或替代 pthread 库,这是一个分配)来解决此类问题,否则我看到我可以使用其他库来使管道不阻塞,但是在那一刻我对如何解决这个问题一无所知(我也尝试了一次失败的线程尝试)。

我附上代码供参考,在此先感谢。

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int p[2];//pipe

void sigHandler (int sig) {
    char c;
    switch (sig) {
        case SIGUSR1:
            c = '1';
        break;
        case SIGUSR2:
            c = '2';
        break;
        case SIGALRM:
            c = '0';
        break;
        case SIGSTOP:
            exit (0);
        break;
    }
    write(p[1], &c, sizeof(char));
}


int main(int argc, char **argv) {
    int signals[3], sig, i;
    for (i=0; i< 3; i++) {
        signals[i] =0;
    }
    pid_t pid;
    char csig;
    pipe(p);

    /*fork code*/
    pid = fork();

    /*parent*/
    if (pid) { 
        close(p[1]);
        int proceed = 1;

        /*cycle keeps on indefinitely*/
        while (proceed) {
            sleep(15); //increased the time so I have time to send signals
            while (read(p[0], &csig, sizeof(char)) > 0) {
                sig=atoi(&csig);
                signals[sig]++;

                /*if alternating sigals, error*/
                if (signals[1] && signals[2]) {
                    printf("Error: alternating SIGUSR1 and SIGUSR2\n%d\t%d\n", signals[1], signals[2]);

                }
                /*if 2 consecutive signals, success*/
                if (signals[sig] == 2) {
                    printf("Success: two consecutive SIGUSR1 or SIGUSR2\n");
                }
                /*if three consecutive signals, exit*/
                if (signals[sig] == 3) {
                    kill(pid, SIGSTOP);
                    proceed = 0;
                    break;
                }
                /*make 0 any non repeating signal != sig*/
                for(i = 0; i< 3; i++) {
                    if (i != sig) {
                        signals[i] = 0;
                    }
                }
            }
        }
    /*child*/
    } else {
        close(p[0]);
        signal(SIGUSR1, sigHandler);
        signal(SIGUSR2, sigHandler);
        signal(SIGALRM, sigHandler);
        signal(SIGSTOP, sigHandler);
        while (1) {
            sleep(5); //no need to do anything else
        }
        exit(1);

    }
    return 0;
}

【问题讨论】:

  • 为什么需要单独的子进程?在同一过程中,如果您收到信号,只需将其添加到队列中并在剩余的时间内恢复睡眠。您可以轻松检查您的睡眠是否被打断或结束。检查sleep的返回值
  • 如果您只想每 5 秒处理一次信号,可以在 sleep(5) 前后调用 sigsetmask 或 sigprocmask。

标签: c signals sleep


【解决方案1】:
  1. 计算五秒后的时间。存储这个时间。
  2. 计算距离您在步骤 1 中计算的时间还有多长时间。
  3. 睡那么久。
  4. 检查时间。
  5. 如果它是在或超过您在第 1 步中存储的时间,那么您就完成了。
  6. 转到第 2 步。

【讨论】:

  • Sleep 如果被打断,也会返回剩余的数量。无需再计算。
  • @AjayBrahmakshatriya 但这四舍五入到最接近的秒数,对于五秒的睡眠可能不够准确。即使您只使用上述算法的第二精度,至少多次中断的错误不会累积。但如果中断很少见且准确性不是特别重要,那绝对是一种选择。
  • @AjayBrahmakshatriya:它也不包括在信号处理程序中花费的时间(或唤醒进程以运行信号处理程序的时间,或睡眠之外的其他开销)。因此,如果传递了很多信号,您最终可能会等待明显比所需时间长得多。,,
【解决方案2】:

clock_nanosleep 让你睡到一个绝对的时间。您可以重复调用此函数,直到达到所需的时间。

struct timespec time_now;
clock_gettime(CLOCK_REALTIME, &time_now);
time_now.tv_sec += 5;
time_now.tv_nsec = 0;
while(clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &time_now, NULL) == EINTR);

这将使您的线程反复休眠,直到完成 5 秒的休眠。 在您的信号处理程序中,您不断将接收到的信号添加到一个数组中,并在睡眠结束后处理它。

【讨论】:

  • 实际处理中断 nanosleep 调用的信号所花费的时间不包括(减去)返回的剩余时间,所以如果有很多信号,这可能是相当不准确的。跨度>
  • @ChrisDodd 我认为这样做的开销会很低,因为信号处理程序会很短。无论如何,我将答案改为使用绝对时间。
【解决方案3】:

为什么这么复杂?信号是每个进程的(并且在使用 POSIX 线程时,可以选择在同一进程中的每个线程),因此仅使用多个进程来处理信号是没有意义的。

考虑以下程序:

#define  _POSIX_C_SOURCE  200809L
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

static const char *signal_name(const int signum)
{
    switch (signum) {
    case SIGUSR1: return "SIGUSR1";
    case SIGUSR2: return "SIGUSR2";
    case SIGALRM: return "SIGALRM";
    case SIGSTOP: return "SIGSTOP";
    case SIGINT:  return "SIGINT";
    default:      return "unnamed";
    }
}

int main(void)
{
    siginfo_t  info;
    sigset_t   mask;
    int        signum;

    sigemptyset(&mask);
    sigaddset(&mask, SIGUSR1);
    sigaddset(&mask, SIGUSR2);
    sigaddset(&mask, SIGALRM);
    sigaddset(&mask, SIGSTOP);
    sigaddset(&mask, SIGINT);

    /* Block the signals in mask. */
    if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
        fprintf(stderr, "Cannot block signals: %s.\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    printf("Process %d is ready for USR1 and USR2 signals.\n", (int)getpid());
    fflush(stdout);

    /* Ensure we receive a SIGALRM signal in five seconds or so. */
    alarm(5);

    while (1) {

        /* Receive one of the signals in mask. */ 
        signum = sigwaitinfo(&mask, &info);
        if (signum == -1) {
            const int  err = errno;

            /* EINTR should not happen, but if it did, it would be okay. */
            if (err == EINTR)
                continue;

            fprintf(stderr, "sigwaitinfo() failed: %s (%d).\n", strerror(err), err);
            exit(EXIT_FAILURE);
        }

        /* Describe the received signal. */
        if (info.si_pid)
            printf("Received %s (signal %d) from process %d.\n", signal_name(signum), signum, (int)info.si_pid);
        else
            printf("Received %s (signal %d).\n", signal_name(signum), signum);
        fflush(stdout);

        /* It is time to exit, if we receive INT or STOP, */
        if (signum == SIGINT || signum == SIGSTOP)
            break;

        /* or an ALRM timeout; don't be fooled by kill()/sigqueue() SIGALRM. */
        if (signum == SIGALRM && !info.si_pid)
            break;
    }

    printf("Done.\n");
    return EXIT_SUCCESS;
}

mask 包含一组被阻止且仅通过sigwaitinfo() 接收的信号。 alarm(5) 确保 SIGALRM 信号在五秒左右的时间内送达。如果设置了info.si_pid,则信号是通过kill()(或类似的特定于操作系统的系统调用)发送的。

这个程序只描述它接收到的每个信号,直到触发警报超时,或者它接收到一个 SIGSTOP 或 SIGINT 信号。 (当您在终端中运行程序并按下 Ctrl+C 时,终端会向(前台)进程发送 SIGINT 信号。)

你不能通过发送 SIGALRM 信号来欺骗程序,因为内核将siginfo_t 结构的.si_pid 字段设置为发送进程;对于所有“真正的”警报,它将为零。

请记住,标准 Unix/POSIX 信号没有排队,并且可能有延迟(如睡眠和计时器,可能会比预期的延迟交付)。

【讨论】:

    猜你喜欢
    • 2014-04-15
    • 2018-01-06
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多