在Linux中链接信号处理程序的正确方法? [英] Proper way to chain signal handlers in linux?

查看:45
本文介绍了在Linux中链接信号处理程序的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个信号处理程序,我正在使用它们干净地退出程序,而且我希望通过链接信号处理程序调用来与其他钩住它们的程序很好地配合使用.我在手册页上使用sigaction表示信号已不再受欢迎.

I've got a couple signal handlers I'm using to exit my program cleanly, and I'd like to play nicely with whatever else has hooked them by chaining the signal handlers calls. I'm using sigaction per the man page for signal saying it's not preferred anymore.

查看sigaction结构,其中指定了信号掩码等,并带有几个标志.调用当前安装的处理程序以使所有这些选项都被考虑在内的正确"方法是什么?

Looking at the sigaction struct, there's signal masks and such that are specified, along with several flags. What's the "right" way to call the currently installed handler so that all those options are taken into account?

推荐答案

答案是这取决于":取决于信号处理程序的作用:

The answer is "it depends": on what the signal handlers do:

  • 许多人的第一个反应是,将使用信号处理程序来处理 SIGINT 等,并干净地终止程序.
  • 另一方面,有(或多或少)诸如 SIGWINCH 之类的良性信号(您希望停止程序).
  • The first reaction from many will be that a signal handler will be used to handle a SIGINT, etc., and terminate a program cleanly.
  • On the other hand, there are (more or less) benign signals such as SIGWINCH (which you would like to not stop your program).

在干净地终止程序"中,可能没有空间供其他程序做一些有用的事情.例如,建议的链接处理程序可能会关闭您依赖的文件.因此,您必须首先了解其他处理程序的功能.

In "terminate a program cleanly", there may not be room for other programs to do something useful. For instance, the proposed chained-handler may close files that you rely upon. So you have to start with a good knowledge of what the other handlers do.

如果信号处理程序位于后一类中,只需更新应用程序可以测试的变量,则信号处理程序 function 只是另一个函数.当您调用 signal 将信号与函数相关联时,该函数将返回前一个处理程序(它可能是魔术值之一,例如, SIG_DFL SIG_IGN).如果您保存了该代码,则可以检查是否全部都不存在,并且可以检查是否是真正的功能.调用 that 函数将继续执行,就好像它是当前信号处理程序的一部分一样.

If the signal handler is in the latter class, simply updating a variable which the application can test, then the signal handler function is just another function. When you call signal to associate a signal with a function, that returns the previous handler (which may be one of the magic values, e.g.,. SIG_DFL, SIG_IGN). If you save that, it's possible to check if it is none of those, and (presumably) a genuine function. Calling that function would continue execution as if it were part of your current signal handler.

这不是一个新主意(我在1990年代末期使用过),并偶尔进行讨论:

It is not a new idea (I used it in the late 1990s), and is discussed occasionally:

当然:

这篇关于在Linux中链接信号处理程序的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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