using sigaction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <signal.h>
#include <unistd.h>
#include <iostream>

using namespace 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.
I figured it out I just had to change this:

 
  (void) signal ( SIGINT, func );	//catch terminal interrupts 


to this:


(void) signal ( SIGKILL, func ); //catch terminal interrupts
Topic archived. No new replies allowed.