C signal handling
lastmod 2022-11-01created 2022-11-016 edits
In the standard library, signal processing (signal processing) defines how a program handles various signals while it is running. A signal can report some exceptional behavior within the program (such as division by zero), or an asynchronous event outside the program (such as a key press on the keyboard).
SIGABRT
Abnormal termination; in the JVM, the signal the JVM raises whenever it detects an error.
- Access to memory that was not allocated. Usually occurs when malloc fails.
- Normal use for exception handling. It occurs as exception handling, not as a crash.
SIGTERM
- A safe way to stop a process. (Gracefully kills the process)
- The process can detect the signal (Can be handled),
- it can be ignored (ignored),
- and it can be blocked (blocked).
- If the process is in the middle of an operation that cannot be interrupted, it may ignore the SIGTERM signal.
- On Linux, its signal ID is
15, and it is the default for the kill command. - SIGTERM does not kill child processes.
SIGKILL
- This is the signal that says "I don't care what you're doing. Stop right now."
- When the signal is received, the process stops immediately.
- It is handled by the kernel and cannot be detected by the process. (Cannot be handled or blocked)
- On Linux, its signal ID is
9. - SIGKILL also kills child processes.
SIGKILL vs SIGTERM
See C signal handling#SIGKILL vs SIGTERM.
- The kill command in Unix / Linux - difference between 9 (SIGKILL) and 15 (SIGTERM)
- What is a signal? Signal (SIGNAL) types, situations, and differences between similar signals
- [Recommended] What is SIGTERM? What's the difference between SIGKILL & SIGTERM?
- [Recommended] Termination Signals (The GNU C Library)
Both signals are used to terminate a process, but there are some differences between them. Here is why you should prefer SIGTERM over SIGKILL:
- SIGTERM terminates a process gracefully, whereas SIGKILL terminates it immediately.
- The SIGTERM signal can be handled, ignored, and blocked, but SIGKILL cannot be handled or blocked.
- SIGTERM does not kill child processes. SIGKILL also kills child processes.
See also
- csignal
- std::atomic_signal_fence
- std::memory_order
- See
/usr/include/bits/signum.h - kill: sample code:
kill -l - Orphan process
- Zombie process
- Signal (IPC) (Unix Signal)