Linux下C醒目的正常终止终止信号 [英] Linux C catching kill signal for graceful termination

查看:273
本文介绍了Linux下C醒目的正常终止终止信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用套接字,数据库连接和喜欢的过程。它基本上是一个服务器进程的传感器数据和网络接口之间的中继,因此它以确保该应用程序,如果杀死,终止正常是重要的。

I have a process using sockets, database connections and the likes. It is basically a server process relaying between sensor data and a web interface, and as such it is important to ensure the application, if killed, terminates gracefully.

我如何处理突发性的异常,如段错误(至少对于调试),以及终止信号,这样我可以关闭所有连接和停止运行,这样的过程不会留下任何一个烂摊子它使用的线程?

How do I handle unexpected exceptions such as segfaults(at least for debugging) as well as kill signals so that I can close any connections and stop any threads running so that the process does not leave a mess of anything it is using?

推荐答案

您安装信号处理程序捕捉信号,它将愉快地关闭所有文件,插座,可用内存和关闭线程。

You install signal handlers to catch signals -- however in 99% of cases you just want to exit and let the Linux OS take care of the cleanup -- it will happily close all files, sockets, free memory and shutdown threads.

所以,除非有什么具体你想做的事情,就像上的插座发送消息,那么你应该从工艺只是退出,不要试图捕捉信号。

So unless there is anything specifically you want to do, like sending a message on the sockets, then you should just exit from the process and not try to catch the signal.

这篇关于Linux下C醒目的正常终止终止信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆