C 语言 signal 函数,基本是根据系统中断,调用handler函数
Example:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
void sighandler(int);
int main()
{
signal(SIGINT, sighandler);
while(1)
{
printf("Going to sleep for a second...\n");
sleep(1);
}
return(0);
}
void sighandler(int signum)
{
printf("Caught signal %d, coming out...\n", signum);
exit(1);
}
Going to sleep for a second...
Going to sleep for a second...
Going to sleep for a second...
Going to sleep for a second...
Going to sleep for a second...
Caught signal 2, coming out...