问题
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?
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/5269942/control-c-and-signal-handling-in-c