在 Linux 系统上,信号 -KILLTERM 发送允许应用程序安全关闭的信号。这些问题可能有点理论性,但我想了解它们。
当系统发送终止信号时,它发送到哪里?
是什么让进程或应用程序有时间“安全”终止?
是否有子进程或类似的东西在查找此信号的应用程序后台运行?
这些问题源于linux watchdog,在看man page的时候看到watchdog的进程是先给给定的PID发送terminate信号,然后是KILL -9信号强制执行。我希望能够利用看门狗内置的安全功能。
最佳答案
查看这段代码,
#include<stdio.h>
#include<signal.h>
#include<stdlib.h>
void cleanUp(){ // Do whatever you want here
printf("Safely terminating \n");
}
void hand(int sig){ // called when you are sent SIGTERM
/*
Here you can safely terminate..
*/
atexit(cleanUp); // call cleanUp at exit.
exit(0);
}
int main(){
signal(SIGTERM, hand); //Assign function to be called on SIGTERM
/*
Your code goes here.
I have put an infinite loop for demonstration.
*/
printf("Started execution..\n");
for(;;);
}
这显示了当信号传递到您的应用程序时如何分配函数以供调用。
传递信号SIGTERM
对此代码,执行此操作,
kill -SIGTERM <pid>
在这里,<pid>
id 您正在运行的程序的进程 ID。
关于c - 杀死 -SIGTERM 和 KILLSIG : Safely terminating applications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33502525/