令Apache中沉睡的無用進程自動退出的方法
在Apache的Access Log中會看到很多如下的訪問日志:
127.0.0.1 - - [05/May/2011:10:54:07 +0800] "OPTIONS * HTTP/1.0" 200 -
127.0.0.1 - - [05/May/2011:10:54:08 +0800] "OPTIONS * HTTP/1.0" 200 -
127.0.0.1 - - [05/May/2011:10:54:09 +0800] "OPTIONS * HTTP/1.0" 200 -
127.0.0.1 - - [05/May/2011:10:54:10 +0800] "OPTIONS * HTTP/1.0" 200 -
在Apache Prefork模式下, 啟動的時候,Apache就會fork出一些worker進程, 來準備接受請求, 這些worker進程,在完成準備工作以后, 就會進入block模式的監(jiān)聽沉睡中, 等待請求到來而被喚醒。
另外一方面, 在Prefork模式下, 當請求很多, 目前的worker進程數不夠處理的時候, 就會額外再fork一些worker進程出來, 以滿足當前的請求。
而在這些請求高峰過后, 如果額外fork出來的進程數大于了MaxSpareServers, Apache就會告訴這些worker進程退出, 那么問題就來了。
這些進程都在沉睡中啊, 怎么告訴他們, 并且讓他們自我退出呢?
Apache會首先發(fā)送一個退出狀態(tài)字(GRACEFUL_CHAR !)給這些Work進程:
{
apr_status_t rv;
char char_of_death = '!';
apr_size_t one = 1;
rv = apr_file_write(pod->pod_out, &char_of_death, &one);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
"write pipe_of_death");
}
return rv;
}
但此時, Worker進程不會去讀這些狀態(tài)字, 因為他們還在沉睡。
這個時候Apache就會發(fā)送一個OPTIONS請求給自己, 喚醒這些沉睡的進程:
{
//...有省略
/* Create the request string. We include a User-Agent so that
* adminstrators can track down the cause of the odd-looking
* requests in their logs.
*/
srequest = apr_pstrcat(p, "OPTIONS * HTTP/1.0\r\nUser-Agent: ",
ap_get_server_banner(),
" (internal dummy connection)\r\n\r\n", NULL);
//...有省略
}
這些進程在處理完當前請求以后(OPTIONS請求), 就會發(fā)現, oh, 主進程讓我退出。
{
//...有省略
while (!die_now && !shutdown_pending) {
//...有省略
//1. listen
//2. accept
//3. process request
/* Check the pod and the generation number after processing a
* connection so that we'll go away if a graceful restart occurred
* while we were processing the connection or we are the lucky
* idle server process that gets to die.
*/
if (ap_mpm_pod_check(pod) == APR_SUCCESS) { /* selected as idle? */
die_now = 1;
}
//...有省略
}
//...有省略
}
于是, 它就做完清理工作, 然后自我消亡了~~~
相關文章
CentOS 7 安裝 MySQL 5.6遇到的各種問題小結
在一測試服務器(CentOS Linux release 7.2.1511)上安裝MySQL 5.6(5.6.19 MySQL Community Server)時遇到了很多奇葩問題,今天小編給大家總結了關于entOS 7 安裝 MySQL 5.6遇到的各種問題,需要的朋友一起看看吧2016-11-11

