首页 > 解决方案 > 进行系统调用时检测键盘中断的linux选项

问题描述

通常,当我在终端中运行程序时,我可以使用控制 C 来停止它。当我编写一个在 while 循环内调用操作系统命令的程序时,其余代码执行得如此之快,以至于无法停止除了杀死pid之外的程序。我一直在做的方法是添加 1 秒的睡眠,但修复它不是一个好习惯。

while(!phrasespotted){
     invoke an operating system command to arecord for 5 secs, during which control C can not stop the program 
     process the audio for phrase spotting 
     sleep(1); //during this I can stop the program in terminal 
}

我应该在我的代码中添加什么,以便它可以在进行系统调用时接收键盘中断,或者甚至注册它以在之后立即停止它?线程是基于捕获信号的唯一选择吗?https://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/

标签: linuxkeyboardinterrupt

解决方案


推荐阅读