我想从我的代码中启动计算器应用程序,用sigint-2中断它,表明它已被中断,再次启动,然后用sigquit-9退出它,这个想法是在C代码中中断它,因此不需要按Ctrl + C或Ctrl + \
编写一个通过signalfd文件描述符接受信号SIGINT和SIGQUIT的C程序。接受SIGQUIT信号后,程序终止。
最佳答案
我想这可能就是您要寻找的
//
// main.c
// Project 4
//
// Found help with understanding and coding at
// http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/
//
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
//signal handling function that will except ctrl-\ and ctrl-c
void sig_handler(int signo)
{
//looks for ctrl-c which has a value of 2
if (signo == SIGINT)
printf("\nreceived SIGINT\n");
//looks for ctrl-\ which has a value of 9
else if (signo == SIGQUIT)
printf("\nreceived SIGQUIT\n");
}
int main(void)
{
//these if statement catch errors
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
if (signal(SIGQUIT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGQUIT\n");
//Runs the program infinitely so we can continue to input signals
while(1)
sleep(1);
return 0;
}
关于c - SIGINT和SIGQUIT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26747590/