无法理解“杀戮" Linux程序 [英] Unable to understand the "Kill" program of linux
问题描述
这是一个非常小的与"kill"相关的程序.该程序正在执行,但无法弄清楚代码.任何人都可以让我理解下面的代码.
Here is a very small "kill" related program .The program is executing but am unable to figure out the code.Can any one please make me understand the code below.
int main(int argc ,char **argv)
{
if(argc < 2)
{
printf("usage : ./kill PID");
return -1;
}
kill(atoi(argv[1]),SIGKILL);
return 0;
}
推荐答案
基本上,它只是检查是否提供了参数. if(argc < 2)
表示程序的参数少于两个.请注意,程序名称本身也是一个参数,这就是为什么它是argc < 2
而不是argc < 1
的原因.确定后,将调用内置kill
函数.此方法的第一个参数是整数,这就是为什么第二个参数(PID,表示为字符串)使用atoi
解析为整数的原因. kill
的第二个参数是信号,在这种情况下为SIGKILL
.也可以使用其他信号,例如SIGHUP
,但是由于该程序杀死"了它,所以使用了SIGKILL
.
Basically, it just checks to see if an argument has been provided. if(argc < 2)
means are there less than two arguments to the program. Note that the program name itself is an argument too, which is why it's argc < 2
and not argc < 1
. Once this has been determined the builtin kill
function is called. The first argument to this method is an integer, which is why the second argument (the PID, represented as a string) is parsed to an integer with atoi
. The second argument to kill
is the signal, in this case SIGKILL
. Other signals, such as SIGHUP
could also have been used, but since this program "kills", it uses SIGKILL
.
这篇关于无法理解“杀戮" Linux程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!