大家好,又见面了,我是你们的朋友全栈君。
信号是Unix和Linux系统响应某些条件而产生的一个事件。接收到该信号的进程会相应地采取一些操作。
每个信号都有一个数字编码。
实例:Ctrl-C的工作原理
1.用户输入ctrl-c
2.驱动程序受到字符
3.匹配VINTR和ISIG的字符被开启
4.驱动程序调用信号系统
5.信号系统发送SIGINT到进程
6.进程受到SIGINT
7.进程消亡
信号来自于内核,生成信号的请求可以来源于用户,内核,进程。
用户:比如Ctrl-C,Ctrl-/.当然这些也可以改变(使用stty改变VINTR控制字符)
内核:非法段存取,浮点溢出等进程运行出错。
进程:一个进程可以通过系统调用kill给另一个进程发送信号;进程间也可以通过信号通信。
注:同步信号-–由进程的某个操作产生的信号,比如被零除;
异步信号-–由进程外部的事件引起的信号,比如用户的击键;
信号的名称在signal.h中定义(一般/usr/include/signal.h)。
man 7 signal 可以查看signal的相关知识和用法等。
常见的有:
caoli@caoli-laptop:~$ man 7 signal
First the signals described in the original POSIX.1-1990 standard.
Signal Value Action Comment
──────────────────────────────────────────────────────────────────────
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no
readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Cont Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at tty
SIGTTIN 21,21,26 Stop tty input for background process
SIGTTOU 22,22,27 Stop tty output for background process
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
Next the signals not in the POSIX.1-1990 standard but described in
SUSv2 and POSIX.1-2001.
Signal Value Action Comment
────────────────────────────────────────────────────────────────────
SIGBUS 10,7,10 Core Bus error (bad memory access)
SIGPOLL Term Pollable event (Sys V).
Synonym for SIGIO
SIGPROF 27,27,29 Term Profiling timer expired
SIGSYS 12,-,12 Core Bad argument to routine (SVr4)
SIGTRAP 5 Core Trace/breakpoint trap
SIGURG 16,23,21 Ign Urgent condition on socket (4.2BSD)
SIGVTALRM 26,26,28 Term Virtual alarm clock (4.2BSD)
SIGXCPU 24,24,30 Core CPU time limit exceeded (4.2BSD)
SIGXFSZ 25,25,31 Core File size limit exceeded (4.2BSD)
Up to and including Linux 2.2, the default behavior for SIGSYS, SIGX‐
CPU, SIGXFSZ, and (on architectures other than SPARC and MIPS) SIGBUS
was to terminate the process (without a core dump). (On some other
Unix systems the default action for SIGXCPU and SIGXFSZ is to terminate
the process without a core dump.) Linux 2.4 conforms to the
POSIX.1-2001 requirements for these signals, terminating the process
with a core dump.
Next various other signals.
Signal Value Action Comment
────────────────────────────────────────────────────────────────────
SIGIOT 6 Core IOT trap. A synonym for SIGABRT
SIGEMT 7,-,7 Term
SIGSTKFLT -,16,- Term Stack fault on coprocessor (unused)
SIGIO 23,29,22 Term I/O now possible (4.2BSD)
SIGCLD -,-,18 Ign A synonym for SIGCHLD
SIGPWR 29,30,19 Term Power failure (System V)
SIGINFO 29,-,- A synonym for SIGPWR
SIGLOST -,-,- Term File lock lost
SIGWINCH 28,28,20 Ign Window resize signal (4.3BSD, Sun)
SIGUNUSED -,31,- Term Unused signal (will be SIGSYS)
(Signal 29 is SIGINFO / SIGPWR on an alpha but SIGLOST on a sparc.)
SIGEMT is not specified in POSIX.1-2001, but nevertheless appears on
most other Unix systems, where its default action is typically to ter‐
minate the process with a core dump.
SIGPWR (which is not specified in POSIX.1-2001) is typically ignored by
default on those other Unix systems where it appears.
SIGIO (which is not specified in POSIX.1-2001) is ignored by default on
several other Unix systems.
进程处理信号的方式有三种:默认,忽略,调用一个函数。
1.默认:(通常是消亡)
针对SIGINT来说,它的默认处理是消亡,可以用以下调用来恢复默认值
signal(SIGINT,SIG_DFL);
2.忽略信号
程序可以通过以下调用来告诉内核他忽略SIGINT信号。
signal(SIGINT,SIG_IGN);
3.调用函数
程序告诉内核,当信号来时,应该调用哪个函数。那个函数称为信号处理函数;
signal(signum,functionname);
关于signal库函数
#include <signal.h>
void (*signal(int sig,void (*func)(int))) (int);
遇到错误返回-1
执行成功返回prevcation
func为信号处理函数,也可以用SIG_DFL和SIG_IGN来替代。
signal(SIGINT,SIG_DFL)里面可以用SIG_IGN或者f替代。
关于发送信号和sigaction见下节
我好累阿,回去睡觉。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136740.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...