hey, thanks. I googled a few pages. this one, IPC:Interrupts and Signals: <signal.h>, as it is written does not terminate itself. I updated it with the volatile variable suggested in the previous post. the program responds to ctrl+c or ctrl+\ by printing out a message, however it doesn't terminate. please give me a hint. thanks.
Code:
#include <stdio.h>
#include <signal.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
using namespace std;
void sigproc(int);
void quitproc(int);
volatile bool signaled;
int main() {
signal(SIGINT, sigproc );
signal(SIGQUIT, quitproc);
cout << "ctrl-c disabled use ctrl-\\ to quit" << endl;
bool signaled = false;
for (;;){
if (signaled) break;
}
; /* infinite loop */
}
void sigproc(int param) {
signal(SIGINT, sigproc); /* */
/* NOTE some versions of UNIX will reset signal to default
after each call. So for portability reset signal each time */
cout << "you have pressed ctrl-c" << endl;
}
void quitproc(int param) {
cout << "ctrl-\\ pressed to quit" << endl;
return; /* normal exit status */
signaled = true;
}
ouptut:
Code:
ctrl-c disabled use ctrl-\ to quit
you have pressed ctrl-c
you have pressed ctrl-c
you have pressed ctrl-c
ctrl-\ pressed to quit
ctrl-\ pressed to quit
ctrl-\ pressed to quit
ctrl-\ pressed to quit
ctrl-\ pressed to quit
ctrl-\ pressed to quit
ctrl-\ pressed to quit
p.s. the comment in the sigproc() is from the website listed above, it seems conflicts with the example on this page: signal - C++ Reference, which is convoluted. anyone can explain to me? Thanks!