2

I have 2 processes talking to each other Sender and Receiver via socket.I would like to catch signal Control-C and instead of exiting - display some output.Sender and Receiver are working fine,so I added signal(SIGINT,handler) to Sender's body. handler() just outputs some text.So when I run them and hit Cnt-C - signal gets caught and handler outputs the text but exits the Sender process.Sender has a loop that listens for user input unless Cnt-D - so why handler is making Sender exit?

user629034
  • 659
  • 2
  • 11
  • 30

2 Answers2

1

If you are not re-registering the signal inside the handler, then it will revert to the default value, and exit once a signal has been sent the second time. For more detailed explanation look at my post here.

Community
  • 1
  • 1
Milan
  • 15,389
  • 20
  • 57
  • 65
1

You need to trap/handle both signal 2 (SIGINT) and 3 (SIGEXIT I believe). Note that you generally do not want to do this: Control-C should always be a last-resort really exit strategy. The only legitimate reason to trap it is to do cleanup etc.

dtech
  • 13,741
  • 11
  • 48
  • 73