How to handle/kill Zombie or <defunct> process for cgi script (C++) on "mongoose webserver" (Linux)?
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) sig开发者_如何学Cnal 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
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
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
.
精彩评论