Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function – sigprocmask()

C Library Function - sigprocmask()

C Standard Library - <signal.h> C Standard Library -

Description

In C language,sigprocmaskThe function is used to examine or change the signal mask of the current process. The signal mask determines which signals are blocked during masking, that is, temporarily not processed.

Syntax

int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

Parameters

- `SIG_BLOCK`: add the signals in the signal set pointed to by `set` to the current signal mask. - `SIG_UNBLOCK`: remove the signals in the signal set pointed to by `set` from the current signal mask. - `SIG_SETMASK`: replace the current signal mask with the signal set pointed to by `set`.

Return Value

Example

The following is a simple example program demonstrating how to use the sigprocmask function to block SIGINT signal, then unblock and view the previous signal mask:

Example

#include

#include

#include

int

main

(

)

{

    sigset_t new_mask

,

old_mask

;

   

   

// initialize the new signal set to contain only SIGINT

    sigemptyset

(

&

new_mask

)

;

    sigaddset

(

&

new_mask

,

SIGINT

)

;

   

// set the new signal mask to block the SIGINT signal

   

if

(

sigprocmask

(

SIG_BLOCK

,

&

new_mask

,

&

old_mask

)

==

1

)

{

       

perror

(

"sigprocmask"

)

;

       

return

1

;

   

}

   

printf

(

"SIGINT signal is blocked. Sleeping for 5 seconds...\n"

)

;

    sleep

(

5

)

;

   

// unblock SIGINT

   

if

(

sigprocmask

(

SIG_UNBLOCK

,

&

new_mask

,

NULL

)

==

1

)

{

       

perror

(

"sigprocmask"

)

;

       

return

1

;

   

}

   

printf

(

"SIGINT signal is unblocked.\n"

)

;

   

return

0

;

}

Output result

Running the above example program may produce the following output:



SIGINT signal is blocked. Sleeping for 5 seconds...

During this period, the SIGINT signal is blocked, so the program waits for 5 seconds.



SIGINT signal is unblocked.

Then, the program unblocks SIGINT signal and resumes normal handling of the signal.

Notes

C Standard Library - <signal.h> C Standard Library -