经常发出警报()和暂停()

时间:2017-06-02 16:14:26

标签: c signals alarm pause

我的任务是:

编写一个程序,其中父进程正好创建一个子进程。创建子项后,进程的行为如下:它每5秒向子项发送一次信号SIGUSR1。要实现此行为,父进程大多使用以下系统调用:alarm()pause()。在发送信号SIGUSR1三次之后,第四次将信号SIGUSR2发送给孩子。在此之后,父母等待孩子完成。

的行为如下:它等待任何信号中断。如果收到的信号是SIGUSR1,它会向标准输出打印一条消息。如果收到的信号是SIGUSR2,那么它就完成了。此外,在执行的前5秒内,应阻止信号SIGUSR2。学生应该在手册页中检查alarm()pause()的行为。

我的解决方案看起来像这样。我试图忽略父级中的alarm(),并通过子级中的警报将标志设置为true并更改取消阻止SIGUSR2

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

bool flag = false;

void alert_ignore(int signum) {
    printf("catched alarm\n");
    return;
}

void alert_setting_flag(int signum) {
    flag = true;
    return;
}

void sigusr1_handler(int signum) {
    printf("Recieved SIGUSR1\n");
    return;
}

void sigusr2_handler(int signum) {
    printf("Recieved SIGUSR2\n");
    exit(0);
}


int main(int argc, char *argv[]) {

    sigset_t set;
    if (sigemptyset(&set) == -1) {
        perror("sigemptyset");
        exit(1);
    }
    if (sigaddset(&set, SIGUSR2) == -1) {
        perror("sigaddset");
        exit(1);
    }

    if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
        perror("sigprocmask");
        exit(1);
    }

    struct sigaction alert_action;
    alert_action.sa_handler = alert_ignore;
    alert_action.sa_mask = set;
    if (sigaction(SIGALRM, &alert_action, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    struct sigaction sigusr1_action;
    sigusr1_action.sa_handler = sigusr1_handler;
    sigusr1_action.sa_mask = set;
    if (sigaction(SIGUSR1, &sigusr1_action, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    struct sigaction sigusr2_action;
    sigusr2_action.sa_handler = sigusr2_handler;
    if (sigaction(SIGUSR2, &sigusr2_action, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        exit(1);
    }

    if (pid == 0) {
        /* Child process duties */
        /* Setting alert handler to turn flag form false to true */
        alert_action.sa_handler = alert_setting_flag;
        if (sigaction(SIGALRM, &alert_action, NULL) == -1) {
            perror("sigaction");
            exit(1);
        }

        alarm(5);
        int count = 0;
        while (true) {
            pause();
            if (flag == true && !count) {
                if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
                    perror("sigprocmask");
                    exit(1);
                }
                printf("SIGUSR2 unblocked\n");
                count++;
            }
        }
    }

    /* Parent Process duties */
    for (int i = 0; i < 3; ++i) {
        alarm(5);
        pause();
        kill(pid, SIGUSR1);
    }
    kill(pid, SIGUSR2);
    wait(NULL);

    return 0;
}

这样做有效,但有时候我会得到很多随机行为,如下所示,我不知道为什么。

Case(0) desired behavior
catched alarm           // after 5 seconds
SIGUSR2 unblocked
Recieved SIGUSR1
catched alarm           // after 10 seconds
Recieved SIGUSR1
catched alarm           // after 15 seconds
Recieved SIGUSR1
Recieved SIGUSR2


Case 1 (a lot):
catched alarm            // after 5 seconds
Recieved SIGUSR1
SIGUSR2 unblocked
Alarm clock              // after 10 seconds

Case 2 (very rare) needed to terminate it:
catched alarm // after 5 seconds
catched alarm // after 10 seconds
catched alarm // after 15 seconds
^Z

Case 3 (double printing SIGUSR2 unblocked):
catched alarm           // after 5 seconds
SIGUSR2 unblocked
SIGUSR2 unblocked
Recieved SIGUSR1
catched alarm           // after 10 seconds
Recieved SIGUSR1
catched alarm           // after 15 seconds
Recieved SIGUSR1
Recieved SIGUSR2

Case 4:
catched alarm           // after 5 seconds
Alarm clock

这种行为的原因是什么? (对我来说最重要的部分是,为什么不按需要忽略SIGALRM,我知道有些问题我没有以原子方式设置标志,但这不应该影响我的父进程,不是吗?)

1 个答案:

答案 0 :(得分:2)

你有:

struct sigaction alert_action;
alert_action.sa_handler = alert_ignore;
alert_action.sa_mask = set;
if (sigaction(SIGALRM, &alert_action, NULL) == -1) {

这里,alert_action.sa_flags未初始化也未分配,因此它将是一个随机整数。可能会发生许多不好的事情。如果SA_RESETHAND标志打开,则在收到第一个SIGALRM后,信号操作将重置为默认值,下一个SIGALRM将终止该进程。这就是为什么你有时会看到shell打印出Alarm clock

要解决此问题,请初始化结构或为所有必填字段指定赋值语句。例如:

struct sigaction alert_action = {
        .sa_handler = alert_ignore,
        .sa_mask = set,
        .sa_flags = 0
};

您应该为所有struct sigaction变量执行此操作。

相关问题