2014-11-05 29 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