簡體   English   中英

嗨,我正在為此硬件編碼,使用信號(sigaction,sigprocmask)

[英]Hi I am coding for this hw, using signal(sigaction, sigprocmask)

#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 信號的數量,一旦達到 10 秒就“阻止”SIGINT 信號,一旦達到 20 秒就“取消阻止”和“忽略”SIGINT 信號它的生命,在 30 秒后終止。

只有前 10 秒有效。 第二個 10 秒似乎不算 SIGINT(我不確定如何“忽略”信號)。 要求是使用sigactionsigpromask

我已經修復了我的熱門評論中提到的問題。

time_duration從 30倒數到 0。但是,如果它從 0 倒數30,它會更接近問題陳述[並使邏輯更容易]。 所以,從這里開始,我將參考那個區間

在 10-20 間隔中,您設置newmask ,但直到 20-30 間隔才在sigprocmask中使用它。 我認為你應該立即使用口罩。

在 20-30 間隔,我們必須恢復原始掩碼,但我們需要另一個sigaction調用, sa_handler設置為SIG_IGN

你有sigpending(&pendmsk); 但不要對它做任何事情[而且它並不是真正需要的]。

由於我在頂級評論中提到的信號安全要求,所有打印都應該在信號處理程序之外完成。 而且,這需要在num_count上使用volatile [因此基本級別將使用最新值]。


我添加了一個標志,允許在進入新的間隔/時代時執行一次給定的操作。

我添加了一個kill系統調用來模擬用戶執行ctrl-c並將信號更改為SIGTERM ,因此真正的SIGINT將終止程序 [方便調試]。 您可以將其還原為生產。

我更改了一些變量名稱以更具描述性。

這是重構的代碼:

#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;
}

這是程序 output:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM