Saturday, August 16, 2008

KILL's not that fatal after all.

In Unix and Unix-like operating systems, kill is a command used to send simple messages to processes running on the system. By default, the message sent is the "termination" signal, which requests that the process exit. But kill is something of a misnomer; the signal sent may have nothing to do with process killing. The kill command is a wrapper around the kill() system call.

A process can be sent a SIGTERM signal in three ways (the process ID is '1234' in this case):

  • kill 1234
  • kill -TERM 1234
  • kill -15 1234

The process can be sent a SIGKILL signal in two ways:

  • kill -KILL 1234
  • kill -9 1234

Other useful signals include HUP, TRAP, INT and ALRM. A SIGINT signal can be generated very simply by pressing CTRL+C in most Unix shells. It is also common for CTRL+Z to be mapped to SIGTSTP (tty stop), and for CTRL+\ (backslash) to be mapped to SIGQUIT, which can force a program to do a core dump.

All signals except for SIGKILL and SIGSTOP can be "intercepted" by the process, meaning that a special function can be called when the program receives those signals. The two exceptions SIGKILL and SIGSTOP are only seen by the host system's kernel, providing reliable ways of controlling the execution of processes. SIGKILL kills the process, and SIGSTOP pauses it until a SIGCONT is received.

On most shells, using the 'fg' command will resume execution of the process (that was suspended with Ctrl-Z), by sending it a CONT signal.

Exceptions such as division by zero or a segmentation violation will generate signals (here, SIGFPE and SIGSEGV respectively, which both cause a core dump by default).

The kernel can generate a signal to notify the process of an event. For example, SIGPIPE will be generated when a process writes to a pipe which has been closed by the reader; by default, this causes the process to terminate, which is convenient when constructing shell pipelines.

For a complete list of supported signals, see signals.

1 comment:

Anonymous said...

good to see the dissection of KILL sysem call.

just to add something on "On most shells, using the 'fg' command will resume execution of the process (that was suspended with Ctrl-Z), by sending it a CONT signal.
"

fg command will resume the execution but it'll be in foreground, you can also start a suspended process using "bg" command in background.

FYI
have posted this comment for the sake of posting a comment only :P