Ch20.7 โ€“ Signal Types & Default Actions

 

Ch20.7 โ€“ Signal Types & Default Actions
Linux System Programming ยท EmbeddedPathashala
๐Ÿ“ก Topic 7 of 19
๐Ÿ“š Reference Table
๐Ÿ’ป 1 Code Example
โ“ Interview Q&A
๐Ÿ”‘ Key Terms
SIGABRTSIGALRMSIGBUS SIGCHLDSIGFPESIGHUP SIGILLSIGINTSIGKILL SIGPIPESIGQUITSIGSEGV SIGTERMSIGUSR1/2
Quick Guide to Badge Colors

term = process terminated ย  core = core dump + terminate ย  ignore = signal discarded ย  stop = process suspended ย  cont = resume stopped process

๐Ÿ“Š Complete Linux Signal Reference Table (Standard Signals)
Signal No. Default Description & When Generated
SIGABRT 6 core Process calls abort(). Useful for debugging โ€” produces core dump.
SIGALRM 14 term Real-time timer set by alarm() or setitimer() has expired.
SIGBUS 7 core Bus error โ€” certain memory access errors, e.g. mmap beyond file end.
SIGCHLD 17 ignore A child process terminated, stopped, or resumed. Sent to parent.
SIGCONT 18 cont Resume a stopped process. Ignored by non-stopped processes.
SIGFPE 8 core Arithmetic exception โ€” integer divide-by-zero, floating point fault.
SIGHUP 1 term Terminal disconnect (hangup). Also used to reload daemons.
SIGILL 4 core Illegal (malformed) machine-language instruction executed.
SIGINT 2 term Terminal interrupt โ€” user pressed Ctrl+C.
SIGIO 29 term I/O event available on certain file descriptors (fcntl async I/O).
SIGKILL 9 term Sure kill โ€” cannot be blocked, ignored, or caught. Always kills.
SIGPIPE 13 term Write to pipe/socket with no reader. Normally means reader closed.
SIGPROF 27 term Profiling timer (user + kernel CPU time) expired.
SIGPWR 30 term UPS power failure detected โ€” sent to init to shut down cleanly.
SIGQUIT 3 core Terminal quit โ€” user pressed Ctrl+\. Generates core dump.
SIGSEGV 11 core Invalid memory reference โ€” bad pointer, accessing unmapped memory.
SIGSTOP 19 stop Sure stop โ€” cannot be blocked, ignored, or caught. Always stops.
SIGSYS 31 core Invalid system call number used.
SIGTERM 15 term Standard termination signal. Default sent by kill command.
SIGTRAP 5 core Debugger breakpoint or system call tracing (used by gdb, strace).
SIGTSTP 20 stop Job-control stop โ€” user pressed Ctrl+Z. Stops foreground process.
SIGTTIN 21 stop Background process tried to read from terminal.
SIGTTOU 22 stop Background process tried to write to terminal (when TOSTOP set).
SIGURG 23 ignore Out-of-band (urgent) data arrived on a socket.
SIGUSR1 10 term User-defined signal 1 โ€” available for application use.
SIGUSR2 12 term User-defined signal 2 โ€” available for application use.
SIGVTALRM 26 term Virtual timer (user-mode CPU time only) expired.
SIGWINCH 28 ignore Terminal window size changed. Used by vi, less to redraw.
SIGXCPU 24 core Process exceeded CPU time resource limit.
SIGXFSZ 25 core Process tried to exceed file size resource limit.
๐Ÿ’ป Code Example โ€“ Print All Signal Names and Numbers
/* Print all signal names and numbers on this system
   Compile: gcc -o list_sigs list_sigs.c
   Run: ./list_sigs */

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

int main(void)
{
    int sig;

    printf("%-5s %-12s %s\n", "Num", "Name", "Description");
    printf("%-5s %-12s %s\n", "---", "----", "-----------");

    for (sig = 1; sig < NSIG; sig++) {
        /* strsignal() returns human-readable description */
        const char *desc = strsignal(sig);
        if (desc == NULL)
            desc = "(unknown)";
        printf("%-5d %-12s %s\n", sig, "SIG?", desc);
    }

    printf("\nNSIG = %d (total signal slots)\n", NSIG);
    return 0;
}
Run it! This prints all signal descriptions for your system using strsignal(). On Linux x86-64 NSIG is typically 65.
โ“ Interview Questions
Q1. What is the difference between SIGTERM and SIGKILL?
SIGTERM is the polite termination request โ€” a process can catch it, run cleanup code, and exit gracefully. SIGKILL is the forceful terminator โ€” it cannot be caught, blocked, or ignored; the kernel kills the process immediately.
Q2. What is the difference between SIGINT and SIGQUIT?
Both are generated by the terminal. SIGINT (Ctrl+C) terminates the process. SIGQUIT (Ctrl+\) terminates the process AND produces a core dump, which is useful for debugging hung programs.
Q3. What is the purpose of SIGUSR1 and SIGUSR2?
They are reserved for application-defined purposes. The kernel never generates them. Applications use them to send custom notifications between processes โ€” for example, telling a daemon to rotate its log file.
Q4. What is SIGHUP and how do daemons use it?
SIGHUP originally meant a terminal hangup (modem disconnected). Daemons repurpose it as a “reload configuration” signal โ€” the system administrator sends SIGHUP to make a daemon re-read its config file without restarting.
Q5. What is the difference between SIGSTOP and SIGTSTP?
SIGSTOP is the sure-stop signal โ€” cannot be caught or ignored. SIGTSTP is the terminal stop signal (Ctrl+Z) which CAN be caught, allowing a program to do cleanup before stopping or to refuse to stop.
Q6. When is SIGCHLD sent and what is its default action?
SIGCHLD is sent to a parent process when one of its children terminates, stops, or resumes. The default action is to ignore it. Parents that want to collect child exit status install a SIGCHLD handler or call wait().
Next Topic โ†’

The signal() API โ€” How to install handlers using the original UNIX API

Next: signal() API โ†’ โ† Previous

Leave a Reply

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