C signal handling: Difference between revisions

Content deleted Content added
Reverted 2 edits by 103.40.80.11 (talk): Printf does not output a newline and while loop prevents execution of the following lines. (TW)
Garo (talk | contribs)
zi
Line 21:
A signal can be generated by calling <code>raise()</code> or <code>kill()</code> system calls. <code>raise()</code> sends a signal to the current process, <code>kill()</code> sends a signal to a specific process.
 
A signal handler can be specified for all but two signals ([[SIGKILL]] and [[SIGSTOP]] cannot be caught, blocked or ignored). A signal handler is a [[Function (computer science)|function]] which is called by the target environment when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls <code>longjmp()</code>. For maximum portability, an asynchronous signal handler should only:
 
*make successful calls to the function <code>signal()</code>
Signal handlers can be set be with <code>signal()</code> or <code>sigaction()</code>. The behavior of <code>signal()</code> has been changed multiple times across history and is now considered deprecated. It is only portable when used to set a signal's disposition to SIG_DFL or SIG_IGN. Signal handlers can be specified for all but two signals ([[SIGKILL]] and [[SIGSTOP]] cannot be caught, blocked or ignored).
*assign values to objects of type [[volatile variable|volatile]] <code>sig_atomic_t</code>
*return control to its caller
 
If the signal reports an error within the program (and the signal is not asynchronous), the signal handler can terminate by calling <code>abort()</code>, <code>exit()</code>, or <code>longjmp()</code>.