如何接收SIGHUP信号后重新启动在Linux下C服务程序 [英] How to restart C daemon program in Linux after receiving SIGHUP signal

查看:568
本文介绍了如何接收SIGHUP信号后重新启动在Linux下C服务程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人能请张贴一些例如code我如何能重新读取配置文件和守护进程收到SIGHUP信号后重新启动我的守护进程。守护程序是用C语言编写的Linux用户空间程序,而不是由inetd启动。

Can anybody please post some example code on how I can reread a configuration file and restart my daemon after the daemon receives a SIGHUP signal. The daemon is a user space program written in C on Linux and is not started by inetd.

推荐答案

根据你的程序是怎么写的干净,有(至少)三种方式来做到这一点:

Depending on how cleanly your program is written, there are (at least) three ways to do that:


  1. 在接收到信号后,返回到程序的开始,在初始化阶段之前(可能 - 但不一定 - 经由setjmp的()/ longjmp的()或此时sigsetjmp()/ siglongjmp()对) ,以此来重置并重新读取配置文件。

  1. On receipt of the signal, return to the start of the program, before the initialization phase (possibly - but not necessarily - via a setjmp()/longjmp() or sigsetjmp()/siglongjmp() pair), thus resetting and rereading the configuration file.

在收到信号,使信号处理程序再次执行的是原来的程序。这有失去所有国家和重置所有全局和静态变量回到其原始状态的优点。它失去所有previous状态的记过处分。

On receipt of the signal, have the signal handler exec the original program again. This has the merit of losing all the state and resetting all globals and static variables back to their original state. It has the demerit of losing all previous state.

第三个选项是那么残酷,也许;它将注意该信号已经被接收,并在该主处理循环的下一个方便的点,将回去读取配置文件

The third option is less brutal, perhaps; it would note that the signal has been received and at the next convenient point in the main processing loop, would go back and reread the configuration file.

什么作品,在某种程度上取决于你的守护进程必须做的一部分。如果在与客户交谈花时间,你可能不希望使用的选项1或2任 - 你会preFER使用选项3.如果你正在做一次性回答简单的问题,残酷方法可能是有效的(和可能是简单的程序)。请注意,选项1要求WIP(工作正在进行中)和事物的谨慎处理,如打开的文件 - 如果你不小心,你将失去的轨道资源,守护进程会失败(内存,超出文件描述符 - 大多数这两个有可能的)。

What works depends in part on what your daemon has to do. If it spends time in a conversation with its clients, you may not want to use either of the options 1 or 2 - you would prefer to use option 3. If you are doing one-shot answers to simple questions, the brutal approaches may be effective (and are probably simpler to program). Note that option 1 requires careful handling of the WIP (work in progress) and things such as open files - if you are not careful, you will lose track of resources, and the daemon will fail (out of memory, out of file descriptors - most likely one of these two).

这篇关于如何接收SIGHUP信号后重新启动在Linux下C服务程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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