📄 signal.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><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"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.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 <signal.h> 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 signal.h):</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="example-code">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> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -