Sobes.tech
Senior

What OS signals can be intercepted, and which cannot?

sobes.tech AI

Answer from AI

In Unix-like operating systems (Linux, macOS), signals can be divided into catchable and uncatchable.

Catchable signals are those for which a process can set up a handler (signal handler). For example:

  • SIGINT (interrupt, usually Ctrl+C)
  • SIGTERM (termination request)
  • SIGHUP (hangup, terminal restart)
  • SIGUSR1, SIGUSR2 (user-defined signals)

Uncatchable signals are signals that cannot be caught or ignored, as they are intended for immediate process termination or critical situations:

  • SIGKILL (immediate process termination)
  • SIGSTOP (pause process)

Example in Go for catching SIGINT:

import (
    "os"
    "os/signal"
    "syscall"
    "fmt"
)

func main() {
    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

    go func() {
        sig := <-sigs
        fmt.Println("Received signal:", sig)
        // handle signal
        os.Exit(0)
    }()

    select {} // wait for signal
}

Thus, most signals can be caught, except for SIGKILL and SIGSTOP, which the OS handles forcibly.

What OS signals can be intercepted, and which cannot… - sobes.tech