Sunday, May 12, 2013

Clarification on SIGKILL, SIGTERM, SIGINT, SIGQUIT, SIGSTP and SIGHUP

A few days ago, i landed upon unix signals that lead to process termination. I guess i was trying to remember the signals generated in linux when one presses Ctrl+Z and Ctrl+C. Memory did not serve me at that moment and i decided to look these up, one more time. I realized that having a consolidated book which explains these terms clearly is better than searching loads of webpages. I did the later since i had kept my unix os book away from my reach.To my disappointment, there was no single link that listed out all differences in an orderly fashion.

Hence, in this post, i wish to delineate these terms by consolidating my findings from stackoverflow, wikipedia and other unix internals websites. Here it goes:

SIGKILL: Terminates a process immediately. This signal cannot be handled (caught), ignored or blocked. (The "kill -9" command in linux generates the same signal).

SIGTERM: Terminates a process immediately. However, this signal can be handled, ignored or caught in code. If the signal is not caught by a process, the process is killed. Also, this is used for graceful termination of a process. (The "kill" command in linux if specified without any signal number like -9, will send SIGTERM)

SIGINT: Interrupts a process. (The default action is to terminate gracefully). This too, like, SIGTERM can be handled, ignored or caught. The difference between SIGINT and SIGTERM is that the former can be sent from a terminal as input characters. This is the signal generated when a user presses Ctrl+C. (Sidenote: Ctrl+C denotes EOT(End of Transmission) for (say) a network stream)

SIGQUIT: Terminates a process. This is different from both SIGKILL and SIGTERM in the sense that it generates a core dump of the process and also cleans up resources held up by a process. Like SIGINT, this can also be sent from the terminal as input characters. It can be handled, ignored or caught in code. This is the signal generated when a user presses Ctrl+\.

SIGSTP: Suspends a process. This too, can be handled, ignored or blocked. Since it does not terminate the process, the process can be resumed by sending a SIGCONT signal. This signal can be generated by pressing Ctrl+Z. (Sidenote: Ctrl+Z stands for substitute character which indicates End-of-File in DOS)

SIGHUP: (From Wikipedia): Hangs up a process when the controlling terminal is disconnected. This especially relates to modem/dial in connections. A process has to explicitly handle this signal for it to work. A good use is to "poke" a process and letting the process (as defined by the programmer) decide what to do with the signal is described here. Hence, SIGHUP can be handled, ignored or caught. This is the signal generated when a user presses Ctrl+D.

Technorati Tags: unix, signals, Unix-signals, SIGINT, SIGTERM, SIGKILL SIGQUIT, SIGSTP, SIGHUP

6 comments:

  1. Nitpicks:

    "SIGKILL: Terminates a process immediately. This signal cannot be handled (caught), ignored or blocked. (The kill command in linux generates the same signal)."

    Actually, if you don't specify a signal number, the kill command will send SIGTERM, not SIGKILL. That's why you have to say "kill -9" to send SIGKILL. Of course you can specify any signal number to send with the kill command.

    "SIGQUIT: ... This is the signal generated when a user presses Ctrl+D."

    No, that's SIGHUP, which is not on your list. SIGQUIT is generated by CTRL+\ rather than CTRL+D.

    ReplyDelete
    Replies
    1. Yes, you are correct about SIGKILL and "kill -9" command. However, i still think that Ctrl+D generates SIGQUIT.

      Delete
    2. No, Lee is correct, SIGQUIT is generated by Ctrl-Backslash (Ctrl+\) and on unix the default behavior for SIGQUIT is a coredump. So the OP as it currently reads saying SIGQUIT is generated by Ctrl-D is wrong.

      Empirical proof: Try running 'sleep 300' and then hit Ctrl-\, on the terminal you'll see:
      ^\Quit (core dumped)
      Same results on linux, freebsd, etc. Note it says "Quit".
      Also, look at the output of 'stty -a', and you should see quit = ^\.
      And the man page for stty should say 'quit' is the quit signal.

      Try doing the same test with sleep and hitting ^D; nothing will happen. It does not generate a signal. ^D is an EOF for programs reading stdin.. functions reading stdin will return an EOF error when it's encountered, which is why hitting ^D to 'cat' will end it only if it's reading stdin. If it's reading some other device, ^D has no effect.

      Delete
    3. Thanks for the comment and information unixguy. I have corrected the post.

      Delete
  2. Hello :) very interesting article, thank you :). One small correction: SIGSTP cannot be handled, ignored or blocked, just like SIGKILL.

    ReplyDelete