[技術] Linux攔截Signal程式

Written on 7:57 下午 by Yu Lai

From: http://blog.csdn.net/lcrystal623/archive/2007/03/05/1521159.aspx

攔截鍵盤Ctrl + C,程式執行後,進入while(1)迴圈,當按下Ctrl + C後,執行signal函數指定的函式fun,輸出字串。
代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void fun(int signal)
{
printf("Hello World!\n");
exit(0);
}

int main()
{
signal(SIGINT,fun);

while(1)
{
;
}
}

If you enjoyed this post Subscribe to our feed

No Comment

張貼留言