2014-11-05 70 views
1

我想从我的代码中启动计算器应用程序,用sigint-2中断它表明它已被中断,再次启动它,然后用sigquit-9退出它,想法是在它内部中断它C代码,因此没有必要按ctrl-c或ctrl- \SIGINT和SIGQUIT

编写一个C程序,通过signalfd文件描述符接受信号SIGINT和SIGQUIT。程序在接受SIGQUIT信号后终止。

+0

什么是你的问题? – 2014-11-05 01:18:27

+0

什么是C语言的语法来启动一个进程,然后中断它,然后结束它? – FlipFlopSquid 2014-11-05 01:26:55

回答

0

我想这可能是你在找什么

// 
// 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; 
} 
+0

表示有点令人毛骨悚然。你们相处得很好吗? :) – 2014-11-05 19:59:09