zsloan112
zsloan112

Reputation: 43

Blocking and Unblocking SIGINT and SIGQUIT

I am writing a program that has two for loops and during the first for loop (just prints 1-5 and sleeps 1 second in between) SIGINT and SIGQUIT are to be blocked and in the second for loop only SIGINT is to be blocked. I successfully block the correct signals for the first for loop, then on the second for loop, I try to remove SIGQUIT from being blocked but both signals still seem to be blocked.

Here is what I have as of right now:

#include <unistd.h>
#include <stdio.h>
#include <wait.h>
#include <signal.h>
#include <string.h>


void hd1(int sig){
    if(sig == SIGINT){
        printf(" SIGINT Recieved\n");
    }else if(sig == SIGQUIT){
        printf(" SIGQUIT Recieved\n");
    }
}


int main(){
    sigset_t new_set;
    sigset_t old_set;

    //struct sigaction act;
    //memset(&act, 0, sizeof(act));

    //act.sa_handler = hd1;

    //sigaction(SIGINT, &act, 0);
    //sigaction(SIGQUIT, &act, 0);

    sigemptyset(&new_set);
    sigaddset(&new_set, SIGINT);
    sigaddset(&new_set, SIGQUIT);

    sigprocmask(SIG_BLOCK, &new_set, &old_set);

    for(int i = 1; i <= 5; i++){
        printf("%d\n", i);
        sleep(1);
    }


    sigdelset(&new_set, SIGQUIT);


    for(int i = 1; i <= 5; i++){
        printf("%d\n", i);
        sleep(1);
    }
}

I have tried using sigprocmask to unblock and setmask to reset the mask but it terminates the program after the loop when I unblock the signal.

Upvotes: 0

Views: 3650

Answers (1)

Stargateur
Stargateur

Reputation: 26767

Just use sigprocmask(); a second time. And use SIG_SETMASK

SIG_SETMASK The set of blocked signals is set to the argument set.

sigdelset(&new_set, SIGQUIT);
sigprocmask(SIG_SETMASK, &new_set, NULL);

Upvotes: 3

Related Questions