Ch20.12 โ€“ strsignal(), psignal() & sys_siglist

 

Ch20.12 โ€“ strsignal(), psignal() & sys_siglist
Linux System Programming ยท EmbeddedPathashala
๐Ÿ“ก Topic 12 of 19
๐ŸŽฏ Beginner
๐Ÿ’ป 2 Code Examples
โ“ Interview Q&A
๐Ÿ”‘ Key Terms
strsignal() psignal() sys_siglist Locale-sensitive NSIG Bounds checking
Printing Signal Descriptions

Every signal has a human-readable description string. Three ways to access them:

Function/Array Use Case Locale-aware?
strsignal(sig) Get description string with bounds check Yes
psignal(sig, msg) Print “msg: description” to stderr Yes
sys_siglist[sig] Direct array access (no bounds check) No

Prefer strsignal() over sys_siglist because it does bounds checking and is locale-sensitive.

๐Ÿ’ป Code Example 1 โ€“ strsignal() to Print Signal Descriptions
/* strsignal() returns human-readable signal description
   Compile: gcc -D_GNU_SOURCE -o strsig strsig.c
   Run: ./strsig */

#define _GNU_SOURCE
#include <stdio.h>
#include <signal.h>
#include <string.h>

int main(void)
{
    int sig;

    printf("Signal descriptions on this system:\n\n");

    for (sig = 1; sig < NSIG; sig++) {
        const char *desc = strsignal(sig);
        if (desc != NULL)
            printf("Signal %2d: %s\n", sig, desc);
    }

    /* Using strsignal for an invalid signal number */
    printf("\nInvalid signal 200: %s\n", strsignal(200));

    return 0;
}
๐Ÿ’ป Code Example 2 โ€“ psignal() and sys_siglist
/* psignal() prints to stderr; sys_siglist is the raw array
   Compile: gcc -D_BSD_SOURCE -o psig psig.c
   Run: ./psig */

#define _BSD_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <signal.h>

/* sys_siglist is declared as: extern const char *const sys_siglist[]; */
extern const char *const sys_siglist[];

int main(void)
{
    /* psignal prints: "my_msg: Interrupt" to stderr */
    psignal(SIGINT,  "Received SIGINT");
    psignal(SIGSEGV, "Caught SIGSEGV");
    psignal(SIGTERM, "Got SIGTERM");

    /* Direct sys_siglist access โ€” no bounds check! */
    printf("\nUsing sys_siglist directly:\n");
    printf("SIGFPE description: %s\n", sys_siglist[SIGFPE]);
    printf("SIGKILL description: %s\n", sys_siglist[SIGKILL]);

    return 0;
}
Note: psignal() writes to stderr, so redirect to see it: ./psig 2>&1 | less
โ“ Interview Questions
Q1. Why prefer strsignal() over sys_siglist[]?
strsignal() performs bounds checking (won’t access out-of-range memory for invalid signal numbers) and is locale-sensitive (descriptions in local language). sys_siglist is a raw array with no bounds checking.
Q2. What is NSIG?
NSIG is a constant defined in <signal.h> equal to one more than the highest signal number. It is used as the upper bound in loops iterating over all signals. It is not in SUSv3 but is available on most UNIX systems.
Q3. What does psignal() do and where does it write?
psignal(sig, msg) writes “msg: signal description” to stderr. It is similar to perror() for errno but for signal numbers. It is locale-sensitive.
Next Topic โ†’

Signal Sets (sigset_t) โ€” Working with collections of signals

Next: Signal Sets โ†’ โ† Previous

Leave a Reply

Your email address will not be published. Required fields are marked *