ctrlc1.c

来自「《Beginning Linux Programming》书的配置实例源代码。」· C语言 代码 · 共 31 行

C
31
字号
/*  We'll start by writing the function which reacts to the signal    which is passed in the parameter sig.    This is the function we will arrange to be called when a signal occurs.    We print a message, then reset the signal handling for SIGINT    (by default generated by pressing CTRL-C) back to the default behavior.    Let's call this function ouch.  */#include <signal.h>#include <stdio.h>#include <unistd.h>void ouch(int sig){    printf("OUCH! - I got signal %d\n", sig);    (void) signal(SIGINT, SIG_DFL);}/*  The main function has to intercept the SIGINT signal generated when we type Ctrl-C .    For the rest of the time, it just sits in an infinite loop,    printing a message once a second.  */int main(){    (void) signal(SIGINT, ouch);    while(1) {        printf("Hello World!\n");        sleep(1);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?