如何处理/杀死僵尸或<已失效>“mongoose webserver”上的 cgi 脚本 (C++) 进程(Linux)?

发布于 2024-11-28 15:42:43 字数 2710 浏览 1 评论 0原文

我有一个在 Ubuntu 10.04 上用 C++ 编写的“mongoose webserver”上运行的 CGI 脚本(独立于 mongoose 特定 API,以便将来实现可移植性)。每当我从网络浏览器(Chrome)调用脚本时,该过程都工作正常,但是当我运行 ps -al 时,我看到

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3567  8877  0  80   0 - 23309 hrtime pts/0    00:00:00 mongoose
4 Z     0  3585  3567  7  80   0 -     0 exit   pts/0    00:00:00 test <defunct>

我在这种情况下使用 sudo Kill -9 3567杀死父进程。我的脚本中有以下代码。

...
#include <sys/wait.h>
...

//==========================================================================
// Define the function to be called when ctrl-c (SIGINT) signal is sent to process
static void signal_callback_handler(int signum)
{ 
    point_of_inspection( __FILE__, __func__, __LINE__, ENABLE_LOG); // Entered the routine

    // Cleanup and close up stuff here
    while(1)
    {
        if (signum == SIGTERM)
        {
            error_log_report("caught signal - premature exit",CAUGHT_SIGNAL_ERROR,ENABLE_LOG);
            break;
        }
    }
    clean_exit();
    // Terminate program
    exit(signum);
}
//======================= Zombies or <defunct> handler ========================
// Signal handler to process terminated children
static void mysig(int nsig) 
{ 
    int nStatus, nPid; 
    while(1)
    {
        if (nsig == SIGCHLD)
        {
            nPid = waitpid(-1, &nStatus, WNOHANG); 
            if(nPid<0)
            {
                error_log_report("waitpid (nPid<0)",CAUGHT_SIGNAL_ERROR,ENABLE_LOG);         
                break;               
            }
            if(nPid==0)
            {
                error_log_report("Caught Signal - Zombies <defunct> (nPid==0)",CAUGHT_SIGNAL_ERROR,ENABLE_LOG);
                break;  
            }
        } 
    }
    clean_exit();
    exit(nsig);
} 

然而,在主函数中

 int main()
 {
  //some initialization variables
  ...

     // Register signal and signal handler
     signal(SIGTERM, signal_callback_handler);

     // To clean up terminated children
     signal(SIGCHLD, mysig); 
  ...

     return 0;
 }

,当用户关闭网络浏览器或导航到不同页面时,它似乎没有捕获任何触发的信号,因为我没有看到任何日志。我想知道这是否是 mongoose 或我的脚本中的错误(我的脚本中没有使用任何 fork() 进程或线程。但是 mongoose 确实使用线程。而且我没有在我的脚本中使用任何 mongoose web 服务器特定的 API。) 。

我指的是这里的信号教程 http://orchard.wccnet。 org/~chasselb/linux275/ClassNotes/process/sigbasics.htmhttp://www.gnu.org/s/hello/manual /libc/Process-Completion.html

I have a CGI script running on "mongoose webserver" written in C++ (independent of mongoose specific APIs for portability in the future) on Ubuntu 10.04. Whenever I invoke the script from web browser (Chrome), the process works fine but when I run ps -al I see

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3567  8877  0  80   0 - 23309 hrtime pts/0    00:00:00 mongoose
4 Z     0  3585  3567  7  80   0 -     0 exit   pts/0    00:00:00 test <defunct>

I use sudo kill -9 3567 in this case to kill the parent process. I have the following code in my script.

...
#include <sys/wait.h>
...

//==========================================================================
// Define the function to be called when ctrl-c (SIGINT) signal is sent to process
static void signal_callback_handler(int signum)
{ 
    point_of_inspection( __FILE__, __func__, __LINE__, ENABLE_LOG); // Entered the routine

    // Cleanup and close up stuff here
    while(1)
    {
        if (signum == SIGTERM)
        {
            error_log_report("caught signal - premature exit",CAUGHT_SIGNAL_ERROR,ENABLE_LOG);
            break;
        }
    }
    clean_exit();
    // Terminate program
    exit(signum);
}
//======================= Zombies or <defunct> handler ========================
// Signal handler to process terminated children
static void mysig(int nsig) 
{ 
    int nStatus, nPid; 
    while(1)
    {
        if (nsig == SIGCHLD)
        {
            nPid = waitpid(-1, &nStatus, WNOHANG); 
            if(nPid<0)
            {
                error_log_report("waitpid (nPid<0)",CAUGHT_SIGNAL_ERROR,ENABLE_LOG);         
                break;               
            }
            if(nPid==0)
            {
                error_log_report("Caught Signal - Zombies <defunct> (nPid==0)",CAUGHT_SIGNAL_ERROR,ENABLE_LOG);
                break;  
            }
        } 
    }
    clean_exit();
    exit(nsig);
} 

In the main function

 int main()
 {
  //some initialization variables
  ...

     // Register signal and signal handler
     signal(SIGTERM, signal_callback_handler);

     // To clean up terminated children
     signal(SIGCHLD, mysig); 
  ...

     return 0;
 }

However, It seems to not catch any signal triggered when the user closes web browser or navigates to a different page as I do not see any logs. I am wondering if this is a bug in mongoose or my script (I do not use any fork() process or threads in my script. But mongoose does use threads. Also I do not use any mongoose webserver specific APIs in my script.).

I am referring the signal tutorial from here http://orchard.wccnet.org/~chasselb/linux275/ClassNotes/process/sigbasics.htm and
http://www.gnu.org/s/hello/manual/libc/Process-Completion.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-12-05 15:42:43

他们更新了 mongoose.c 文件中的代码来收获僵尸。以下是代码的一部分。

#if !defined(_WIN32) && !defined(__SYMBIAN32__)
  // Ignore SIGPIPE signal, so if browser cancels the request, it
  // won't kill the whole process.
  (void) signal(SIGPIPE, SIG_IGN);
  // Also ignoring SIGCHLD to let the OS to reap zombies properly.
  (void) signal(SIGCHLD, SIG_IGN);
#endif // !_WIN32

They updated the code in mongoose.c file to reap zombies. The following is the portion of the code.

#if !defined(_WIN32) && !defined(__SYMBIAN32__)
  // Ignore SIGPIPE signal, so if browser cancels the request, it
  // won't kill the whole process.
  (void) signal(SIGPIPE, SIG_IGN);
  // Also ignoring SIGCHLD to let the OS to reap zombies properly.
  (void) signal(SIGCHLD, SIG_IGN);
#endif // !_WIN32
满意归宿 2024-12-05 15:42:43

Unix 中的僵尸进程是被父进程终止但尚未等待的进程。它们的存在应该是暂时的,或者表示父级中存在错误,在本例中为 mongoose

zombie processes in Unix are processes which are terminated but not waited yet by the parent process. Their presence should be temporary or is denoting a bug in the parent, in the present case mongoose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文