apache mpm worker , 多核 cpu 下的进程/线程混合调度机制,是apache 在2.2版本之后引入的。官方介绍:
This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.
较高的处理效率是 worker 模式的最大优点。
出于稳定和跨平台的考虑,apache2 默认编译 为 prefork模式(多进程模式)。所以,要使用 worker模式,我们还需要多费些手脚。
worker 模式的编译和安装:
[fancp@RedhatFancp httpd-2.2.17]$ ./configure --prefix=/usr/local/apache2worker --enable-so --with-mpm=worker
可以看到五个进程。
fancp 15242 1 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15243 15242 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15244 15242 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15246 15242 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15248 15242 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 15329 4762 0 23:19 pts/1 00:00:00 grep httpd
观察上面进程列表,发现 进程 15242 的父进程 pid 是1,同时它也是 进程 15243,15244,15246, 15248 的父进程。
为什么是5个进程呢?5个进程之间有什么关系呢?
mpm_default.h 中设置了默认进程数(DEFAULT_START_DAEMON),最大进程数(DEFAULT_MAX_FREE_DAEMON),最小进程数(DEFAULT_MIN_FREE_DAEMON)。在没有参数配置的情况下,会应用这些值。在 2.2.17 的版本中, DEFAULT_START_DAEMON 的值是 3。所以,上面的5个进程的组成是:一个listen 进程,3个 recv进程.还有一个进程,我就不知道啦。
修改配置文件 http.conf,加入以下内容,设置主线程为1:
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_worker_module>
StartServers 1
MaxClients 15
MinSpareThreads 1
MaxSpareThreads 1
ThreadsPerChild 1
MaxRequestsPerChild 0
</IfModule>
查看进程
[fancp@RedhatFancp httpd-2.2.17]$ ps -ef |grep httpd
fancp 15242 1 0 23:19 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 16035 15242 0 23:45 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 16036 15242 0 23:45 ? 00:00:00 /usr/local/apache2worker/bin/httpd -k start
fancp 16041 4762 0 23:45 pts/1 00:00:00 grep httpd
[fancp@RedhatFancp httpd-2.2.17]$