0

我在Linux下遇到了一个信号处理问题,我的目标是让进程忽略SIGTERM信号。但有时,进程仍然退出,这个问题的概率是 1/60。

我的应用程序的假代码:

static int g_count_sig_old = 0;

static volatile int g_count_sig = 0;

void _signalhandler(){

   g_count_sig ++;

   printf(...); // Maybe not safe, just for debugging

   myTrace(...); // Write log to file1 is not signal safe, but just for debugging.

}

main(){

  sigaction(...) // Register signal handler for SIGTERM

  while(1){

    sleep(1000);  // wait one second

    myNewTrace(...); // Output value of g_count_sig to file2

    if( g_count_sig != g_count_sig_old ) {

       g_count_sig_old = g_count_sig;

       printf(...); // output value of g_count_sig

       myNewTrace(...); // Output value of g_count_sig to file2 
    } 

  }  
}

我想这个应用程序在收到信号 SIGTERM 时不会退出,但实际测试结果与我的设计不符。有时,进程在收到信号 SIGTERM 后仍然退出。当问题发生时,我确认进程收到 SIGTERM 信号,我可以观察控制台输出和跟踪文件。

所以我就纳闷了,为什么这个应用忽略SIGTERM也会退出呢?我不确定如何定位这个问题的原因,或者它是 Linux 下的合理症状。

希望能得到您的帮助。谢谢!

4

0 回答 0