C library function -psignal()

C Standard Library - <signal.h>

Description

psignalThe function is a function in the C standard library, used to print the signal number and its corresponding description information to standard error output (stderr). This function is defined in<signal.h>In the header file.

Syntax

void psignal(int signum, const char *message);

Parameter

  • int signum: signal number.
  • const char *message: a user-defined message. If this message isNULLor an empty string, the function only prints the signal description information.

Return Value

psignalThe function has no return value.

Example

The following is an example usingpsignalAn example program demonstrating how the function prints signal description information. The program sets up a signal handler, and when it catchesSIGINTWhen a signal occurs, usepsignalThe function prints the signal number and description.

Example

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

// Signal handler
void handle_sigint(int sig) {
    psignal(sig, "Caught signal");
}

int main() {
    // Set the signal handler for SIGINT
    signal(SIGINT, handle_sigint);

    // Infinite loop, waiting for signal
    while (1) {
        printf("Running... Press Ctrl+C to send SIGINT\n");
        sleep(1);
    }

    return 0;
}

When the program capturesSIGINTWhen a signal occurs, callhandle_sigintSignal handler, and usespsignalThe function prints 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);: SettingsSIGINTThe signal handler ishandle_sigint。
  • psignal(sig, "Caught signal");: when caughtSIGINTWhen a signal occurs, usepsignalfunction printsCaught signal: Interrupt。
  • User-defined messageCaught signalAnd signal description informationInterruptAre printed together.

Notes

  • psignalThe function is used to print the signal number and its corresponding description information to standard error output (stderr).
  • User-defined messages can beNULLor an empty string, if so,psignalonly prints the signal description information.

C Standard Library - <signal.h>

other extensions