C Function Psignal
# C Library Function - psignal()
The `psignal()` function is a utility function in C used to print a user-defined message followed by the standard description of a system signal to the standard error stream (`stderr`).
This function is defined in the `` header file and is highly useful for debugging and logging signal-handling events in Unix-like operating systems.
---
## Syntax
```c
void psignal(int signum, const char *message);
```
### Parameters
* **`int signum`**: The signal number (e.g., `SIGINT`, `SIGTERM`, `SIGSEGV`).
* **`const char *message`**: A user-defined prefix string. If this parameter is `NULL` or an empty string (`""`), only the standard signal description is printed, without any prefix.
### Return Value
* The `psignal()` function does not return any value (`void`).
---
## Output Format
The output is written to `stderr` in the following format:
```text
:
```
If `message` is `NULL` or empty, the output is simply:
```text
```
---
## Code Example
The following example demonstrates how to use `psignal()` inside a signal handler. The program registers a handler for the `SIGINT` signal (triggered by pressing `Ctrl+C`). When the signal is caught, `psignal()` prints the custom message along with the system's description of `SIGINT`.
```c
#include
#include
#include
// Signal handler function
void handle_sigint(int sig) {
// Print the signal description to stderr
psignal(sig, "Caught signal");
}
int main() {
// Register the signal handler for SIGINT
signal(SIGINT, handle_sigint);
// Infinite loop waiting for a signal
while (1) {
printf("Running... Press Ctrl+C to send SIGINT\n");
sleep(1);
}
return 0;
}
```
### Sample Output
When you run the program and press `Ctrl+C`, the output will look like this:
```text
Running... Press Ctrl+C to send SIGINT
Running... Press Ctrl+C to send SIGINT
Running... Press Ctrl+C to send SIGINT
^CCaught signal: Interrupt
```
### Code Explanation
1. **`signal(SIGINT, handle_sigint);`**: Registers `handle_sigint` as the callback function when the process receives a `SIGINT` signal.
2. **`psignal(sig, "Caught signal");`**: When `Ctrl+C` is pressed, the handler is invoked. `psignal` formats the output by appending the standard description of `SIGINT` ("Interrupt") to the custom prefix "Caught signal", resulting in `Caught signal: Interrupt`.
---
## Important Considerations
* **Standard Error Output**: `psignal()` always writes to `stderr`, not `stdout`. This ensures that diagnostic messages are visible even if standard output is redirected to a file or piped to another process.
* **Thread Safety and Async-Signal Safety**:
* In modern POSIX standards, `psignal()` is **not** guaranteed to be async-signal-safe. Calling non-async-signal-safe functions inside a signal handler can lead to undefined behavior (such as deadlocks if the signal interrupts a standard I/O operation).
* For production-grade, robust signal handling, it is generally recommended to set a volatile flag in the handler and handle the logging/printing within the main execution loop, or use strictly async-signal-safe functions like `write()`.
* **Portability**: While widely available on POSIX-compliant systems (like Linux and macOS), `psignal()` is not part of the ANSI C standard. If strict cross-platform compatibility (e.g., Windows) is required, consider using `strsignal()` combined with `fprintf()`, or standard conditional preprocessor directives.
YouTip