且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何避免scanf函数用C我的信号处理函数后?

更新时间:2022-12-24 21:04:41

信号处理程序的的中断 scanf函数及其阅读STDIN的>。然而,由于你的方式设置信号配置,系统调用立刻在信号处理程序返回重新启动。这就是为什么你在卡在 scanf函数,而不是回到你的循环的顶端。

The signal handler is interrupting scanf during its read of STDIN. However, because of the way you set signal disposition, the read system call restarts immediately upon return of the signal handler. That's why you are "stuck" in the scanf rather than back at the top of your loop.

您可以做一个重要的事情是要使用的sigaction ,而不是信号 。这将迫使你指定中断通话的行为:重新启动与否

One important thing you can do is to use sigaction rather than signal. This will force you to specify the behavior of interrupted calls: restart them or not?

接下来要做的事情就是将信号处理程序限制为那些异步信号安全功能,免得你风险苦难

The next thing to do is to limit your signal handlers to functions that are async-signal-safe, lest you risk misery.

顺便说一句,另一个变化做出是给我们所有的要求包括:(< unistd.h中> )和定义( _GNU_SOURCE ?),让您的工作方案。

As an aside, another change to make is to give us all the required includes (<unistd.h>?) and defines (_GNU_SOURCE ?) to make your program work.