Linux操作系统:信号
信号的基本介绍
信号是系统响应某个条件而产生的事件,进程接收到信号会执行响应的操作;
(1)信号的储存位置
vim /usr/include/x86_64-linux-gnu/bits/signum.h 旧版
新版:
vim /usr/include/x86_64-linux-gnu/bits/signum-arch.h
vim /usr/include/x86_64-linux-gnu/bits/signum-generic.h
(2)常见信号对应的功能
以下是提取的内容:
SIGABORT:进程异常终止
SIGALRM:超时警告
SIGFPE:浮点运算异常
SIGHUP:连接挂断
SIGILL:非法指令
SIGINT:终端中断
SIGKILL:终止进程(此信号不能被捕获或忽略)
SIGPIPE:向无读进程的管道写数据
SIGQUIT:终端退出
SIGSEGV:无效内存段访问
SIGTERM:终止
SIGUSR1:用户定义信号1
SIGUSR2:用户定义信号2
信号名称 信号代号 #define SIGHUP 1
#define SIGINT 2 //键盘按下 Ctrl+c 时,会产生终端中断信号
#define SIGQUIT 3//键盘按下 Ctrl+\ 时,会产生终端退出信号
#define SIGILL 4
#define SIGTRAP 5
#define SIGABRT 6
#define SIGIOT 6
#define SIGBUS 7
#define SIGFPE 8
#define SIGKILL 9 //该信号的响应方式不允许改变
#define SIGUSR1 10
#define SIGSEGV 11
#define SIGUSR2 12
#define SIGPIPE 13 //读端关闭的描述符,写端写入时产生,
//该信号会终止程序(向无读进程的管道写数据)
#define SIGALRM 14
#define SIGTERM 15 //系统 kill 命令默认发送的信号
#define SIGSTKFLT 16
#define SIGCHLD 17 //子进程结束后,内核会默认给父进程发送该信号
#define SIGCONT 18
#define SIGSTOP 19
#define SIGTSTP 20
#define SIGTTIN 21
#define SIGTTOU 22
#define SIGURG 23...
响应方式
三种响应方式:默认,忽略,自定义;
演示默认的处理方式;
也就是收到信号了,进程按照信号默认的方式去处理;
代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
int main()
{while(1){printf("main run\n");sleep(1);}exit(0);
}
键盘按下ctrl+c时,其实就是因为该进程收到了一个信号:SIGNT----终端中断的信号。(2号新号);就是说,在键盘上按下ctrl+c时,会给当前终端前台执行的进程发送SIGINT信号;