0

From APUE

A process has three choices for dealing with a signal.

  1. Ignore the signal. This option isn’t recommended for signals that denote a hardware exception, such as dividing by zero or referencing memory outside the address space of the process, as the results are undefined.

  2. Let the default action occur. For a divide-by-zero condition, the default is to terminate the process.

  3. Provide a function that is called when the signal occurs (this is called ‘‘catching’’ the signal). By providing a function of our own, we’ll know when the signal occurs and we can handle it as we wish.

  1. I think there is only two choices - the last two listed above, and both of them can "ignore the signal" (the first choice listed above).

    Is my understanding correct, or are there indeed three nonoverlapping choices as in the quote? Why?

  2. The book mentions that the default action of some signal is to ignore it. Does it mean the action of the signal is SIG_IGN or SIG_DFL or an empty function? For example:

    The default action for SIGCHLD is to be ignored. We describe these options in Chapter 10.

    SIGCONT: The default action is to continue a stopped process, but to ignore the signal if the process wasn’t stopped.

Thanks.

Tim
  • 101,790

1 Answers1

3

Of course one can write a signal handler that does nothing, and thus effectively ignoring the signal, but the first option is to specifically ignore the signal by using the SIG_IGN argument the signal() system call.

So in terms of code, assuming the SIGINT signal, these are the three options:

  • signal(SIGINT, SIG_IGN); to ignore
  • To not call the signal() function, or to call it with signal(SIGINT, SIG_DFL); and thus to let the default action occur, i.e. to terminate the process
  • signal(SIGINT, termination_handler);, where termination_handler() is a function that is called the first time the signal occurs.

Source: https://www.gnu.org/software/libc/manual/html_node/Basic-Signal-Handling.html

For the specific case of signals were the default action is to ignore the signal, the two first options (SIG_IGN and SIG_DFL) are identical. Creating an empty handler function probably won't have a visible effect, other than a (small) overhead.

NOTE: The signal() is used in this answer for simplicity, but in new code sigaction() is recommended for reasons of portability.

user000001
  • 3,635
  • Thanks. In "the first option is to specifically ignore the signal by using the SIG_IGN argument the signal() system call", do you mean that signal() is a system call, compared to sigaction()? – Tim May 31 '18 at 04:21
  • Just to clarify, sigaction() is also a system call – user000001 May 31 '18 at 04:29
  • I also see in the book mentioning that the default action of some signal is to ignore it. Does it mean the action of the signal is SIG_IGN or SIG_DFL or an empty function? – Tim May 31 '18 at 04:43
  • Yes for signals that the default is to ignore, then the options are identical – user000001 May 31 '18 at 04:47
  • I am not sure what you mean by "the options are identical". Do you mean "SIG_IGN" and "SIG_DFL" are identical for some signal but not for others? If yes, that is confusing. See my update to mypost. – Tim May 31 '18 at 04:58
  • @Tim: If the default is to ignore, explicitly setting it to SIG_IGN won't change anything. If the default is to take an action (such as terminating the process) obviously ignoring the signal will change the semantics. Could you clarify what is confusing about it? – user000001 May 31 '18 at 04:59
  • (1) See my post for "default is ignoring" question. (2) Also for whether system() is a system call, see https://stackoverflow.com/questions/50616899/is-signal-a-system-call-function – Tim May 31 '18 at 05:01
  • @Tim (1) I already did that both before and after the edit. (2) It is included in the man page about signals. How does the presence of wrapper function change anything? – user000001 May 31 '18 at 05:08
  • are there indeed three nonoverlapping choices as in the quote? – Tim May 31 '18 at 05:15
  • @Tim: The quote doesn't say that they are non-overlapping. As you said, only for signals that the default is to ignore the two first choices do the same thing. For the rest of the signals, the outcome of two first choices is different – user000001 May 31 '18 at 05:20