-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal.html
More file actions
187 lines (145 loc) · 4.71 KB
/
signal.html
File metadata and controls
187 lines (145 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Mirrored from cppreference.com/stdother/signal.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:24:30 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<title>signal</title>
<link href="../cppreference.css" rel="stylesheet" type="text/css">
<link href="../prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../prettify.js"></script>
</head>
<body onload="prettyPrint()">
<table>
<tr>
<td>
<div class="body-content">
<div class="header-box">
<a href="../index-2.html">cppreference.com</a> > <a href=
"index.html">Other Standard C Functions</a> > <a href=
"signal.html">signal</a>
</div>
<div class="name-format">
signal
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <csignal>
void ( *signal( int signal, void (* func) (int)) ) (int);
</pre>
<p>The signal() function sets <em>func</em> to be called when
<em>signal</em> is recieved by your program. <em>func</em> can be a
custom signal handler, or one of these macros (defined in
the csignal header file):</p>
<table class="code-table">
<tr>
<th class="code-table-th">Macro</th>
<th class="code-table-th">Explanation</th>
</tr>
<tr>
<td class="code-table-td">SIG_DFL</td>
<td class="code-table-td">default signal handling</td>
</tr>
<tr>
<td class="code-table-td">SIG_IGN</td>
<td class="code-table-td">ignore the signal</td>
</tr>
</table>
<p>Some basic signals that you can attach a signal handler to
are:</p>
<table class="code-table">
<tr>
<th class="code-table-th">Signal</th>
<th class="code-table-th">Description</th>
</tr>
<tr>
<td class="code-table-td">SIGTERM</td>
<td class="code-table-td">Generic stop signal that can be
caught.</td>
</tr>
<tr>
<td class="code-table-td">SIGINT</td>
<td class="code-table-td">Interrupt program, normally
ctrl-c.</td>
</tr>
<tr>
<td class="code-table-td">SIGQUIT</td>
<td class="code-table-td">Interrupt program, similar to
SIGINT.</td>
</tr>
<tr>
<td class="code-table-td">SIGKILL</td>
<td class="code-table-td">Stops the program. Cannot be
caught.</td>
</tr>
<tr>
<td class="code-table-td">SIGHUP</td>
<td class="code-table-td">Reports a disconnected terminal.</td>
</tr>
</table>
<p>The return value of signal() is the address of the previously
defined function for this signal, or SIG_ERR is there is an
error.</p>
<div class="related-examples-format">
Example code:
</div>
<div class="related-examples">
<p>The following example uses the signal() function to call an
arbitrary number of functions when the user aborts the program. The
functions are stored in a vector, and a single "clean-up"
function calls each function in that vector of functions when the
program is aborted:</p>
<pre class="prettyprint">
void f1() {
cout << "calling f1()..." << endl;
}
void f2() {
cout << "calling f2()..." << endl;
}
typedef void(*endFunc)(void);
vector<endFunc> endFuncs;
void cleanUp( int dummy ) {
for( unsigned int i = 0; i < endFuncs.size(); i++ ) {
endFunc f = endFuncs.at(i);
(*f)();
}
exit(-1);
}
int main() {
// connect various signals to our clean-up function
signal( SIGTERM, cleanUp );
signal( SIGINT, cleanUp );
signal( SIGQUIT, cleanUp );
signal( SIGHUP, cleanUp );
// add two specific clean-up functions to a list of functions
endFuncs.push_back( f1 );
endFuncs.push_back( f2 );
// loop until the user breaks
while( 1 );
return 0;
}
</pre>
</div>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="raise.html">raise</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/stdother/signal.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:24:30 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>