1 What are signals and how are they used

1.当进程接收到信号,进程会暂停,来处理信号

1.1 How we use signals

1.killall cat 杀死所有的cat程序
2.信号源与信号

信号源 信号
kill SIGTERM
Ctrl-c SIGINT
Ctrl-z SIGTSTP
fg SIGCONT

2 The Wide World of Signals

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

2.1 Signal Names and Values

  1. each signal has a name, value, and default action.
  2. sys/signal.h
#define SIGHUP  1       /* hangup */
#define SIGINT  2       /* interrupt */
#define SIGQUIT 3       /* quit */
#define SIGILL  4       /* illegal instruction (not reset when caught) */
#define SIGTRAP 5       /* trace trap (not reset when caught) */
#define SIGABRT 6       /* abort() */
#define SIGPOLL 7       /* pollable event ([XSR] generated, not supported) */
#define SIGFPE  8       /* floating point exception */
#define SIGKILL 9       /* kill (cannot be caught or ignored) */

2.2 Default Actions of Signals

动作 描述
Term The process will terminate
Core The process will terminate and produce a core dump file that traces the process state at the time of termination.
Ign The process will ignore the signal
Stop The process will stop, like with a Ctrl-Z
Cont The process will continue from being stopped

3 Signals from the Command Line

3.1 Preparing for the kill

3.2 Sending Terminal Signals with Kill

4 Handling and Generating Signals

4.1 Hello world of Signal Handling

1.int signal(int signum, void (*handler)(int))
2.参数1,接收到的信号 参数2:接收到指定信号后调用的函数

#include <stdlib.h>
#include <stdio.h>#include <signal.h> /*for signal() and raise()*/void hello(int signum){printf("Hello World!\n");
}int main(){//execute hello() when receiving signal SIGUSR1  signal(SIGUSR1, hello); //再执行//send SIGUSR1 to the calling process  raise(SIGUSR1); //先执行
}

4.2 Asynchronous Execution

1.asynchronous,当程序接收到信号,会暂停,去处理信号,然后再接着刚才的暂停继续执行
2.下面的程序无法用ctrl+c来退出,因为程序接收到ctrl+c的信号后,会执行hello,然后继续循环.所以可以发送ctrl + \来停止发送SIGSTOP信号,此信号不能被处理和忽略

/* hello_loop.c*/
void hello(int signum){printf("Hello World!\n");
}int main(){//Handle SIGINT with hellosignal(SIGINT, hello);//loop forever!while(1);}

4.3 Inter Process Communication

1.int kill(pid_t pid, int signum);
2. `kill函数

/*ipc_signal.c*/
void hello(){printf("Hello World!\n");
}int main(){pid_t cpid;pid_t ppid;//set handler for SIGUSR1 to hello()signal(SIGUSR1, hello);if ( (cpid = fork()) == 0){/*CHILD*///get parent's pidppid = getppid();//send SIGUSR1 signal to parrentkill(ppid, SIGUSR1);exit(0);}else{/*PARENT*///just wait for child to terminatewait(NULL);}}

4.4 Ignoring Signals

  1. 忽略信号有两种方法

    1. SIG_IGN : Ignore the signal
    2. SIG_DFL : Replace the current signal handler with the default handler
int main(){// using SIG_IGNsignal(SIGINT, SIG_IGN);while(1);
}

4.5 Changing and Reverting to the default handler

1.在handler中可以再次接收信号

/*you_shot_me.c*/
void handler_3(int signum){printf("Don't you dare shoot me one more time!\n");//Revert to default handler, will exit on next SIGINTsignal(SIGINT, SIG_DFL);
}void handler_2(int signum){printf("Hey, you shot me again!\n");//switch handler to handler_3signal(SIGINT, handler_3);
}void handler_1(int signum){printf("You shot me!\n");//switch handler to handler_2signal(SIGINT, handler_2);
}int main(){//Handle SIGINT with handler_1signal(SIGINT, handler_1);//loop forever!while(1);}

4.6 Some signals are more equal than others

1.SIGSTOP ctrl+z不可处理

/* ignore_stop.c */
int main(){//ignore SIGSTOP ?signal(SIGSTOP, SIG_IGN);//infinite loopwhile(1);}

2.SIGKILL kill -SIGKILL 不可处理

int main(){//ignore SIGSTOP ?signal(SIGKILL, SIG_IGN);//infinite loopwhile(1);}

4.7 Checking Errors of signal()

/*signal_errorcheck.c*/
int main(){//ignore SIGSTOP ?if( signal(SIGKILL, SIG_IGN) == SIG_ERR){perror("signal");;exit(1);}//infinite loopwhile(1);}

19 Signals and Signal Handling相关推荐

  1. C++ Report:应用设计模式去简化信号控制

    译者:封尘浪 http://blog.csdn.net/qqzwp ------------------------------------------------------------------ ...

  2. linux signal 处理

    linux signal 处理 说明: 本文主要翻译自ULK 3rd chapter 11. 主要受 http://blog.csdn.net/yunsongice 影响,故发表在csdn. 另外,本 ...

  3. signal信号的处理过程

    signal信号的处理过程 mips架构下signal信号的处理.信号是linux下非常重要的部分,把这几天看的整理一下. 执行kill -l列出所有的信号 HUP INT QUIT ILL TRAP ...

  4. signal 这个软中断是怎么实现的

    为什么80%的码农都做不了架构师?>>>    struct signal_struct {atomic_t count;atomic_t live;wait_queue_head_ ...

  5. 从kernel源代码的角度分析signal的错误用法和理解

    读这份文档之前,建议先浏览一下 <Unix Advanced Programming >里面的signal 一章和下面这份出 自IBM 论坛的文章:进程间通信 信号(上) http://w ...

  6. 【转贴】gdb中的信号(signal)相关调试技巧

    一篇不错的帖子,讲的是gdb中的信号(signal)相关调试技巧 转自Magic C++论坛  http://www.magicunix.com/index_ch.html  http://www.m ...

  7. Signal Processing Toolbox

    MathWorks官网是个宝,没事就去逛逛兴许就能学到东西.MATLAB用户很多很多,但会去逛MathWorks官网的人并不多.官网有很多好东西,只是上面的内容实在太多了,在不熟悉官网架构的情况下要找 ...

  8. 介绍 Preact Signals

    1. 什么是 Signals? Signals 是用来处理状态的一种方式,它参考自 SolidJS,吸收了其大部分的优点.无论应用多么复杂,它都能保证快速响应. Signals 的独特之处在于状态更改 ...

  9. gdb中的信号(signal)相关调试技巧

    一篇不错的帖子,讲的是gdb中的信号(signal)相关调试技巧 转自Magic C++论坛 http://www.magicunix.com/index_ch.html http://www.mag ...

最新文章

  1. matlab朴素贝叶斯手写数字识别_基于MNIST数据集实现手写数字识别
  2. netty源码分析系列——EventLoop
  3. lt li gt html,lt;ligt;...这个符号什么意思,放在中间有什么作用?
  4. 最大熵模型(Maximum Etropy)—— 熵,条件熵,联合熵,相对熵,互信息及其关系,最大熵模型。。...
  5. Linux常用命令大全(转载收藏)
  6. springboot去掉git版本控制_关于 Git 提交这些规范,你都遵守了吗?
  7. 计算机教育的进制转换,计算机数制及编码进制转换公开课教学教育资料.doc
  8. onnx模型部署 python_onnxruntime模型部署流程
  9. matlab车辆测距,一种基于单目视觉的车辆测距方法
  10. Spring :事务使用的注意事项
  11. PL/SQL数据类型
  12. 华为简单静态路由配置
  13. 虚拟机安装与双系统(win10+ubuntu)安装及其他
  14. 用Qt实现一个桌面弹幕程序(六)-- -- 桌面客户端实现②
  15. Python sys模块常见函数
  16. 《高效能青少年的七个习惯》读后感作文3700字
  17. KVM多电脑切换器的KVM发展史
  18. layui table 表头合并_LayUI Table复杂表头实现
  19. web前端开发主要培训哪些内容
  20. ping +域名 具体能做什么

热门文章

  1. java eml解析_javamail 收邮件 解析eml文件
  2. rabbitmq 手动提交_第四章----SpringBoot+RabbitMQ发送确认和消费手动确认机制
  3. Python生成CSV文件模拟某小区用户手机通话记录
  4. 微课|《Python编程基础与案例集锦(中学版)》第2章(2)
  5. 微课|中学生可以这样学Python(3.4节):选择结构的嵌套
  6. Python截屏扩展库pyscreenshot安装与使用
  7. 虚拟Python环境可以这样创建
  8. 有没有什么方法快速能找到导致软件崩溃的进程_手机软件闪退闪得怀疑人生?看我专治闪退二十年!...
  9. 业务流程图6个图例_史上最全PID图例攻略!
  10. Linux 一个进程如何从用户态切换到内核态运行