Senior
What Linux signals do you know? What are they used for?
sobes.tech AI
Answer from AI
In Linux, signals are a mechanism for asynchronous notification of processes about events. They are used for process control, error handling, and interaction with the OS.
Main signals:
SIGINT(2) — process interruption (usually Ctrl+C in the terminal).SIGTERM(15) — request for graceful process termination.SIGKILL(9) — immediate process termination (cannot be caught).SIGSTOP(19) — process suspension (cannot be ignored).SIGCONT(18) — continuation of a suspended process.SIGSEGV(11) — segmentation fault (invalid memory access).SIGALRM(14) — timer signal.SIGCHLD(17) — notification to the parent process about child process termination.
Usage:
- Managing process lifecycle (start, stop, restart).
- Handling exceptional situations (e.g., resource cleanup on termination).
- Implementing timeouts and periodic tasks.
Example of handling SIGINT signal in Go:
import (
"os"
"os/signal"
"syscall"
"fmt"
)
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
fmt.Println("Waiting for SIGINT (Ctrl+C)...")
<-sigs
fmt.Println("SIGINT received, terminating")
}