Welcome, guest | Sign In | My Account | Store | Cart

http://www-h.eng.cam.ac.uk/help/tpl/unix/signals.html

do you want certain signals to be ignored or blocked? The sigaction(), sigprocmask(), siginterrupt(), and sigsuspend() functions control the manipulation of the signal mask, which defines the set of signals currently blocked. The manual pages give details. The following code shows how the response to signals can be delayed.

C, 55 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

void divert (int sig) {
    printf("signal received=%d\n",sig);
}

int main() {
    sigset_t mask, pending;

    if (signal(SIGINT, divert)== SIG_ERR) {
        perror("signal(SIGINT, divert) failed");
        exit(1);
    }
    printf("going to sleep for 5 secs, during which Ctrl-C will wake up the process\n");
    sleep(5);

    sigemptyset(&mask);
    sigaddset(&mask,SIGINT);
    if(sigprocmask(SIG_BLOCK, &mask, 0) < 0) {
        perror("sigprocmask");
        exit(1);
    }

    printf("sleeping again for 5 secs, delaying the response to Ctrl-C\n");
    sleep(5);

    if(sigpending(&pending) <0) {
        perror("sigpending");
        exit(1);
    }

    if(sigismember(&pending, SIGINT))
        printf("SIGINT pending\n");

    if(sigprocmask(SIG_UNBLOCK,&mask,0) < 0) {
        perror("sigblockmask");
        exit(1);
    }

    printf("SIGINT unblocked\n");
}

/*
going to sleep for 5 secs, during which Ctrl-C will wake up the process
signal received=2
sleeping again for 5 secs, delaying the response to Ctrl-C
SIGINT pending
signal received=2
SIGINT unblocked

*/

1 comment

J Y (author) 12 years, 6 months ago  # | flag

The sigpending() function shall store, in the location referenced by the set argument, the set of signals that are blocked from delivery to the calling thread and that are pending on the process or the calling thread.

Created by J Y on Mon, 25 May 2009 (MIT)
C recipes (32)
J Y's recipes (21)

Required Modules

  • (none specified)

Other Information and Tasks