Middle
What signals exist in the Linux operating system?
sobes.tech AI
Answer from AI
POSIX-compatible signals:
SIGINT: Interrupt request from the user (Ctrl+C).SIGTERM: Request to terminate the program.SIGKILL: Unconditional termination of the program (cannot be caught).SIGQUIT: Request to terminate and dump memory (Ctrl+).SIGHUP: Hangup detected on controlling terminal; restart daemon.SIGFPE: Arithmetic error (e.g., division by zero).SIGSEGV: Segmentation fault (invalid memory access).SIGBUS: Bus error (invalid memory access).SIGILL: Illegal instruction.SIGTRAP: Trace/breakpoint trap.SIGABRT: Abort signal (e.g., fromabort()).SIGUSR1,SIGUSR2: User-defined signals (for applications).SIGSTOP: Stop process (cannot be caught).SIGTSTP: Stop process from terminal (Ctrl+Z).SIGCONT: Continue a stopped process.SIGCHLD: Child process status change.SIGTTIN: Background process reading from terminal.SIGTTOU: Background process writing to terminal.SIGPOLL(SIGIO): I/O now possible on a file descriptor.SIGPROF: Profiling timer expired.SIGSYS: Bad system call.SIGURG: Urgent condition on socket.SIGVTALRM: Virtual timer expired.SIGXCPU: CPU time limit exceeded.SIGXFSZ: File size limit exceeded.
Real-time signals: Start from SIGRTMIN and end with SIGRTMAX. They are used for specific scheduling and communication tasks between processes, with their numbers depending on the implementation.
Signal handling in Python:
import signal
import os
import time
def handler(signum, frame):
# Signal handler
print(f"\nReceived signal: {signal.Signals(signum).name}")
if signum == signal.SIGTERM:
print("Terminating gracefully...")
# Code for clean shutdown can be placed here
exit(0)
# Setting handlers for SIGTERM and SIGINT
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)
print(f"My PID: {os.getpid()}")
print("Press Ctrl+C or send SIGTERM...")
while True:
# Program runs
time.sleep(1)
print("Working...")
Sending signals from the command line:
# Send SIGTERM to process with PID 12345
kill 12345
# Send SIGKILL to process with PID 12345
kill -9 12345
# Send SIGINT (equivalent to Ctrl+C)
# In another terminal window, if the process is not running in background
# kill -INT <PID>