函数原型
NAME signal - ANSI C signal handling SYNOPSIS #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);
signal函数具有注册功能,什么事都不干。只是告诉系统,当来信号signum时,按handler的方式处理。只有来信号时,才会调用这个函数。信号不来,永远不会调用这个函数。
用户能够通过输入CTRL+c、Ctrl+,或者是终端驱动程序分配给信号控制字符的其他任何键来请求内核产生信号。
ctrl + c --> 2)SIGINT
ctrl + --> 3)SIGQUIT
以上两者都可以让程序退出。
代码
/************************************************************************* > File Name: hello.c > Author: KrisChou > Mail:zhoujx0219@126.com > Created Time: Mon 25 Aug 2014 09:25:15 AM CST ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <assert.h> #include <signal.h> #include <sys/select.h> void handler(int num) { printf("sig_num: %d ", num); } int main(int argc, char* argv[]) { char buf[1024]; signal(2, handler); int iret ; fd_set read_set, ready_set ; FD_ZERO(&read_set); FD_SET(0, &read_set); while(1) { ready_set = read_set ; iret = select(1, &ready_set, NULL, NULL, NULL); if(iret == 0) { continue ; }else if(iret == -1) { perror("select"); continue ; }else { iret = read(0, buf, 1024); buf[iret] = '