questions about signals

Hello! Here are some questions & answers. The goal isn't to get all the questions "right". Instead, the goal is to learn something! If you find a topic you're interested in learning more about, I'd encourage you to look it up and learn more.

what Unix program can you use to send signals?

kill

counterintuitively, you can use kill to send *any* signal, not just signals which kill the process. For example:

$ kill -STOP 1234

will send a SIGSTOP signal to PID 1234. The system call that's used to send signals is also called kill.

can any process send a signal to any other process?

nope!

to send a signal, you need to be running as the same user (or root). Otherwise, you'd be able to kill processes run by root, which would be bad!

what does a process do by default when you run kill PID on it?

the process exits immediately!

kill without any argument sends the SIGTERM signal. The default behavior for processes when they get a SIGTERM is to exit immediately.

can a process choose to not exit when you run kill PID?

yes!

any process can set signal handlers functions for a given signal (like SIGINT). You can either use a predefined signal disposition (like SIG_IGN for "ignore") or run a custom function.

why does pressing Ctrl+C in a terminal terminate the foreground process?

it sends a signal!

when you press Ctrl+C in your terminal, the kernel translates that into sending a SIGINT signal to the running process

if you run kill -9 PID, is it possible for the process to choose to handle it another way than just exiting immediately?

nope!

kill -9 sends the SIGKILL signal, which can't be blocked. It's a good way to force a process to exit immediately.

SIGSTOP can't be blocked either.

is kill -9 guaranteed to always kill a process?

nope!

processes can't block being killed with SIGKILL by setting a signal handler, but sometimes on Linux they get stuck doing I/O or something and become impossible to kill. This unkillable state is called "uninterruptable sleep".

when you send a process a signal, does it wait for the current function to finish?

not by default!

by default, signals don't wait for anything -- they just interrupt the process at a completely arbitrary point in its execution.

If you'd rather be interrupted only when it's convenient for the process, you can set the process's signal mask to block signals and then run sigwait when you want to receive them

does kill PID send the same signal as pressing Ctrl+C in a terminal?

nope!

kill PID sends a SIGTERM and Ctrl+C sends a SIGINT

does "stopping" a process (with SIGSTOP) cause the process to exit?

nope!

stopping the process is like hitting "pause" -- it stops using the CPU but all of the process's current state stays intact. You can resume it later and it'll pick up right where it left off.

if you have a program (like vim) running in a terminal, you can send SIGTSTP (which will also stop the process by default) to it by pressing Ctrl+Z

can you use signals for things other than killing / stopping processes?

yes!

SIGUSR1, SIGUSR2, and SIGHUP terminate the process by default, but processes sometimes use these signals to reload configuration or print out debug info. For example, if you send nginx a SIGHUP, it'll reload its configuration.

more reading