「c++」signal函数

信号机制是进程之间相互传递消息的一种方法,信号全称为软中断信号,也有人称软中断。从它的命名可以看出,它的实质和使用很像中断,所有,信号可以说是进程控制的一部分。
信号signal处理是Linux程序的一个特色,用信号处理来模拟操作系统的中断功能。

头文件

#include <signal.h>

#include<csignal> #include <signal.h> 是处理信号的C-library。该库包含 signalraise 两个功能函数。

  • 函数 signal 用于捕获信号,可指定信号处理的方式。
  • 函数 raise产生一个信号,并向当前正在执行的程序发送该信号

一、signal()函数

函数signal 用于捕获信号,可指定信号处理的方式,函数声明如下:

void (*signal(int sig, void (*func)(int)))(int);

第一个参数 sig 指明了所要处理的信号类型,它可以取除了SIGKILL和SIGSTOP外的任何一种信号。

第二个参数描述了与信号关联的动作,下面将会对这三个参数进行讲解。

  • 当程序收到信号的进程对各种信号有不同的处理方法。处理方法可以分为三类:
        I. 默认处理:对信号进行该信号的系统默认处理,第二参数为 SIG_DFL
        II. 忽略信号:忽略该信号,第二参数为 SIG_IGN
        III. Function handler:指定处理函数,由该函数来处理,第二参数为 函数指针
  • 若signal函数中第二参数(函数指针)中通过 raise发出与signal函数对应信号类型相同的信号。程序将在signal函数这成为死循环,自己发信号,自己捕获,进程暂停。

例如:
signal(SIGINT, signalHandler); 代表当程序检测到SIGINT信号的时候,执行signalHandler函数。SIGINT信号是程序终止(interrupt)信号,故当你按ctrl+c时,就相当于释放了程序终止(interrupt)信号,就会自动调用signalHandler函数。

二、raise()函数

函数raise 产生一个信号sig,并向当前正在执行的程序发送信号sig,其声明如下:

int raise (int sig);

重点: 值得注意的是,raise发出的信号,可被当前进程中同类型型号的signal函数捕获。

其他信号

系统的信号我们可以在终端键入kill -l查看(共64个)。其实这些信号时系统定义的宏。
在这里插入图片描述一些常用的Signal 如下:

  Signal		Description
  SIGALRM		用alarm函数设置的timer超时或setitimer函数设置的interval timer超时
  SIGBUS		某种特定的硬件异常,通常由内存访问引起
  SIGCANCEL		由Solaris Thread Library内部使用,通常不会使用
  SIGCHLD		进程Terminate或Stop的时候,SIGCHLD会发送给它的父进程。缺省情况下该Signal会被忽略
  SIGCONT		当被stop的进程恢复运行的时候,自动发送
  SIGEMT		和实现相关的硬件异常
  SIGFPE		数学相关的异常,如被0除,浮点溢出,等等
  SIGFREEZE		Solaris专用,Hiberate或者Suspended时候发送
  SIGHUP		发送给具有Terminal的Controlling Process,当terminal 被disconnect时候发送
  SIGILL		非法指令异常
  SIGINFO		BSD signal。由Status Key产生,通常是CTRL+T。发送给所有Foreground Group的进程
  SIGINT		由Interrupt Key产生,通常是CTRL+C或者DELETE。发送给所有ForeGround Group的进程
  SIGIO			异步IO事件
  SIGIOT		实现相关的硬件异常,一般对应SIGABRT
  SIGKILL		无法处理和忽略。中止某个进程
  SIGLWP		由Solaris Thread Libray内部使用
  SIGPIPE		在reader中止之后写Pipe的时候发送
  SIGPOLL		当某个事件发送给Pollable Device的时候发送
  SIGPROF		Setitimer指定的Profiling Interval Timer所产生
  SIGPWR		和系统相关。和UPS相关。
  SIGQUIT		输入Quit Key的时候(CTRL+\)发送给所有Foreground Group的进程
  SIGSEGV		非法内存访问
  SIGSTKFLT		Linux专用,数学协处理器的栈异常
  SIGSTOP		中止进程。无法处理和忽略。
  SIGSYS		非法系统调用
  SIGTERM		请求中止进程,kill命令缺省发送
  SIGTHAW		Solaris专用,从Suspend恢复时候发送
  SIGTRAP		实现相关的硬件异常。一般是调试异常
  SIGTSTP		Suspend Key,一般是Ctrl+Z。发送给所有Foreground Group的进程
  SIGTTIN		当Background Group的进程尝试读取Terminal的时候发送
  SIGTTOU		当Background Group的进程尝试写Terminal的时候发送
  SIGURG		当out-of-band data接收的时候可能发送
  SIGUSR1		用户自定义signal 1
  SIGUSR2		用户自定义signal 2
  SIGVTALRM		setitimer函数设置的Virtual Interval Timer超时的时候
  SIGWAITING	Solaris Thread Library内部实现专用
  SIGWINCH		当Terminal的窗口大小改变的时候,发送给Foreground Group的所有进程
  SIGXCPU		当CPU时间限制超时的时候
  SIGXFSZ		进程超过文件大小限制
  SIGXRES		Solaris专用,进程超过资源限制的时候发

参考

C++ Signal(信号)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值