进程如何自杀?
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
int main(){
pid_t pid = fork();
if(pid==0){
system("watch ls");
}
else{
sleep(5);
killpg(getpid(),SIGTERM); //to kill the complete process tree.
}
return 0;
}
终端:
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ gcc test.c
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ ./a.out
Terminated
在前 5 秒内显示“watch ls”的输出,然后它终止,因为我发送了一个 SIGTERM。
问题:进程如何自杀?我已经完成了kill(getpid(),SIGTERM);
我的假设: 因此在kill()调用期间进程切换到内核模式。 Kill 调用将 SIGTERM 发送到进程并将其复制到进程的进程表中。当进程返回到用户模式时,它会在其表中看到信号并自行终止(如何?我真的不知道) (我认为我的假设中的某个地方出错了(可能是一个错误)......所以请启发我)
这段代码实际上是一个存根,我用它来测试项目的其他模块。 它为我完成了这项工作,我对此感到满意,但我心中有一个问题,一个进程实际上是如何自杀的。我想知道一步一步的假设。
预先感
谢阿尼鲁德·托梅尔
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
int main(){
pid_t pid = fork();
if(pid==0){
system("watch ls");
}
else{
sleep(5);
killpg(getpid(),SIGTERM); //to kill the complete process tree.
}
return 0;
}
Terminal:
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ gcc test.c
anirudh@anirudh-Aspire-5920:~/Desktop/testing$ ./a.out
Terminated
for the first 5 secs the output of the "watch ls" is shown and then it terminates because I send a SIGTERM.
Question: How can a process kills itself ? I have done kill(getpid(),SIGTERM);
My hypothesis:
so during the kill() call the process switches to kernel mode. The kill call sends the SIGTERM to the process and copies it in the process's process table. when the process comes back to user mode it sees the signal in its table and it terminates itself (HOW ? I REALLY DO NOT KNOW )
(I think I am going wrong (may be a blunder) somewhere in my hypothesis ... so Please enlighten me)
This code is actually a stub which I am using to test my other modules of the Project.
Its doing the job for me and I am happy with it but there lies a question in my mind how actually a process kills itself. I want to know the step by step hypothesis.
Thanks in advance
Anirudh Tomer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的进程终止是因为您使用了
killpg()
,它向进程组而不是进程发送信号。当您
fork()
时,子进程会从父进程继承,其中包括进程组。来自man fork
:所以你杀死了父进程和子进程。
如果你执行一个简单的
kill(getpid(), SIGTERM)
那么父亲将杀死孩子(即watch
ingls
),然后将会平静地退出。Your process dies because you are using
killpg()
, that sends a signal to a process group, not to a process.When you
fork()
, the children inherits from the father, among the other things, the process group. Fromman fork
:So you kill the parent along with the child.
If you do a simple
kill(getpid(), SIGTERM)
then the father will kill the child (that iswatch
ingls
) and then will peacefully exit.在Linux中,当从内核模式返回到用户空间模式时,内核会检查是否有任何信号可以传递的待处理信号。如果有的话,它会在返回用户空间模式之前传递信号。它还可以在其他时间传递信号,例如,如果进程在
select()
上被阻塞然后被终止,或者当线程访问未映射的内存位置时。In Linux, when returning from the kernel mode to the user-space mode the kernel checks if there are any pending signals that can be delivered. If there are some it delivers the signals just before returning to the user-space mode. It can also deliver signals at other times, for example, if a process was blocked on
select()
and then killed, or when a thread accesses an unmapped memory location.我认为当它在进程表中看到 SIGTERM 信号时,它首先会杀死它的子进程(因为我调用了killpg(),所以它是完整的树),然后调用 exit() 。
我仍在寻找这个问题的更好答案。
I think it when it sees the SIGTERM signal in its process tables it first kills its child processes( complete tree since I have called killpg() ) and then it calls exit().
I am still looking for a better answer to this question.
我在使用 case 0: 进行
fork
后测试了它,它从单独的父进程中正常退出。我不知道这是否是一个标准的认证方法....
(我可以从我的psensor工具中看到CPU使用率返回34%,就像正常的程序代码一样
计数器停止)。
I tested it after a
fork
with case 0: and it quit regular from separate parent process.I don't know if this is a standard certification method ....
(I can see from my psensor tool that CPU usage return in 34% like a normal program code with
a counter stopped ) .
这在 Perl 中非常简单:
转换为 C 留给读者作为练习。
This is super-easy in Perl:
Conversion into C is left as an exercise for the reader.