是否可以删除信号
Is it possible to delete signal
是否可以删除/取消映射/重新映射信号?
例如,ctrl-c 实际上发送了一个 SIGINT 信号。
我可以修改它,以便 ctrl-c 按键不会发出信号,而是将其 ascii 值写入标准输入,就像任何其他键一样吗?
不知道我说的是否清楚,不客气的追问
编辑:
我希望我的终端停止响应 ctrl-c 作为信号
您可以在代码中捕获信号并按您的意愿处理它们。
这是一个基本的例子,你需要做的是注册回调来处理你想要捕获的信号。在这种情况下,SIGINT
:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
void sig_handler(int signo)
{
if (signo == SIGINT) {
printf("received SIGINT\n");
}
}
int main(void)
{
if (signal(SIGINT, sig_handler) == SIG_ERR) {
printf("\ncan't catch SIGINT\n");
}
// A long long wait so that we can easily issue a signal to this process
while(1) {
sleep(1);
}
return 0;
}
因此,在此示例中,sig_handler()
函数被注册为使用 signal(SIGINT, sig_handler)
调用来处理 SIGINT
信号。
here 中有更多示例。
在POSIX系统上,您可以控制哪个字符发送SIGINT,或者将其设置为无字符。
struct termios t;
if (tcgetattr(STDIN_FILENO, &t) == 0) {
t.c_cc[VINTR] = 0; // set the INT character to 0 (disable)
tcsetattr(STDIN_FILENO, TCSANOW, &t);
} else {
// stdin is not a terminal
}
是否可以删除/取消映射/重新映射信号? 例如,ctrl-c 实际上发送了一个 SIGINT 信号。 我可以修改它,以便 ctrl-c 按键不会发出信号,而是将其 ascii 值写入标准输入,就像任何其他键一样吗?
不知道我说的是否清楚,不客气的追问
编辑:
我希望我的终端停止响应 ctrl-c 作为信号
您可以在代码中捕获信号并按您的意愿处理它们。
这是一个基本的例子,你需要做的是注册回调来处理你想要捕获的信号。在这种情况下,SIGINT
:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
void sig_handler(int signo)
{
if (signo == SIGINT) {
printf("received SIGINT\n");
}
}
int main(void)
{
if (signal(SIGINT, sig_handler) == SIG_ERR) {
printf("\ncan't catch SIGINT\n");
}
// A long long wait so that we can easily issue a signal to this process
while(1) {
sleep(1);
}
return 0;
}
因此,在此示例中,sig_handler()
函数被注册为使用 signal(SIGINT, sig_handler)
调用来处理 SIGINT
信号。
here 中有更多示例。
在POSIX系统上,您可以控制哪个字符发送SIGINT,或者将其设置为无字符。
struct termios t;
if (tcgetattr(STDIN_FILENO, &t) == 0) {
t.c_cc[VINTR] = 0; // set the INT character to 0 (disable)
tcsetattr(STDIN_FILENO, TCSANOW, &t);
} else {
// stdin is not a terminal
}