Skip to content
Snippets Groups Projects
Select Git revision
  • 65df02fc0b0921c826165732fd27b55751fe5c34
  • master default protected
2 results

prog.c

Blame
  • prog.c 2.23 KiB
    #include <signal.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    volatile sig_atomic_t count = 0;
    struct sigaction usr1;
    
    static void handler_usr1(int __attribute__((unused)) signum) {
        // if (sigismember(&usr1.sa_mask, SIGTSTP) == 1) {
        //     fprintf(stdout, "SIGTSTP will be unblocked...\n");
        //
        //     if (sigdelset(&usr1.sa_mask, SIGTSTP) == -1) {
        //         perror("sigdelset");
        //     }
        // } else if (sigismember(&usr1.sa_mask, SIGTSTP) == 0) {
        //     fprintf(stdout, "SIGTSTP will be blocked...\n");
        //
        //     if (sigaddset(&usr1.sa_mask, SIGTSTP) == -1) {
        //         perror("sigaddset");
        //     }
        // } else {
        //     perror("sigismember");
        // }
    
        if (sigismember(&usr1.sa_mask, SIGTSTP) == 1) {
            fprintf(stdout, "SIGTSTP will be unblocked...\n");
            if (sigprocmask(SIG_UNBLOCK, &usr1.sa_mask, NULL) == -1) {
                perror("sigprocmask");
            }
            sigaddset(&usr1.sa_mask, SIGTSTP);
        } else if (sigismember(&usr1.sa_mask, SIGTSTP) == 0) {
            fprintf(stdout, "SIGTSTP will be blocked...\n");
    
            sigaddset(&usr1.sa_mask, SIGTSTP);
            if (sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL) == -1) {
                perror("sigprocmask");
            }
        } else {
            perror("sigismember");
        }
    }
    
    // static void handler_usr2(int signum) {
    //     // fprintf(stdout, "Ctrl-C has been pressed %d times\n", counter);
    // }
    
    int main(void) {
        usr1.sa_handler = handler_usr1;
        usr1.sa_flags = 0;
        sigemptyset(&usr1.sa_mask);
    
        sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL);
    
        if (sigaddset(&usr1.sa_mask, SIGTSTP) == -1) {
            perror("sigaddset");
        }
    
        if (sigprocmask(SIG_SETMASK, &usr1.sa_mask, NULL) == -1) {
            perror("sigprocmask");
            exit(EXIT_FAILURE);
        }
    
        if (sigaction(SIGUSR1, &usr1, NULL) == -1) {
            perror("sigaction");
            exit(EXIT_FAILURE);
        }
    
        // struct sigaction usr2;
        // usr2.sa_handler = handler_usr2;
        // usr2.sa_flags = 0;
        // sigemptyset(&usr2.sa_mask);
    
        // if (sigaddset(&usr2.sa_mask, SIGUSR2) == -1) {
        //     perror("sigaddset");
        // }
    
        while (true) {
            fprintf(stdout, "hello world\n");
            sleep(1);
        }
    
        return EXIT_SUCCESS;
    }