简体   繁体   English

C 块信号

[英]C Block signals

I am trying to write a shell-like application and struggle with the signal blocking.我正在尝试编写一个类似 shell 的应用程序并与信号阻塞作斗争。 I want to disable/block some signals like SIGINT or SIGQUIT so when user press Ctrl+C or Ctrl+\, nothing will happen.我想禁用/阻止一些信号,如 SIGINT 或 SIGQUIT 所以当用户按下 Ctrl+C 或 Ctrl+\ 时,什么都不会发生。

I have done some research and was able to do that by using either signal(SIGQUIT, SIG_IGN) or sigaction() .我已经做了一些研究,并且能够通过使用signal(SIGQUIT, SIG_IGN)sigaction()来做到这一点。

My problem is that the signal is not totally blocked.我的问题是信号没有被完全屏蔽。 When I pressed Ctrl+\ several times inside my application, it will not exit as expected but will also display this ^\ .当我在我的应用程序中多次按下 Ctrl+\ 时,它不会按预期退出,但也会显示这个^\

Also, because I use an readline() to catch the input of the user, when I use Ctrl+\ and press Enter, there is a strange newline.另外,因为我使用readline()来捕获用户的输入,所以当我使用 Ctrl+\ 并按 Enter 时,会出现一个奇怪的换行符。

Seems like you blocked signals properly.好像你正确地阻止了信号。 As you disabled the signal, when you press Ctrl+\ then your terminal sends it to your application.当您禁用信号时,当您按下Ctrl+\时,您的终端会将其发送到您的应用程序。 The same way as a is sent when you press "A" on your keyboard, but unlike "A", Ctrl+\ does not generate any printable character, your terminal can not print it directly.与在键盘上按“A”时发送a方式相同,但与“A”不同的是, Ctrl+\不会生成任何可打印字符,您的终端无法直接打印。 ^\ is basically the special notation of that non-printable character. ^\基本上是该不可打印字符的特殊符号。 You will also get that "character" in readline() results, that is the strange thing are seeing.您还将在readline()结果中得到那个“字符”,这是奇怪的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM