C Library Function - psignal()
Description
psignal is a function in the C standard library used to print a signal number and its corresponding descriptive message to the standard error output (stderr). This function is defined in header.
Syntax
void psignal(int signum, const char *message);
Parameters
int signum: The signal number.const char *message: A user-defined message. If this message isNULLor an empty string, the function prints only the signal description.
Return Value
psignal The function has no return value.
Example
The following is an example of using psignal An example program in which the function prints signal description information. The program sets up a signal handler that, when a SIGINT signal is caught, uses the psignal function to print the signal number and description.
Example
#include
#include
#include
// Signal handler
void
handle_sigint
(
int
sig
)
{
psignal
(
sig
,
"Caught signal"
)
;
}
int
main
(
)
{
// Set up the signal handler for SIGINT
signal
(
SIGINT
,
handle_sigint
)
;
// Infinite loop, waiting for the signal
while
(
1
)
{
printf
(
"Running... Press Ctrl+C to send SIGINT\n"
)
;
sleep
(
1
)
;
}
return
0
;
}
When the program catches a SIGINT signal, it calls the handle_sigint signal handler, and uses psignal function to print the signal number and description.
Running... Press Ctrl+C to send SIGINT
Running... Press Ctrl+C to send SIGINT
Running... Press Ctrl+C to send SIGINT
Caught signal: Interrupt
Explanation
signal(SIGINT, handle_sigint);: set theSIGINTThe signal handler forhandle_sigint.psignal(sig, "Caught signal");: When aSIGINTsignal is caught, uses thepsignalsignal is caught, theCaught signal: Interrupt.- user-defined message
Caught signaland the signal descriptionInterruptare printed together.
Notes
psignalThe function is used to print a signal number and its corresponding description to the standard error output (stderr).- The user-defined message can be
NULLor an empty string; if so,psignalonly the signal description is printed.
C Standard Library -