成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

關于kill -0 pid的作用詳解

系統 Linux
在服務器運維以及程序開發過程中,經常會用到kill命令或者kill()方法。那么,kill是做什么以及信號0的作用又是什么,一起來探尋吧。

[[402074]]

在服務器運維以及程序開發過程中,經常會用到kill命令或者kill()方法。那么,kill是做什么以及信號0的作用又是什么,一起來探尋吧。

kill可以使用的信號

  1. [root@localhost ~]# kill -l 
  2.  1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP 
  3.  6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1 
  4. 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 
  5. 16) SIGSTKFLT   17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 
  6. 21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ 
  7. 26) SIGVTALRM   27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR 
  8. 31) SIGSYS   
  9. 34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3 
  10. 38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8 
  11. 43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 
  12. 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 
  13. 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7 
  14. 58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2 
  15. 63) SIGRTMAX-1  64) SIGRTMAX 

 注意:

下面內容常作為面試題:前31個信號和后31個信號的區別?

在Linux上執行kill -l看到可使用信號共有62個(仔細看沒有32、33哦)。

  • 編號為1 ~ 31的信號為傳統UNIX支持的信號,是不可靠信號(非實時的)
  • 編號為34 ~ 64的信號是后來擴充的,稱做可靠信號(實時信號)

不可靠信號、可靠信號區別:前者不支持排隊,可能會造成信號丟失,而后者不會。

kill的文檔描述

通過man命令可以看到關于kill指令的描述以及參數解釋,這里截取部分描述,如下:

  1. [root@localhost ~]# man 1 kill 
  2.  
  3. KILL(1)                                                                      User Commands                                                                      KILL(1) 
  4.  
  5. NAME 
  6.        kill - terminate a process 
  7.  
  8. SYNOPSIS 
  9.        kill [-s signal|-p] [-q sigval] [-a] [--] pid... 
  10.        kill -l [signal] 
  11.  
  12. DESCRIPTION 
  13.        The  command  kill  sends  the specified signal to the specified process or process group.  If no signal is specified, the TERM signal is sent.  The TERM signal 
  14.        will kill processes which do not catch this signal.  For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught. 
  15.  
  16.        Most modern shells have a builtin kill functionwith a usage rather similar to that of the command described here.  The '-a' and '-p' options, and  the  possi‐ 
  17.        bility to specify processes by command name are a local extension. 
  18.  
  19.        If sig is 0, then no signal is sent, but error checking is still performed. 
  20.        ...... 
  21.         
  22.  
  23. # [root@localhost ~]# man 2 kill 
  24. KILL(2)                                                                Linux Programmer's Manual                                                                KILL(2) 
  25.  
  26. NAME 
  27.        kill - send signal to a process 
  28.  
  29. SYNOPSIS 
  30.        #include <sys/types.h> 
  31.        #include <signal.h> 
  32.  
  33.        int kill(pid_t pid, int sig); 
  34.  
  35.    Feature Test Macro Requirements for glibc (see feature_test_macros(7)): 
  36.  
  37.        kill(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE 
  38.  
  39. DESCRIPTION 
  40.        The kill() system call can be used to send any signal to any process group or process. 
  41.  
  42.        If pid is positive, then signal sig is sent to the process with the ID specified by pid. 
  43.  
  44.        If pid equals 0, then sig is sent to every process in the process group of the calling process. 
  45.  
  46.        If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init), but see below. 
  47.  
  48.        If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid. 
  49.  
  50.        If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID. 
  51.  
  52.        For  a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL capability), or the real or effective user ID of 
  53.        the sending process must equal the real or saved set-user-ID of the target process.  In the case of SIGCONT it suffices when the sending and receiving processes 
  54.        belong to the same session. 

 從描述可知,無論是man 1文檔還是man 2文檔都指出:kill命令用于向指定的pid進程發送信號,進程在接收到對應的信號之后會進行對應的操作。

關于信號0的作用

man 1 文檔中有一句 If sig is 0, then no signal is sent, but error checking is still performed, 意思是:如果sig為0,則不發送信號,但仍然進行錯誤檢查。

man 2 文檔中有一句 If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID,意思是:如果sig為0,則不發送信號,但仍然進行錯誤檢查; 這可以用來檢查是否存在進程ID或進程組ID。

也就是說,kill -0 pid 執行時不會發送信號,但是會對pid對應進程做錯誤檢查。如果返回0則進程、服務正在運行中;反之是其他值,則進程死了或者服務已停止。

信號0的用法

既然,信號kill -0 pid不發送信號,主要用于檢查對應進程做錯誤檢查。那么,在開發中我們就可以通過kill返回的錯誤信息來判斷進程是否存在、正常運行。

shell腳本中示例:

  1. #!/bin/bash 
  2.  
  3. PIDFILE=$1 
  4.  
  5. if [ -f $PIDFILE ]; then 
  6.       PID="$(cat $PIDFILE)" 
  7.      
  8.     if kill -0 "$PID" &> /dev/nullthen 
  9.       echo "process is exists" 
  10.       exit 0 
  11.     else 
  12.       echo "process is not exists" 
  13.       exit 5 
  14.     fi 
  15. fi 

 Go代碼中示例:

  1. func processExists(pid int) bool { 
  2.     if err := syscall.Kill(pid, 0); err == nil { 
  3.         return true 
  4.     } else { 
  5.         return false 
  6.     } 

 【編輯推薦】

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2010-06-04 14:49:15

Linux 查看進程

2024-10-22 16:54:14

2020-09-26 07:19:46

Java

2024-08-13 15:46:57

2024-08-26 15:13:36

2012-05-14 10:18:54

jQuery

2024-10-11 17:13:14

SQL數據庫查詢數據庫

2014-11-06 09:49:22

CloudFoundrPaaS

2011-03-18 09:27:00

Spring

2014-11-06 10:46:48

CloudFoundr

2010-09-01 09:10:30

DHCP作用域

2010-09-27 13:21:02

DHCP作用域

2014-06-05 17:02:41

FTP

2011-09-09 16:23:16

Android Web測試

2024-10-17 16:17:21

MySQL臨時表數據庫

2022-04-14 11:17:41

MySQL字符配置

2009-10-16 13:19:41

布線施工要求

2024-11-21 07:00:00

線程池Java開發

2009-12-14 15:52:52

路由器配置

2022-08-29 10:52:37

線程函數操作系統
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: www.黄色网 | 日本精品视频 | 波多野结衣一区二区三区在线观看 | 久久久精彩视频 | 成人av网站在线观看 | 亚洲精品1区2区3区 91免费看片 | 精品欧美一区二区三区精品久久 | 久久国产亚洲 | 国产一区二区久久 | 亚洲区一区二 | 一区二区三区日韩 | 91精品国产综合久久久亚洲 | 亚洲精品美女视频 | 成人精品一区亚洲午夜久久久 | 国产激情在线观看视频 | 视频在线一区二区 | 国产91久久久久 | 欧美一区二区三区在线播放 | 久久久综合色 | 91精品久久久久 | 国产二区在线播放 | 国产精品久久久久久久久久免费 | 色婷婷狠狠 | 91视频.com | 欧美国产一区二区 | 亚洲精品第一 | 国产亚洲欧美另类一区二区三区 | 国产激情91久久精品导航 | 精品自拍视频在线观看 | 91精品国产综合久久久久 | 91视频在线 | 视频在线一区二区 | 欧洲色综合 | 久久久久久一区 | a视频在线观看 | 成人精品一区 | 国产在线高清 | 亚洲一区二区中文字幕 | 免费一级大片 | 91精品国产综合久久久久久首页 | 亚洲精品9999久久久久 |