linux signals 进程信号简介 -凯发k8官方网
2019独角兽企业重金招聘python工程师标准>>>
在linux中,signal是一种局限较强的进程间通信方式。
主要有三种类型:
1. 从hardware signal到posix signal
hardware signal也叫hardware interrupt,cpu中存放着 interrupt vectors 指向 存放在kernel space中的interrupt handling routine。
例: 键盘输入;segmentation fault -> sigsegv
2. 直接从kernel生成的posix signal
例:exit() -> sigchld
3. 从一个进程发起到另一个进程的signal
例:进程885 执行了 “kill 1234” 代码
posix signal
1. generated from cpu to kernel, or from cpu to processes
例 1: segmentation fault.
the signal is labeled sigsegv, which comes from cpu to kernel then to process.
例 2: floating point exception (e.g., divided by 0).
the signal is labeled sigfpe, which is coming from cpu to process
2. from kernel to process
例: child process termination.
the signal is labeled sigchld, which is coming from the kernel to process
3. generated from one process to another
例: from terminals: e.g., “ctrl c”, “ctrl z”
using programs: e.g., “kill”, “top”, etc.
using the “kill()” system call.
一些典型的signal
asynchronous signal: 异步信号
– the signal received is not generated by the process itself。 so, its arrival time is usually not deterministic
– e.g., external hardware interrupt, another process sends ctrl-c
synchronous signal: 同步信号
– the signal is caused by the process itself, its arrival time is usually deterministic
– e.g., a certain line leads to sigfpe
– e.g., a certain line accesses a memory region: sigsegv
kill() 方法
用于给特地进程发送posix signal
signal() 方法
更新当前进程的特定signal的处理方法,即signal handler
例:写个无法“ctrl c”中止的小病毒
#include
通常情况下,我们希望当signal handler处理完signal后,程序恢复到之前执行的地方;而有时候,则希望程序继续运行下去。
signal的检查方法
一个进程如何知道它收到了一个特定的signal?
在kernel space中,有一个bitmask
进程会不定期地去检查这个bitmask,如果看到是1,则调用对应的handler,并重置为0。
因此,发送signal和处理signal是异步的。
pause()方法
the pause() system call suspends the calling process until a signal is received. 等待一个信号
有益的重写signal handler
重写sigint的handler,保证进程在被用户ctrl-c 中止之前,进行一些必要的清理工作比如 关闭数据库连接等。
alarm()方法
设置一个“闹钟”在指定时间后“响”起。
通过在hardware设置一个闹钟,到达时间后,hardware的signal会转换成sigalrm signal发送给进程,默认的处理方式是结束当前进程。
alarm()是一次性的,如果需要周期性的,则使用 settimer()方法
转载于:https://my.oschina.net/bruce370/blog/885260
总结
以上是凯发k8官方网为你收集整理的linux signals 进程信号简介的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: php安装加载yaf扩展
- 下一篇: javascript no overlo