本文介绍了从信号处理程序调用SysV msgsnd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从信号处理程序中调用msgsnd函数是否安全?

Is it safe to invoke msgsnd function from signal handler?

我们的服务代码并非旨在每一个正常完成,所以我没有退出点,但是当服务停止时,我需要向另一个进程发送消息,因此我需要捕获SIGTERM并执行msgsnd调用exit(0).

Code for our services is not intended to every gracefully complete, so I don't have exit point, however I need to send message to another process when services is stopped, so I need to catch SIGTERM and perform msgsnd before calling exit(0).

那样安全吗?

我查看了信号安全手册页并且在列表中未找到msgsnd.我应该认为这是不安全的功能吗?可能会有什么后果?

I looked into signal safety manual page and did not found msgsnd in the list. Should I consider this as unsafe function? What are the possible consequences?

推荐答案

不,这不安全. (请注意,exit()不会出现在 async-signal-安全功能列表,尽管_exit()确实如此.)

No, it's not safe. (Note that exit() doesn't appear on the async-signal-safe function list, either, though _exit() does.)

msgsnd可能会静默失败,可能会出错,可能出现段错误,可能永远挂起等.

msgsnd might silently fail, might error out, might segfault, might hang forever, etc.

考虑一下重构-如果您需要对SIGTERM做一些事情,那么我会说您 do 需要优美的,或者至少不是笨拙的关闭逻辑.如果那不可行,也许 SEM_UNDO 可以做你想要的,因为您似乎已经对SysV IPC感到满意.或持有在进程终止时释放的文件系统锁.或者让服务父捕获SIGCHLD,然后调用msgsnd.等等.

Consider refactoring a bit — if you need to do something upon SIGTERM then I'd say you do need graceful, or at least not clumsy, shutdown logic. If that's not feasible, perhaps SEM_UNDO can do what you want, as you seem to be comfortable with SysV IPC already. Or hold a file-system lock that is released upon process death. Or have the service parent catch SIGCHLD and then call msgsnd. Etc.

这篇关于从信号处理程序调用SysV msgsnd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 19:53