当前位置: 首页 > 科技观察

kill-0pid作用详解

时间:2023-03-14 21:57:12 科技观察

在服务器运维和程序开发过程中,经常会用到kill命令或kill()方法。那么,kill是做什么用的,信号0有什么作用呢,我们一起来探讨一下。kill[root@localhost~]#kill-l1)SIGHUP2)SIGINT3)SIGQUIT4)SIGILL5)SIGTRAP6)SIGABRT7)SIGBUS8)SIGFPE9)SIGKILL10)SIGUSR111)SIGSEGV12)SIGUSR213)SIGPIPE14)SIGALRM15)SIGTERM16)SIGSTKFLT18)19)SIGCONT)SIGSTOP20)SIGTSTP21)SIGTTIN22)SIGTTOU23)SIGURG24)SIGXCPU25)SIGXFSZ26)SIGVTALRM27)SIGPROF28)SIGWINCH29)SIGIO30)SIGPWR31)SIGSYS34)SIGRTMIN35)SIGRTMIN+136)SIGRTMIN+237)SIGRTMIN+338)SIGRTMIN+439)SIGRTMIN+540)SIGRTMIN+641)SIGRTMIN+742)SIGRTMIN+843)SIGRTMIN+944)SIGRTMIN+1045)SIGRTMIN+1146)SIGRTMIN+1247)SIGRTMIN+1348)SIGRTMIN+1449)SIGRTMIN+1550)SIGRTMIN-1451)SIGRTMIN-1352)1253)SIGRTMAX-1154)SIGRTMAX-1055)SIGRTMAX-956)SIGRTMAX-857)SIGRTMAX-758)SIGRTMAX-659)SIGRTMAX-560)SIGRTMAX-461)SIGRTMAX-362)SIGRTMAX-263)SIGRTMAX-164)SIGRTMAX注:以下内容为经常用作面试题:前31个信号和后31个信号的区别?在Linux上执行kill-l,可以看到有62个可用信号(仔细看,没有32和33)。编号为1到31的信号是传统UNIX支持的信号,属于不可靠信号(非实时)。编号从34到64的信号在后面进行了扩展,称为可靠信号(实时信号)。不可靠信号和可靠信号的区别:前者不支持排队,可能会造成信号丢失,后者不支持。kill的文档说明通过man命令可以看到kill命令的说明和参数解释。这里是部分说明,如下:[root@localhost~]#man1killKILL(1)UserCommandsKILL(1)NAMEkill-terminateaprocessSYNOPSISkill[-ssignal|-p][-qsigval][-a][--]pid...kill-l[signal]DESCRIPTIONThecommandkillsendsthespecifiedsignaltothespecifiedprocessorprocessgroup.Ifnosignalisspecified,theTERMsignalissent.TheTERMsignalwillkillprocesseswhichdonotcatchthissignal.Forotherprocesses,itmaybenecessarytousetheKILL(9)signal,sincethissignalcannotbecaught.Mostmodernshellshaveabuiltinkillfunction,withausagerathersimilartothatofthecommanddescribedhere.The'-a'and'-p'options,andthepossibilitytospecifyprocessesbycommandnamearealocalextension.Ifsigis0,thennosignalissent,buterrorcheckingisstillperformed.......#[root@localhost~]#man2killKILL(2)LinuxProgrammer'sManualKILL(2)sysNAMEkill-sendsignaltoaprocessSYNOPSIS/typesinclude<.h>#includeintkill(pid_tpid,intsig);FeatureTestMacroRequirementsforglibc(seefeature_test_macros(7)):kill():_POSIX_C_SOURCE>=1||_XOPEN_SOURCE||_POSIX_SOURCEDESCRIPTIONThekill()systemcallcanbeusedtosendanysignaltoanyprocessgrouporprocess.Ifpidispositive,thensignalsigissenttotheprocesswiththeIDspecifiedbypid.Ifpidequals0,thensigissenttoeveryprocessintheprocessgroupofthecallingprocess.Ifpidequals-1,thensigissenttoeveryprocessforwhichthecallingprocesshaspermissiontosendsignals,exceptforprocess1(init),butseebelow.Ifpidislessthan-1,thensigissenttoeveryprocessintheprocessgroupwhoseIDis-pid.Ifsigis0,thennosignalissent,buterrorcheckingisstillperformed;thiscanbeusedtocheckfortheexistenceofaprocessIDorprocessgroupID.Foraprocesstohavepermissiontosendasignalitmusteitherbeprivileged(underLinux:havetheCAP_KILLcapability),ortherealoreffectiveuserIDofthesendingprocessmustequaltherealorsavedset-user-IDofthetargetprocess.InthecaseofSIGCONTitsufficeswhenthesendingandreceivingprocessesbelongtothesamesession.从描述可知,无论是man1文档还是man2文档都指出:kill命令用于向指定的pid进程发送信号,进程在接收到对应的ofSignal之后,将执行相应的操作。关于signal0的作用,man1文档中有一句话:Ifsigis0,thennosignalissent,butstillerrorchecking,意思是:ifsigis0,nosignalissent,butstill进行错误检查。man2文档中有一句话:如果sig为0,则不发送信号,但仍进行错误检查;这可用于检查进程ID或进程组ID是否存在,这意味着:如果sig为0,则不发送信号,但仍会进行错误检查;这可用于检查是否存在进程ID或进程组ID。也就是说,kill-0pid在执行的时候不会发出信号,而是会对pid对应的进程做一次错误检查。如果它返回0,则进程和服务正在运行;否则,进程已死或服务已停止。signal0的用法由于signalkill-0pid不发送信号,所以主要是用来检查相应的进程,进行错误检查。那么在开发中,我们可以通过kill返回的错误信息来判断进程是否存在,是否正常运行。shell脚本中的示例:#!/bin/bashPIDFILE=$1if[-f$PIDFILE];thenPID="$(cat$PIDFILE)"ifkill-0"$PID"&>/dev/null;thenecho"processiseexists"exit0elseecho“processisnotexists”exit5fifiGo代码示例:funcprocessExists(pidint)bool{iferr:=syscall.Kill(pid,0);err==nil{returntrue}else{returnfalse}}【编者推荐】5月全国程序员平均工资,你越线了吗?Windows必备的5款软件!一旦你尝试欲罢不能,赶紧收藏这些良心软件,让你的Windows爽起来。2021年薪资最高的5种编程语言Linux密码管理器1Password如何安装?