首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嗨,我正在为这个硬件编码,使用signal(sigaction,sigprocmask)

嗨,我正在为这个硬件编码,使用signal(sigaction,sigprocmask)
EN

Stack Overflow用户
提问于 2021-03-13 02:39:10
回答 1查看 52关注 0票数 2
代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>

unsigned time_duration;
int num_count=0;

void sig_handling(int sig){
    if(sig==SIGINT){
        num_count++;
        printf("The number of SIGINT signals are %d\n", num_count);
    }
 } 


int main(int argc, char *argv[])
{
    unsigned time_stamp1=(unsigned)time(NULL);
    time_stamp1=time_stamp1+30;
    sigset_t newmask;
    sigset_t oldmask;
    sigset_t pendmask;
    struct sigaction act;
    act.sa_handler=&sig_handling;
act.sa_flags=SA_RESTART;
sigaction(SIGINT, &act, NULL);
while(1){
    unsigned time_stamp2=(unsigned)time(NULL);
    time_duration=time_stamp1-time_stamp2;
    if(time_duration>20&&time_duration<30)
    {
        sigemptyset(&newmask);
        sigaddset(&newmask, SIGINT);
    }
    if(time_duration>10&&time_duration<20)
    {
        sigprocmask(SIG_BLOCK, &newmask, &oldmask);
    }
    if(time_duration>0&&time_duration<10)
    {
        sigpending(&pendmask);
        sigprocmask(SIG_SETMASK, &oldmask, NULL);
        
    }
    if(time_duration==0)
    {
        break;
    }
}
return 0;

}

要求计数并打印在前10秒内接收到的SIGINT信号的数量,一旦SIGINT信号达到其生命周期的10秒,则对其进行“阻止”,一旦SIGINT信号达到其生命周期的20秒,则对其进行“解除阻止”和“忽略”,并在30秒后终止。

只有前10秒有效。第二个10秒似乎不计入SIGINT (我不确定如何“忽略”该信号)。要求是使用sigactionsigpromask

EN

回答 1

Stack Overflow用户

发布于 2021-03-13 03:55:53

我已经修复了我的热门评论中提到的问题。

time_duration从30倒数到0。但是,如果从0到30递增,它将更接近问题陈述,并使逻辑更容易。因此,从现在开始,我将引用该间隔

在10-20时间间隔中,您设置了newmask,但是直到20-30时间间隔,您才在sigprocmask中使用它。我觉得你应该马上戴上口罩。

在20-30间隔,我们必须恢复原始掩码,但我们需要另一个将sa_handler设置为SIG_IGNsigaction调用

你已经有了sigpending(&pendmsk);,但是不要对它做任何事情,它并不是真正需要的。

由于我在顶部注释中提到的信号安全要求,所有打印都应该在信号处理程序之外完成。而且,这需要在num_count上使用volatile,所以基本级别将使用最新的值。

我添加了一个标志,它允许在进入新的时间间隔/era时执行一次给定的操作。

我已经添加了一个kill系统调用来模拟用户执行ctrl-c,并将信号更改为SIGTERM,因此真正的SIGINT将终止程序,这在调试中很方便。您可以将其恢复为生产环境。

我更改了一些变量名以使其更具描述性。

下面是重构后的代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

time_t time_duration;
volatile long long num_count = 0;

#define SIGSELF     SIGTERM

void
sig_handling(int sig)
{
    if (sig == SIGSELF) {
        num_count++;
#if 0
        printf("The number of SIGINT signals are %d\n", num_count);
#endif
    }
}

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

    time_t time_begin = time(NULL);

    sigset_t newmask;
    sigset_t oldmask;
    struct sigaction act;

#if 1
    sigaction(SIGSELF,NULL,&act);
#endif
    act.sa_handler = sig_handling;
    act.sa_flags = SA_RESTART;
    sigaction(SIGSELF, &act, NULL);

    pid_t pid = getpid();
    time_t time_msg = -1;

    long long kill_count = 0;

    int interval_count[10] = { 0 };

    while (1) {
        kill(pid,SIGSELF);
        ++kill_count;

        time_t time_current = time(NULL);
        time_duration = time_current - time_begin;

        if (time_duration != time_msg) {
            time_msg = time_duration;
            printf("At %ld seconds the number of signals are %lld",
                time_duration,num_count);
            printf(" (kills %lld)",kill_count);
            printf("\n");
        }

        time_t time_switch = time_duration / 10;

        int curstate = interval_count[time_switch];

        if (curstate == 0)
            printf("New interval %ld\n",time_switch);

        switch (time_switch) {
        case 0:  // 0-10
            break;

        case 1:  // 10-20
            if (curstate == 0) {
                sigemptyset(&newmask);
                sigaddset(&newmask, SIGSELF);
                sigprocmask(SIG_BLOCK, &newmask, &oldmask);
            }
            break;

        case 2:  // 20-30
            if (curstate == 0) {
                act.sa_handler = SIG_IGN;
                sigaction(SIGSELF, &act, NULL);
                sigprocmask(SIG_SETMASK, &oldmask, NULL);
            }
            break;
        }

        interval_count[time_switch] = curstate + 1;

        if (time_duration > 30)
            break;
    }

    return 0;
}

下面是程序的输出:

代码语言:javascript
复制
At 0 seconds the number of signals are 1 (kills 1)
New interval 0
At 1 seconds the number of signals are 487973 (kills 487973)
At 2 seconds the number of signals are 1021629 (kills 1021629)
At 3 seconds the number of signals are 1560710 (kills 1560710)
At 4 seconds the number of signals are 2100419 (kills 2100419)
At 5 seconds the number of signals are 2647694 (kills 2647694)
At 6 seconds the number of signals are 3192151 (kills 3192151)
At 7 seconds the number of signals are 3731573 (kills 3731573)
At 8 seconds the number of signals are 4273751 (kills 4273751)
At 9 seconds the number of signals are 4817509 (kills 4817509)
At 10 seconds the number of signals are 5360348 (kills 5360348)
New interval 1
At 11 seconds the number of signals are 5360348 (kills 6948004)
At 12 seconds the number of signals are 5360348 (kills 8523401)
At 13 seconds the number of signals are 5360348 (kills 10074263)
At 14 seconds the number of signals are 5360348 (kills 11432598)
At 15 seconds the number of signals are 5360348 (kills 12950546)
At 16 seconds the number of signals are 5360348 (kills 14533591)
At 17 seconds the number of signals are 5360348 (kills 15990611)
At 18 seconds the number of signals are 5360348 (kills 17410250)
At 19 seconds the number of signals are 5360348 (kills 18986650)
At 20 seconds the number of signals are 5360348 (kills 20552985)
New interval 2
At 21 seconds the number of signals are 5360348 (kills 22138819)
At 22 seconds the number of signals are 5360348 (kills 23743963)
At 23 seconds the number of signals are 5360348 (kills 25265593)
At 24 seconds the number of signals are 5360348 (kills 26802154)
At 25 seconds the number of signals are 5360348 (kills 28349228)
At 26 seconds the number of signals are 5360348 (kills 29926249)
At 27 seconds the number of signals are 5360348 (kills 31509756)
At 28 seconds the number of signals are 5360348 (kills 33100829)
At 29 seconds the number of signals are 5360348 (kills 34688121)
At 30 seconds the number of signals are 5360348 (kills 36274367)
New interval 3
At 31 seconds the number of signals are 5360348 (kills 37870874)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66605490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档