Middle
What signals exist in the Linux operating system?
sobes.tech AI
Answer from AI
Signals in Linux are a way of asynchronously notifying a process about events that have occurred.
Main standard signals:
- SIGINT (2): Interrupt from keyboard (Ctrl+C).
- SIGQUIT (3): Quit from keyboard, generating a core dump (Ctrl+).
- SIGKILL (9): Forceful process termination that cannot be caught or ignored.
- SIGTERM (15): Request for process termination, which can be caught for cleanup.
- SIGHUP (1): Hang up signal, usually sent when the connection is lost or the controlling terminal is closed.
- SIGSEGV (11): Invalid memory reference (segmentation fault).
- SIGFPE (8): Floating point exception (e.g., division by zero).
- SIGBUS (10): Bus error accessing physical memory.
- SIGCHLD (17): Notification to parent about child process termination or stop.
- SIGSTOP (19): Stop process (cannot be caught or ignored).
- SIGTSTP (20): Stop process from terminal (Ctrl+Z, can be caught).
- SIGCONT (18): Continue a stopped process.
Besides standard POSIX signals, there are others specific to Linux or different architectures. The list and signal numbers may vary.
Signal handling in Python is done using the signal module:
# Import the signal module
import signal
import time
# Signal handler function for SIGINT
def signal_handler(sig, frame):
print(f"Process interrupted by signal {sig}")
# Perform cleanup or other actions before exit
exit(0)
# Register handler for SIGINT
signal.signal(signal.SIGINT, signal_handler)
print("Press Ctrl+C to send SIGINT")
# Process will run until signal is received
while True:
time.sleep(1)
A process can:
- Ignore signals (except SIGKILL and SIGSTOP).
- Perform default action (most signals terminate the process).
- Execute a registered handler function.