Control-C and signal handling in C

佐手、 提交于 2019-12-07 21:32:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!