171 lines
5.4 KiB
HTML
171 lines
5.4 KiB
HTML
<html>
|
|
<head>
|
|
<title>
|
|
C Guide--2.9 signal.h
|
|
</title>
|
|
<!-- Changed by: eric huss, 12-Mar-1997 -->
|
|
</head>
|
|
<body text="#000000" bgcolor="#FFFFFF">
|
|
|
|
<center>
|
|
<table border=0 width=100%>
|
|
<tr>
|
|
<td align=left width=20% valign=top>
|
|
<a href="2.8.html">
|
|
<img src="left.gif" border=0>
|
|
Previous Section<br>
|
|
2.8 setjmp.h</a></td>
|
|
<td align=center width=60% valign=top>
|
|
| <a href="index.html">Table of Contents</a> |
|
|
<a href="index2.html">Index</a> |</td>
|
|
<td align=right width=20% valign=top>
|
|
<a href="2.10.html">
|
|
Next Section
|
|
<img src="right.gif" border=0><br>
|
|
2.10 stdarg.h</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
<hr>
|
|
|
|
|
|
<h1> 2.9 signal.h</h1>
|
|
<p>
|
|
The signal header provides a means to handle signals reported during a program's
|
|
execution.
|
|
<p>
|
|
Macros:
|
|
<blockquote><code><b>
|
|
SIG_DFL<br>
|
|
SIG_ERR<br>
|
|
SIG_IGN<br>
|
|
SIGABRT<br>
|
|
SIGFPE<br>
|
|
SIGILL<br>
|
|
SIGINT<br>
|
|
SIGSEGV<br>
|
|
SIGTERM<br>
|
|
</b></code></blockquote>
|
|
Functions:
|
|
<blockquote><code><b>
|
|
signal();<br>
|
|
raise();<br>
|
|
</b></code></blockquote>
|
|
Variables:
|
|
<blockquote><code><b>
|
|
typedef sig_atomic_t
|
|
</b></code></blockquote>
|
|
|
|
<a name="variables"></a>
|
|
<h2>2.9.1 Variables and Definitions</h2>
|
|
<p>
|
|
The <code><b>sig_atomic_t</b></code> type is of type <code><b>int</b></code> and is used as a variable in a signal handler.
|
|
The <code><b>SIG_</b></code> macros are used with the signal function to define signal functions.
|
|
<table border=0>
|
|
<tr><td><code><b> SIG_DFL</b></code></td><td> Default handler.</td></tr>
|
|
<tr><td><code><b> SIG_ERR</b></code></td><td> Represents a signal error.</td></tr>
|
|
<tr><td><code><b> SIG_IGN</b></code></td><td> Signal ignore.</td></tr>
|
|
</table>
|
|
<p>
|
|
The <code><b>SIG</b></code> macros are used to represent a signal number in the following conditions:
|
|
<table border=0>
|
|
<tr><td><code><b> SIGABRT</b></code></td><td> Abnormal termination (generated by the abort function).</td></tr>
|
|
<tr><td><code><b> SIGFPE</b></code></td><td> Floating-point error (error caused by division by zero, invalid operation, etc.). </td></tr>
|
|
<tr><td><code><b> SIGILL</b></code></td><td> Illegal operation (instruction).</td></tr>
|
|
<tr><td><code><b> SIGINT </b></code></td><td> Interactive attention signal (such as ctrl-C).</td></tr>
|
|
<tr><td><code><b> SIGSEGV</b></code></td><td> Invalid access to storage (segment violation, memory violation).</td></tr>
|
|
<tr><td><code><b> SIGTERM </b></code></td><td> Termination request.</td></tr>
|
|
</table>
|
|
<a name="signal"></a>
|
|
<h2>2.9.2 signal</h2>
|
|
<p>
|
|
Declaration:
|
|
<blockquote>
|
|
<code><b>void (*signal(int</b></code><i> sig</i><code><b>, void (*</b></code><i>func</i><b><code>)(int)))(int);</b></code>
|
|
</blockquote
|
|
<p>
|
|
Controls how a signal is handled. <i>sig</i> represents the signal number compatible with the
|
|
<code><b>SIG</b></code> macros. <i>func</i> is the function to be called when the signal occurs. If func is <code><b>SIG_DFL</b></code>, then
|
|
the default handler is called. If <i>func</i> is <code><b>SIG_IGN</b></code>, then the signal is ignored. If <i>func</i> points to a
|
|
function, then when a signal is detected the default function is called (<code><b>SIG_DFL</b></code>), then the
|
|
function is called. The function must take one argument of type <code><b>int</b></code> which represents the signal
|
|
number. The function may terminate with <code><b>return</b></code>, <code><b>abort</b></code>, <code><b>exit</b></code>, or <code><b>longjmp</b></code>. When the function
|
|
terminates execution resumes where it was interrupted (unless it was a <code><b>SIGFPE</b></code> signal in which
|
|
case the result is undefined).
|
|
<p>
|
|
If the call to signal is successful, then it returns a pointer to the previous signal handler
|
|
for the specified signal type. If the call fails, then <code><b>SIG_ERR</b></code> is returned and <code><b>errno</b></code> is set
|
|
appropriately.
|
|
<p>
|
|
<a name="raise"></a>
|
|
<h2>2.9.3 raise</h2>
|
|
<p>
|
|
Declaration
|
|
<blockquote>
|
|
<code><b>int raise(int </b></code><i>sig</i><code><b>);</b></code>
|
|
</blockquote>
|
|
Causes signal <i>sig</i> to be generated. The <i>sig</i> argument is compatible with the <code><b>SIG</b></code> macros.
|
|
<p>
|
|
If the call is successful, zero is returned. Otherwise a nonzero value is returned.
|
|
<p>
|
|
Example:
|
|
<blockquote><code><b><pre>
|
|
#include<signal.h>
|
|
#include<stdio.h>
|
|
|
|
void catch_function(int);
|
|
|
|
int main(void)
|
|
{
|
|
if(signal(SIGINT, catch_function)==SIG_ERR)
|
|
{
|
|
printf("An error occured while setting a signal handler.\n");
|
|
exit(0);
|
|
}
|
|
|
|
printf("Raising the interactive attention signal.\n");
|
|
if(raise(SIGINT)!=0)
|
|
{
|
|
printf("Error raising the signal.\n");
|
|
exit(0);
|
|
}
|
|
printf("Exiting.\n");
|
|
return 0;
|
|
}
|
|
|
|
void catch_function(int signal)
|
|
{
|
|
printf("Interactive attention signal caught.\n");
|
|
}
|
|
</pre></b></code></blockquote>
|
|
The output from the program should be (assuming no errors):<br>
|
|
<pre>
|
|
Raising the interactive attention signal.
|
|
Interactive attention signal caught.
|
|
Exiting.
|
|
</pre>
|
|
<hr>
|
|
|
|
<center>
|
|
<table border=0 width=100%>
|
|
<tr>
|
|
<td align=left width=20% valign=top>
|
|
<a href="2.8.html">
|
|
<img src="left.gif" border=0>
|
|
Previous Section<br>
|
|
2.8 setjmp.h</a></td>
|
|
<td align=center width=60% valign=top>
|
|
| <a href="index.html">Table of Contents</a> |
|
|
<a href="index2.html">Index</a> |</td>
|
|
<td align=right width=20% valign=top>
|
|
<a href="2.10.html">
|
|
Next Section
|
|
<img src="right.gif" border=0><br>
|
|
2.10 stdarg.h</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
|
|
</body>
|
|
</html>
|