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

C Library Function - psignal()

C Library Function - psignal()

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

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

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

Notes

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