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

C Library Function – sigpending()

C Library Function - sigpending()

sigpending The function is a function in the C standard library, used to get the pending (unhandled) signal set of the current process. A pending signal is one that has been blocked but not yet handled. This function is defined in header.

Syntax



int sigpending(sigset_t *set);

Parameters

Return Value

Example

The following is an example of using sigpending Example program demonstrating the function getting the pending signal set of the current process. The program blocks SIGINT signal, then sends the signal, and uses sigpending checks whether the signal is in the pending signal set.

Example

#include

#include

#include

void

handle_sigint

(

int

sig

)

{

   

printf

(

"Caught signal %d\n"

,

sig

)

;

}

int

main

(

)

{

    sigset_t new_mask

,

old_mask

,

pending

;

   

// set the handler for SIGINT

    signal

(

SIGINT

,

handle_sigint

)

;

   

// initialize the signal set and block the SIGINT signal

    sigemptyset

(

&

new_mask

)

;

    sigaddset

(

&

new_mask

,

SIGINT

)

;

   

if

(

sigprocmask

(

SIG_BLOCK

,

&

new_mask

,

&

old_mask

)

==

1

)

{

       

perror

(

"sigprocmask"

)

;

       

return

1

;

   

}

   

// send the SIGINT signal

   

raise

(

SIGINT

)

;

   

// get the pending signal set

   

if

(

sigpending

(

&

pending

)

==

1

)

{

       

perror

(

"sigpending"

)

;

       

return

1

;

   

}

   

// check whether SIGINT is in the pending signal set

   

if

(

sigismember

(

&

pending

,

SIGINT

)

)

{

       

printf

(

"SIGINT is pending\n"

)

;

   

}

else

{

       

printf

(

"SIGINT is not pending\n"

)

;

   

}

   

// restore the original signal mask

   

if

(

sigprocmask

(

SIG_SETMASK

,

&

old_mask

,

NULL

)

==

1

)

{

       

perror

(

"sigprocmask"

)

;

       

return

1

;

   

}

   

return

0

;

}

After compiling and running the program, the output will be:



SIGINT is pending

Caught signal 2

The program first blocks SIGINT signal, then sends the signal, and uses sigpending Checks SIGINT whether it is in the pending signal set. Finally, the program restores the original signal mask, and catches the SIGINT signal and executes the signal handler.

Parsing

Notes

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