无论如何可以将参数传递给信号处理程序吗?
我在 main 中注册了一个 SIGTERM 处理程序,如下所示:
signal(SIGTERM, sigterm_handler);
该处理程序很简单:
void sigterm_handler()
{ exit(1); }
如果我需要向处理程序传递一些参数(例如 2 个指针或其他参数)怎么办?如何在信号函数中注册处理程序?或者至少......有什么办法可以实现这一目标吗?
注意:该进程是被第三方进程杀死的,而不是被它自己杀死的。但在关闭之前,我需要手动释放一些结构并将它们写入文件。
I'm registering in the main an handler for the SIGTERM like this:
signal(SIGTERM, sigterm_handler);
And the handler is a simple:
void sigterm_handler()
{ exit(1); }
What if I need to pass to the handler a few arguments like a 2 pointers or anything else? How do I register the handler in the signal function? Or at least... is there anyway to achieve that?
Notice: the process is killed by a third party process, not by itself. But before closing I need to manually free some structures and write them on files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您打算从哪里传递参数?唯一一次发送信号是执行您正在执行的操作的合理方式是当您要终止的进程不是进程本身时,但在这种情况下,指针将毫无意义。您可以使用 sigqueue 函数和 SA_SIGINFO 类型信号处理程序传递指针,但在我看来,您不理解信号或没有任何充分的理由在第一名。当你想退出而不是发出信号时只需进行函数调用......
Where do you intend to pass the arguments from? The only time sending a signal would be a reasonable way to do what you're doing is when the process you're terminating is not the process itself, but in that case, pointers would be meaningless. You can pass a pointer with the
sigqueue
function andSA_SIGINFO
type signal handlers, but it looks to me like you don't understand signals or have any good reason for using them in the first place. Just make a function call when you want to exit rather than raising signals...只需使用全局变量即可。正如其他人指出的那样,您的信号处理程序将由不受您控制的调用堆栈调用。
您可以考虑使用
setjmp()
和longjmp()
将控制从信号处理程序转移到先前执行的代码路径。这种老派的方法对你来说可能真的很有趣。Simply use a global variable. As others have pointed out, your signal handler will be invoked by a call stack not under your control.
You might consider using
setjmp()
andlongjmp()
to transfer control from your signal handler to a previously-executed code path. This old-school approach could be really fun for you.您不能将参数传递给处理程序 - 因为您不是调用处理程序的人。处理程序将从您无法控制的地方调用。
您可以通过调用
signal
或sigaction
来注册处理程序。马里奥
you cannot pass an argument to the handler - since you are not the one calling the handler. The handler will be called from somewhere outside your control.
you register a handler either with a call to
signal
orsigaction
.hth
Mario
信号通常是异步出现的,这意味着不是来自程序中的特定位置,因此您无法正常传递参数。如果我在程序运行时按 Control-C,信号处理程序应该获得什么指针?
如果您在程序中构造了一个信号并故意引发它,则只能传递有意义的参数(如果 C 允许,但它不允许),在这种情况下,您应该调用一个函数。
因此,如果您想在程序中调用某些内容,请使用函数。如果您希望在存在外部事件时调用一个函数,而该函数对您正在处理的内容一无所知,请使用信号处理程序。
Signals normally come asynchronously, meaning not from specific spots in the program, and therefore you can't pass arguments normally. If I were to press control-C while the program was running, what pointers should the signal handler get?
You could only pass meaningful arguments (if C would allow that, which it doesn't) if you constructed a signal within your program and deliberately raised it, and in such a case you should call a function instead.
So, if you want to call something in your program, use a function. If you want a function called if there's an external event, which will know nothing of what you're processing, use a signal handler.