#include <signal.h>
#include <unistd.h>
#include <iostream>
usingnamespace std;
void func ( int sigaction )
{
cout << "Oops! -- I got a signal " << sigaction << endl;
}
int main()
{
(void) signal ( SIGINT, func ); //catch terminal interrupts
while (1) {
cout << "CSUSB CS 460 lab on signals" << endl;
sleep ( 1 );
}
return 0;
}
Modify your test_signal.cpp program above by using sigaction() to intercept SIGINT; replace the "for" loop with "while ( 1 ); you should be able to quit the program by entering "^\".
So I followed the requirement to the best of my ability but I'm unsure how to do it because the program does not quit when I do ctrl C. What do I do next?
You're probably not sleeping long enough, this is a common enough issue and one reason why we tell people not to use this function. Try hitting "Pause", this should freeze execution of the application long enough for you to get a signal in edgewise.