Test
string
class has been briefly introduced in an earlier chapter. It is a very powerful class to handle and manipulate strings of characters. However, because strings are, in fact, sequences of characters, we can represent them also as plain arrays of elements of a character type.;
), and are executed in the same order in which they appear in a program.Signal | Description |
---|---|
SIGABRT | Abnormal termination of the program, such as a call to abort |
SIGFPE | An erroneous arithmetic operation, such as a divide by zero or an operation resulting in overflow. |
SIGILL | Detection of an illegal instruction |
SIGINT | Receipt of an interactive attention signal. |
SIGSEGV | An invalid access to storage. |
SIGTERM | A termination request sent to the program. |
void (*signal (int sig, void (*func)(int)))(int);
#include <iostream> #include <csignal> using namespace std; void signalHandler( int signum ) { cout << "Interrupt signal (" << signum << ") received.\n"; // cleanup and close up stuff here // terminate program exit(signum); } int main () { // register signal SIGINT and signal handler signal(SIGINT, signalHandler); while(1){ cout << "Going to sleep...." << endl; sleep(1); } return 0; }
Going to sleep.... Going to sleep.... Going to sleep....
Going to sleep.... Going to sleep.... Going to sleep.... Interrupt signal (2) received.
int raise (signal sig);
#include <iostream> #include <csignal> using namespace std; void signalHandler( int signum ) { cout << "Interrupt signal (" << signum << ") received.\n"; // cleanup and close up stuff here // terminate program exit(signum); } int main () { int i = 0; // register signal SIGINT and signal handler signal(SIGINT, signalHandler); while(++i){ cout << "Going to sleep...." << endl; if( i == 3 ){ raise( SIGINT); } sleep(1); } return 0; }
Going to sleep.... Going to sleep.... Going to sleep.... Interrupt signal (2) received.