APUE--kill 和kill -9 的区别

有时候我们使用kill无法杀掉一个进程,但是用kill -9却可以,why?

     首先看一下kill的功能,man手册对kill描述如下:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. KILL(1)                                          Linux User's Manual                                          KILL(1)  
  2.   
  3. NAME  
  4.        kill - send a signal to a process  
  5.   
  6. SYNOPSIS  
  7.        kill [ -signal | -s signal ] pid ...  
  8.        kill [ -L | -V, --version ]  
  9.        kill -l  [ signal ]  
  10.   
  11. DESCRIPTION  
  12.        The  default  signal  for  kill  is TERM. Use -l or -L to list available signals.  Particularly useful signals  
  13.        include HUP, INT, KILL, STOP, CONT, and 0.  Alternate signals may be specified  in  three  ways:  -9  -SIGKILL  
  14.        -KILL.  Negative PID values may be used to choose whole process groups; see the PGID column in ps command out‐  
  15.        put. A PID of -1 is special; it indicates all processes except the kill process itself and init.  

           在NAME部分注明:kill的作用是向进程发送一个信号(并没有说是杀掉进程哈)。具体发送什么信号由后面接的参数决定。

           那么为什么很多时候我们又可以杀掉进程?请看DESDCRIPTION部分: kill默认参数是TERM。也就是说,如果没指定具体的信号作为参数,则默认使用kill TERM pid。因此kill pid是可以杀掉一个进程。

          那么问题来了,为什么有的时候kill不能杀掉进程,而kill -9就可以了?

          《APUE》中关于信号的描述中有介绍,大多数信号可以被捕获的。而TERM信号就是在这个大多数里的,一些进程可能为了特殊的用途捕获了TERM信号,导致了你使用kill pid时无法杀掉进程。 另外《APUE》中也强调了,有两个信号不能被捕获,SIGKILL 和SIGSTOP

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Note that the two signals SIGKILL and SIGSTOP can’t be caught.  

        没错,kill -9 就是向进程发送SIGKILL信号。


         kill的man手册:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. SIGNALS  
  2.        The signals listed below may be available for use with kill.  When known constant, numbers and default  behav‐  
  3.        ior are shown.  
  4.   
  5.        Name     Num   Action    Description  
  6.        0          0   n/a       exit code indicates if a signal may be sent  
  7.        ALRM      14   exit  
  8.        HUP        1   exit  
  9.        INT        2   exit  
  10.        KILL       9   exit      cannot be blocked  
  11.        PIPE      13   exit  
  12.        POLL           exit  
  13.        PROF           exit  
  14.        TERM      15   exit  
  15.        USR1           exit  
  16.        USR2           exit  
  17.        VTALRM         exit  
  18.        STKFLT         exit      might not be implemented  
  19.        PWR            ignore    might exit on some systems  
  20.        ……       
  21.        ……  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值