mongoose的main函数源码如下
int main(int argc, char *argv[])
{
init_server_name();
start_mongoose(argc, argv);
printf("%s started on port(s) %s with web root [%s]\n",
server_name, mg_get_option(ctx, "listening_ports"),
mg_get_option(ctx, "document_root"));
while (exit_flag == 0) {
sleep(1);
}
printf("Exiting on signal %d, waiting for all threads to finish...",
exit_flag);
fflush(stdout);
mg_stop(ctx);
printf("%s", " done.\n");
return EXIT_SUCCESS;
}
先初始化服务器的名字,名字中包含了当前mongoose的版本号,该版本号是一个宏。然后启动mongoose服务,mongoose之所以能工作也是这个启动函数的功劳,启动服务后进入循环阶段,循环条件是exit_flag,这是一个静态全局变量,默认初始化为0,然后在信号处理程序中置为别的值,前面在start_mongoose中捕捉SIGTERM和SIGINT信号,SIGTERM信号一般是因为使用了kill或killall命令杀掉进程所产生,SIGINT信号一般是因为用户键入了CTRL+C所产生。当捕捉到这里两个信号时程序将结束。最后调用mg_stop()结束程序。