<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>OurApache &#187; worker</title>
	<atom:link href="http://ourapache.com/archives/tag/worker/feed" rel="self" type="application/rss+xml" />
	<link>http://ourapache.com</link>
	<description>我们致力于一个Apache知识的分享网站</description>
	<lastBuildDate>Tue, 13 Apr 2010 05:18:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Apache两种常用工作模式比较:prefork和worker</title>
		<link>http://ourapache.com/archives/288</link>
		<comments>http://ourapache.com/archives/288#comments</comments>
		<pubDate>Sun, 16 Aug 2009 14:21:06 +0000</pubDate>
		<dc:creator>OurApache</dc:creator>
				<category><![CDATA[Apache基础知识]]></category>
		<category><![CDATA[prefork]]></category>
		<category><![CDATA[worker]]></category>

		<guid isPermaLink="false">http://www.ourapache.com/?p=288</guid>
		<description><![CDATA[在这里我就prefork和worker两种最为常用的工作模式进行比较：

prefork–Unix平台上的默认（缺省）MPM，使用多个子进程，每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接，效率高，但内存占用量比较大。

worker–使用多个子进程，每个子进程有多个线程，每个线程在某个确定的时间只能维持一个连接，内存占用量比较小，适合高流量的http服务器。缺点是假如一个线程崩溃，整个进程就会连同其任何线程一起”死掉”，所以要保证一个程式在运行时必须被系统识别为”每 个线程都是安全的”。]]></description>
			<content:encoded><![CDATA[<p>以前一直在用Apache，却也一直不知道它有那么多的工作模式，看了手册才知道，囧！</p>
<p>在这里我就prefork和worker两种最为常用的工作模式进行比较：</p>
<p><a href="http://ourapache.com/archives/tag/prefork" class="st_tag internal_tag" rel="tag" title="标签 prefork 下的日志">prefork</a>–Unix平台上的默认（缺省）MPM，使用多个子进程，每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接，效率高，但内存占用量比较大。</p>
<p><a href="http://ourapache.com/archives/tag/worker" class="st_tag internal_tag" rel="tag" title="标签 worker 下的日志">worker</a>–使用多个子进程，每个子进程有多个线程，每个线程在某个确定的时间只能维持一个连接，内存占用量比较小，适合高流量的http服务器。缺点是假如一个线程崩溃，整个进程就会连同其任何线程一起”死掉”，所以要保证一个程式在运行时必须被系统识别为”每 个线程都是安全的”。</p>
<p>了解了这些个之后，怎么才能知道你的Apache服务器工作在什么状态呢？使用“apachectl -l”命令就可以显示出来。</p>
<p>Apache的默认的工作模式为prefork，如何把它改成worker模式呢？</p>
<p>如果你还未编译安装，那就简单了，在编译的时候加个参数 “–with-mpm=worker”，就好了；</p>
<p>如果你想更改正在运行的Apache的工作模式：</p>
<p>1.将当前的prefork模式启动文件改名</p>
<blockquote><p>mv httpd httpd.prefork</p></blockquote>
<p>2.将worker模式的启动文件改名</p>
<blockquote><p>mv httpd.worker httpd</p></blockquote>
<p>3.修改Apache配置文件</p>
<blockquote><p>vi /usr/local/apache2/conf/extra/httpd-mpm.conf</p></blockquote>
<p>找到里边的如下一段，可适当修改负载等参数：</p>
<blockquote><p>StartServers 2<br />
MaxClients 150<br />
MinSpareThreads 25<br />
MaxSpareThreads 75<br />
ThreadsPerChild 25<br />
MaxRequestsPerChild 0</p></blockquote>
<p>4.重新启动服务</p>
<blockquote><p>/usr/local/apache2/bin/apachectl restart</p></blockquote>
<p>PS：处于稳定性和安全性考虑，在真正的生产环境，请勿轻易改变其工作模式。</p>
<h3  class="related_post_title">相关文章</h3><ul class="related_post"><li>2009年03月30号 -- <a href="http://ourapache.com/archives/234" title="Apache的prefork模式和worker模式">Apache的prefork模式和worker模式</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/232" title="Apache Prefork和Worker模式的性能比较测试">Apache Prefork和Worker模式的性能比较测试</a></li><li>2009年03月9号 -- <a href="http://ourapache.com/archives/222" title="Apache 2.0中prefork.c模块和worker.c模块的比较">Apache 2.0中prefork.c模块和worker.c模块的比较</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/229" title="对Apache中并发控制参数prefork理解和调优">对Apache中并发控制参数prefork理解和调优</a></li><li>2009年01月22号 -- <a href="http://ourapache.com/archives/64" title="Apache连接数设置prefork">Apache连接数设置prefork</a></li><li>2009年01月22号 -- <a href="http://ourapache.com/archives/62" title="对apache中并发控制参数prefork理解和调优">对apache中并发控制参数prefork理解和调优</a></li></ul>
	标签：<a href="http://ourapache.com/archives/category/basic" title="Apache基础知识" rel="tag">Apache基础知识</a>, <a href="http://ourapache.com/archives/tag/prefork" title="prefork" rel="tag">prefork</a>, <a href="http://ourapache.com/archives/tag/worker" title="worker" rel="tag">worker</a><br />
]]></content:encoded>
			<wfw:commentRss>http://ourapache.com/archives/288/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache的prefork模式和worker模式</title>
		<link>http://ourapache.com/archives/234</link>
		<comments>http://ourapache.com/archives/234#comments</comments>
		<pubDate>Mon, 30 Mar 2009 13:13:59 +0000</pubDate>
		<dc:creator>OurApache</dc:creator>
				<category><![CDATA[Apache基础知识]]></category>
		<category><![CDATA[prefork]]></category>
		<category><![CDATA[worker]]></category>

		<guid isPermaLink="false">http://www.ourapache.com/?p=234</guid>
		<description><![CDATA[Apache的prefork模式和worker模式]]></description>
			<content:encoded><![CDATA[<p><strong>prefork模式</strong></p>
<p>这个多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器，它的工作方式类似于Apache 1.3。它适合于没有线程安全库，需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM，这样若一个请求出现问题就不会影响到其他请求。</p>
<p>这个MPM具有很强的自我调节能力，只需要很少的配置指令调整。最重要的是将MaxClients设置为一个足够大的数值以处理潜在的请求高峰，同时又不能太大，以致需要使用的内存超出物理内存的大小。</p>
<p><strong>worker模式</strong></p>
<p>此多路处理模块(MPM)使网络服务器支持混合的多线程多进程。由于使用线程来处理请求，所以可以处理海量请求，而系统资源的开销小于基于进程的MPM。但是，它也使用了多进程，每个进程又有多个线程，以获得基于进程的MPM的稳定性。</p>
<p>控制这个MPM的最重要的指令是，控制每个子进程允许建立的线程数的ThreadsPerChild指令，和控制允许建立的总线程数的MaxClients指令。</p>
<p> </p>
<p><strong>prefork和worker模式的切换</strong></p>
<p>1.将当前的prefork模式启动文件改名<br />
mv httpd httpd.<a href="http://ourapache.com/archives/tag/prefork" class="st_tag internal_tag" rel="tag" title="标签 prefork 下的日志">prefork</a></p>
<p>2.将worker模式的启动文件改名<br />
mv httpd.<a href="http://ourapache.com/archives/tag/worker" class="st_tag internal_tag" rel="tag" title="标签 worker 下的日志">worker</a> httpd</p>
<p>3.修改Apache配置文件<br />
vi /usr/local/apache2/conf/extra/httpd-mpm.conf</p>
<p>找到里边的如下一段，可适当修改负载等参数：</p>
<blockquote><p>&lt;IfModule mpm_prefork_module&gt;<br />
ServerLimit 256<br />
StartServers 5<br />
MinSpareServers 5<br />
MaxSpareServers 10<br />
MaxClients 256<br />
MaxRequestsPerChild 0<br />
&lt;/IfModule&gt;</p></blockquote>
<p>4.重新启动服务<br />
/usr/local/apache2/bin/apachectl restart</p>
<p>即可换成worker方式启动apache2</p>
<p>处于稳定性和安全性考虑，不建议更换apache2的运行方式，使用系统默认prefork即可。另外很多php模块不能工作在worker模式下，例如redhat linux自带的php也不能支持线程安全。所以最好不要切换工作模式。</p>
<p><strong>prefork和worker模式的比较</strong></p>
<p>prefork模式使用多个子进程，每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接。在大多数平台上，Prefork MPM在效率上要比Worker MPM要高，但是内存使用大得多。prefork的无线程设计在某些情况下将比worker更有优势：它可以使用那些没有处理好线程安全的第三方模块，并且对于那些线程调试困难的平台而言，它也更容易调试一些。</p>
<p>worker模式使用多个子进程，每个子进程有多个线程。每个线程在某个确定的时间只能维持一个连接。通常来说，在一个高流量的HTTP服务器上，Worker MPM是个比较好的选择，因为Worker MPM的内存使用比Prefork MPM要低得多。但worker MPM也由不完善的地方，如果一个线程崩溃，整个进程就会连同其所有线程一起”死掉”.由于线程共享内存空间，所以一个程序在运行时必须被系统识别为”每个线程都是安全的”。</p>
<p>总的来说，prefork方式速度要稍高于worker，然而它需要的cpu和memory资源也稍多于woker。</p>
<p><strong>prefork模式配置详解</strong></p>
<blockquote><p>&lt;IfModule mpm_prefork_module&gt;<br />
ServerLimit 256<br />
StartServers 5<br />
MinSpareServers 5<br />
MaxSpareServers 10<br />
MaxClients 256<br />
MaxRequestsPerChild 0<br />
&lt;/IfModule&gt;</p></blockquote>
<p>ServerLimit<br />
默认的MaxClient最大是256个线程,如果想设置更大的值，就的加上ServerLimit这个参数。20000是ServerLimit这个参数的最大值。如果需要更大，则必须编译apache,此前都是不需要重新编译Apache。<br />
生效前提：必须放在其他指令的前面</p>
<p>StartServers<br />
指定服务器启动时建立的子进程数量，prefork默认为5。</p>
<p>MinSpareServers<br />
指定空闲子进程的最小数量，默认为5。如果当前空闲子进程数少于MinSpareServers ，那么Apache将以最大每秒一个的速度产生新的子进程。此参数不要设的太大。</p>
<p>MaxSpareServers<br />
设置空闲子进程的最大数量，默认为10。如果当前有超过MaxSpareServers数量的空闲子进程，那么父进程将杀死多余的子进程。此参数不要设的太大。如果你将该指令的值设置为比MinSpareServers小，Apache将会自动将其修改成”MinSpareServers+1″。</p>
<p>MaxClients<br />
限定同一时间客户端最大接入请求的数量(单个进程并发线程数)，默认为256。任何超过MaxClients限制的请求都将进入等候队列,一旦一个链接被释放，队列中的请求将得到服务。要增大这个值，你必须同时增大ServerLimit。</p>
<p>MaxRequestsPerChild<br />
每个子进程在其生存期内允许伺服的最大请求数量，默认为10000.到达MaxRequestsPerChild的限制后，子进程将会结束。如果MaxRequestsPerChild为”0″，子进程将永远不会结束。将MaxRequestsPerChild设置成非零值有两个好处：<br />
1.可以防止(偶然的)内存泄漏无限进行，从而耗尽内存。<br />
2.给进程一个有限寿命，从而有助于当服务器负载减轻的时候减少活动进程的数量。</p>
<p><strong>worker模式配置详解</strong></p>
<blockquote><p>&lt;IfModule mpm_worker_module&gt;<br />
StartServers 2<br />
MaxClients 150<br />
MinSpareThreads 25<br />
MaxSpareThreads 75<br />
ThreadsPerChild 25<br />
MaxRequestsPerChild 0<br />
&lt;/IfModule&gt;</p></blockquote>
<p>StartServers<br />
服务器启动时建立的子进程数，默认值是”3″。</p>
<p>MaxClients<br />
允许同时伺服的最大接入请求数量(最大线程数量)。任何超过MaxClients限制的请求都将进入等候队列。默认值是”400″,16(ServerLimit)乘以25(ThreadsPerChild)的结果。因此要增加MaxClients的时候，你必须同时增加ServerLimit的值。</p>
<p>MinSpareThreads<br />
最小空闲线程数,默认值是”75″。这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太少，子进程将产生新的空闲线程。</p>
<p>MaxSpareThreads<br />
设置最大空闲线程数。默认值是”250″。这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太多，子进程将杀死多余的空闲线程。MaxSpareThreads的取值范围是有限制的。Apache将按照如下限制自动修正你设置的值：worker要求其大于等于MinSpareThreads加上ThreadsPerChild的和。</p>
<p>ThreadsPerChild<br />
每个子进程建立的常驻的执行线程数。默认值是25。子进程在启动时建立这些线程后就不再建立新的线程了。</p>
<p>MaxRequestsPerChild<br />
设置每个子进程在其生存期内允许伺服的最大请求数量。到达MaxRequestsPerChild的限制后，子进程将会结束。如果MaxRequestsPerChild为”0″，子进程将永远不会结束。将MaxRequestsPerChild设置成非零值有两个好处：<br />
1.可以防止(偶然的)内存泄漏无限进行，从而耗尽内存。<br />
2.给进程一个有限寿命，从而有助于当服务器负载减轻的时候减少活动进程的数量。<br />
注意对于KeepAlive链接，只有第一个请求会被计数。事实上，它改变了每个子进程限制最大链接数量的行为。</p>
<h3  class="related_post_title">相关文章</h3><ul class="related_post"><li>2009年08月16号 -- <a href="http://ourapache.com/archives/288" title="Apache两种常用工作模式比较:prefork和worker">Apache两种常用工作模式比较:prefork和worker</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/232" title="Apache Prefork和Worker模式的性能比较测试">Apache Prefork和Worker模式的性能比较测试</a></li><li>2009年03月9号 -- <a href="http://ourapache.com/archives/222" title="Apache 2.0中prefork.c模块和worker.c模块的比较">Apache 2.0中prefork.c模块和worker.c模块的比较</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/229" title="对Apache中并发控制参数prefork理解和调优">对Apache中并发控制参数prefork理解和调优</a></li><li>2009年01月22号 -- <a href="http://ourapache.com/archives/64" title="Apache连接数设置prefork">Apache连接数设置prefork</a></li><li>2009年01月22号 -- <a href="http://ourapache.com/archives/62" title="对apache中并发控制参数prefork理解和调优">对apache中并发控制参数prefork理解和调优</a></li></ul>
	标签：<a href="http://ourapache.com/archives/category/basic" title="Apache基础知识" rel="tag">Apache基础知识</a>, <a href="http://ourapache.com/archives/tag/prefork" title="prefork" rel="tag">prefork</a>, <a href="http://ourapache.com/archives/tag/worker" title="worker" rel="tag">worker</a><br />
]]></content:encoded>
			<wfw:commentRss>http://ourapache.com/archives/234/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache Prefork和Worker模式的性能比较测试</title>
		<link>http://ourapache.com/archives/232</link>
		<comments>http://ourapache.com/archives/232#comments</comments>
		<pubDate>Mon, 30 Mar 2009 13:07:44 +0000</pubDate>
		<dc:creator>OurApache</dc:creator>
				<category><![CDATA[Apache高级应用]]></category>
		<category><![CDATA[prefork]]></category>
		<category><![CDATA[worker]]></category>

		<guid isPermaLink="false">http://www.ourapache.com/?p=232</guid>
		<description><![CDATA[相对来说，prefork方式速度要稍高于worker，然而它需要的cpu和memory资源也稍多于woker。]]></description>
			<content:encoded><![CDATA[<div>选择prefork还是worker可以在编译时使用–with-mpm=MPM参数指定,默认为prefork</div>
<div><strong></strong> </div>
<div><strong><a href="http://ourapache.com/archives/tag/prefork" class="st_tag internal_tag" rel="tag" title="标签 prefork 下的日志">prefork</a></strong></div>
<div> </div>
<div>prefork采用预派生子进程方式，用单独的子进程来处理 不同的请求，进程之间彼此独立。在make编译和make install安装后，使用httpd -l来确定当前使用的MPM是prefork.c。查看httpd-mpm.conf配置文件，里面包含如下默认的配置段：</div>
<div> </div>
<blockquote>
<div>&lt;IfModule prefork.c&gt;<br />
StartServers 5<br />
MinSpareServers 5<br />
MaxSpareServers 10<br />
MaxClients 150<br />
MaxRequestsPerChild 0<br />
&lt;/IfModule&gt;</div>
</blockquote>
<div>
prefork 控制进程在最初建立“StartServers”个子进程后，为了满足MinSpareServers设置的需要创建一个进程，等待一秒钟，继续创建两 个，再等待一秒钟，继续创建四个……如此按指数级增加创建的进程数，最多达到每秒32个，直到满足MinSpareServers设置的值为止。这种模式 可以不必在请求到来时再产生新的进程，从而减小了系统开销以增加性能。</div>
<div> </div>
<div>MaxSpareServers设置了最大的空闲进程数，如果空闲进程数大于这个 值，Apache会自动kill掉一些多余进程。这个值不要设得过大，但如果设的值比MinSpareServers小，Apache会自动把其调整为 MinSpareServers+1。如果站点负载较大，可考虑同时加大MinSpareServers和MaxSpareServers。</div>
<div> </div>
<div>MaxRequestsPerChild设置的是每个子进程可处理的请求数。每个子进程在处理了“MaxRequestsPerChild”个请求后将自 动销毁。0意味着无限，即子进程永不销毁。虽然缺省设为0可以使每个子进程处理更多的请求，但如果设成非零值也有两点重要的好处：1、可防止意外的内存泄 漏。2、在服务器负载下降的时侯会自动减少子进程数。因此，可根据服务器的负载来调整这个值。</div>
<div> </div>
<div>MaxClients是这些指令中最为重要的一个，设定的是 Apache可以同时处理的请求，是对Apache性能影响最大的参数。其缺省值150是远远不够的，如果请求总数已达到这个值（可通过ps -ef|grep http|wc -l来确认），那么后面的请求就要排队，直到某个已处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。虽然理论上这个值越大，可以 处理的请求就越多，但Apache默认的限制不能大于256。ServerLimit指令无须重编译Apache就可以加大MaxClients。</div>
<blockquote>
<div>&lt;IfModule prefork.c&gt;</div>
<div>ServerLimit  10000<br />
StartServers 5<br />
MinSpareServers 5<br />
MaxSpareServers 10<br />
MaxClients 10000<br />
MaxRequestsPerChild 0<br />
&lt;/IfModule&gt;</div>
</blockquote>
<div> </div>
<div><strong><a href="http://ourapache.com/archives/tag/worker" class="st_tag internal_tag" rel="tag" title="标签 worker 下的日志">Worker</a></strong></div>
<p><strong></strong></p>
<div>
相对于prefork，worker全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理，所以可以处理相对海量的请求，而系统资源的开销要小于 基于进程的服务器。但是，worker也使用了多进程，每个进程又生成多个线程，以获得基于进程服务器的稳定性。在configure –with-mpm=worker后，进行make编译、make install安装。在缺省生成的httpd-mpm.conf中有以下默认配置段：</div>
<blockquote>
<div>&lt;IfModule worker.c&gt;<br />
StartServers 2<br />
MaxClients 150<br />
MinSpareThreads 25<br />
MaxSpareThreads 75<br />
ThreadsPerChild 25<br />
MaxRequestsPerChild 0<br />
&lt;/IfModule&gt;</div>
</blockquote>
<div>Worker 由主控制进程生成“StartServers”个子进程，每个子进程中包含固定的ThreadsPerChild线程数，各个线程独立地处理请求。同样，为了不在请求到来时再生成线程，MinSpareThreads和MaxSpareThreads设置了最少和最多的空闲线程数；而MaxClients 设置了同时连入的clients最大总数。如果现有子进程中的线程总数不能满足负载，控制进程将派生新的子进程。</div>
<div>MinSpareThreads和 MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大，可以按照实际情况相应调节。 ThreadsPerChild是worker MPM中与性能相关最密切的指令。</div>
<div>ThreadsPerChild的最大缺省值是64，如果负载较大，64也是不够的。这时要显式使用 ThreadLimit指令，它的最大缺省值是20000。</div>
<div>Worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild 值决定的，应该大于等于MaxClients。如果负载很大，现有的子进程数不能满足时，控制进程会派生新的子进程。默认最大的子进程总数是16，加大时 也需要显式声明ServerLimit（最大值是20000）。需要注意的是，如果显式声明了ServerLimit，那么它乘以 ThreadsPerChild的值必须大于等于MaxClients，而且MaxClients必须是ThreadsPerChild的整数倍，否则 Apache将会自动调节到一个相应值。</div>
<blockquote>
<div>&lt;IfModule worker.c&gt;<br />
ServerLimit 25<br />
ThreadLimit 200<br />
StartServers 3<br />
MaxClients 2000<br />
MinSpareThreads 50<br />
MaxSpareThreads 200<br />
ThreadsPerChild 100<br />
MaxRequestsPerChild 0<br />
&lt;/IfModule&gt;</div>
</blockquote>
<div>下面是利用Apache自带的测试工具ab对Server进行测试的情况(设定请求的index页面为6bytes),cpu%为cpu占用率，mem为内存使用量(M为单位)，RequestsPerSecond为每秒处理的请求数。</div>
<div>1、Prefor方式<br />
  <span style="color: #0000ff;">(ServerLimit,StartServer,MinSpareServers,MaxSpareServers,MaxClients,MaxRequestPerChild)</span>            </div>
<table border="1" cellspacing="0">
<tbody>
<tr>
<td colspan="4" width="357" height="18">
<table style="width: 255px; height: 450px;" border="1" cellspacing="1" cellpadding="1" width="255">
<tbody>
<tr>
<td><span style="color: #000000;">-n/-c(ab参数)</span></td>
<td>Cpu%</td>
<td>Mem</td>
<td>
<div>Requestspersecond</div>
</td>
</tr>
<tr>
<td colspan="4"><span style="color: #0000ff;">(－,5,5,10,150,0)</span></td>
</tr>
<tr>
<td>100000/100</td>
<td>28.8</td>
<td>285</td>
<td>8434</td>
</tr>
<tr>
<td>100000/200</td>
<td>29.2</td>
<td>304</td>
<td>8032</td>
</tr>
<tr>
<td>100000/500</td>
<td>25.3</td>
<td>323</td>
<td>7348</td>
</tr>
<tr>
<td>100000/1000</td>
<td>24.4</td>
<td>330</td>
<td>5886</td>
</tr>
<tr>
<td colspan="4"><span style="color: #0000ff;">(10000,5,5,10,500,0)</span></td>
</tr>
<tr>
<td>100000/100</td>
<td>28.7</td>
<td>371</td>
<td>8345</td>
</tr>
<tr>
<td>100000/200</td>
<td>27.4</td>
<td>389</td>
<td>7929</td>
</tr>
<tr>
<td>100000/500</td>
<td>24.9</td>
<td>417</td>
<td>7229</td>
</tr>
<tr>
<td>100000/1000</td>
<td>23.4</td>
<td>437</td>
<td>6676</td>
</tr>
<tr>
<td colspan="4"><span style="color: #0000ff;">(10000,5,5,10,1000,0)</span></td>
</tr>
<tr>
<td>100000/100</td>
<td>28.8</td>
<td>408</td>
<td>8517</td>
</tr>
<tr>
<td>100000/200</td>
<td>27.0</td>
<td>422</td>
<td>8045</td>
</tr>
<tr>
<td>100000/500</td>
<td>24.2</td>
<td>455</td>
<td>7236</td>
</tr>
<tr>
<td>100000/1000</td>
<td>22.5</td>
<td>470</td>
<td>6570</td>
</tr>
<tr>
<td colspan="4"><span style="color: #0000ff;">(10000,5,5,10,1500,0)</span></td>
</tr>
<tr>
<td>100000/100</td>
<td>29.6</td>
<td>330</td>
<td>8407</td>
</tr>
<tr>
<td>100000/200</td>
<td>28.1</td>
<td>349</td>
<td>8014</td>
</tr>
<tr>
<td>100000/500</td>
<td>26.4</td>
<td>380</td>
<td>7290</td>
</tr>
<tr>
<td>100000/1000</td>
<td>24.0</td>
<td>400</td>
<td>6686</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<div>2、Worker方式<br />
(<span style="color: #0000ff;">ServerLimt,Threadlimt,Startservers,MaxClients,MinspareThread,MaxspareThread,ThreadperChild,MaxRequestPerChild)</span><br />
                   </div>
<table style="width: 281px; height: 422px;" border="1" cellspacing="1" cellpadding="1" width="281">
<tbody>
<tr>
<td>-n/-c(ab参数)</td>
<td>cpu%</td>
<td>mem</td>
<td>RequestsperSecond</td>
</tr>
<tr>
<td colspan="4"><span style="color: #0000ff;">(50,500,5,10000,50,200,200,0)</span></td>
</tr>
<tr>
<td>100000/100 </td>
<td>18.6</td>
<td>188</td>
<td>6020</td>
</tr>
<tr>
<td>100000/200</td>
<td>20.1</td>
<td>195</td>
<td>5892</td>
</tr>
<tr>
<td>100000/500</td>
<td>19.8</td>
<td>209</td>
<td>5708</td>
</tr>
<tr>
<td>100000/1000</td>
<td>22.2</td>
<td>218</td>
<td>6081</td>
</tr>
<tr>
<td colspan="4"><span style="color: #0000ff;">(100,500,5,10000,50,200,100,0)</span></td>
</tr>
<tr>
<td>100000/100 </td>
<td>24.5</td>
<td>240</td>
<td>6919</td>
</tr>
<tr>
<td>100000/200</td>
<td>23.6</td>
<td>247</td>
<td>6798</td>
</tr>
<tr>
<td>100000/500</td>
<td>24.6</td>
<td>254</td>
<td>6827</td>
</tr>
<tr>
<td>100000/1000</td>
<td>22.3</td>
<td>271</td>
<td>6114</td>
</tr>
<tr>
<td colspan="4"><span style="color: #0000ff;">(200,500,5,10000,50,200,50,0)</span></td>
</tr>
<tr>
<td>100000/100 </td>
<td>27.3</td>
<td>301</td>
<td>7781</td>
</tr>
<tr>
<td>100000/200</td>
<td>27.4</td>
<td>307</td>
<td>7789</td>
</tr>
<tr>
<td>100000/500</td>
<td>26.0</td>
<td>320</td>
<td>7141</td>
</tr>
<tr>
<td>100000/1000</td>
<td>21.8</td>
<td>344</td>
<td>6110</td>
</tr>
</tbody>
</table>
<div>相对来说，prefork方式速度要稍高于worker，然而它需要的cpu和memory资源也稍多于woker。</div>
<h3  class="related_post_title">相关文章</h3><ul class="related_post"><li>2009年08月16号 -- <a href="http://ourapache.com/archives/288" title="Apache两种常用工作模式比较:prefork和worker">Apache两种常用工作模式比较:prefork和worker</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/234" title="Apache的prefork模式和worker模式">Apache的prefork模式和worker模式</a></li><li>2009年03月9号 -- <a href="http://ourapache.com/archives/222" title="Apache 2.0中prefork.c模块和worker.c模块的比较">Apache 2.0中prefork.c模块和worker.c模块的比较</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/229" title="对Apache中并发控制参数prefork理解和调优">对Apache中并发控制参数prefork理解和调优</a></li><li>2009年01月22号 -- <a href="http://ourapache.com/archives/64" title="Apache连接数设置prefork">Apache连接数设置prefork</a></li><li>2009年01月22号 -- <a href="http://ourapache.com/archives/62" title="对apache中并发控制参数prefork理解和调优">对apache中并发控制参数prefork理解和调优</a></li></ul>
	标签：<a href="http://ourapache.com/archives/category/advanced" title="Apache高级应用" rel="tag">Apache高级应用</a>, <a href="http://ourapache.com/archives/tag/prefork" title="prefork" rel="tag">prefork</a>, <a href="http://ourapache.com/archives/tag/worker" title="worker" rel="tag">worker</a><br />
]]></content:encoded>
			<wfw:commentRss>http://ourapache.com/archives/232/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apache 2.0中prefork.c模块和worker.c模块的比较</title>
		<link>http://ourapache.com/archives/222</link>
		<comments>http://ourapache.com/archives/222#comments</comments>
		<pubDate>Mon, 09 Mar 2009 06:16:58 +0000</pubDate>
		<dc:creator>OurApache</dc:creator>
				<category><![CDATA[Apache高级应用]]></category>
		<category><![CDATA[prefork]]></category>
		<category><![CDATA[worker]]></category>
		<category><![CDATA[模块]]></category>

		<guid isPermaLink="false">http://www.ourapache.com/?p=222</guid>
		<description><![CDATA[Apache 2.0中prefork.c模块和worker.c模块的比较]]></description>
			<content:encoded><![CDATA[<p>空闲子进程：是指没有正在处理请求的子进程。</p>
<p><span style="color: #0000ff;"><strong><a href="http://ourapache.com/archives/tag/prefork" class="st_tag internal_tag" rel="tag" title="标签 prefork 下的日志">prefork</a>.c模块(一个非线程型的、预派生的MPM)</strong></span><br />
prefork MPM 使用多个子进程，每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接。在大多数平台上，Prefork MPM在效率上要比Worker MPM要高，但是内存使用大得多。prefork的无线程设计在某些情况下将比worker更有优势：它可以使用那些没有处理好线程安全的第三方模块，并且对于那些线程调试困难的平台而言，它也更容易调试一些。</p>
<p>ServerLimit 20000<br />
StartServers 5<br />
MinSpareServers 5<br />
MaxSpareServers 10<br />
MaxClients 1000<br />
MaxRequestsPerChild 0</p>
<p><span style="color: #ff0000;">perfork模块最大并发数为 MaxClients = 1000</span></p>
<p>ServerLimit 2000<br />
//默认的MaxClient最大是256个线程,如果想设置更大的值，就的加上ServerLimit这个参数。20000是ServerLimit这个参数的最大值。如果需要更大，则必须编译apache,此前都是不需要重新编译Apache。<br />
生效前提：必须放在其他指令的前面</p>
<p>StartServers 5<br />
//指定服务器启动时建立的子进程数量，prefork默认为5。</p>
<p>MinSpareServers 5<br />
//指定空闲子进程的最小数量，默认为5。如果当前空闲子进程数少于MinSpareServers ，那么Apache将以最大每秒一个的速度产生新的子进程。此参数不要设的太大。</p>
<p>MaxSpareServers 10<br />
//设置空闲子进程的最大数量，默认为10。如果当前有超过MaxSpareServers数量的空闲子进程，那么父进程将杀死多余的子进程。此参数不要设的太大。如果你将该指令的值设置为比MinSpareServers小，Apache将会自动将其修改成”MinSpareServers+1″。</p>
<p>MaxClients 256<br />
//限定同一时间客户端最大接入请求的数量(单个进程并发线程数)，默认为256。任何超过MaxClients限制的请求都将进入等候队列,一旦一个链接被释放，队列中的请求将得到服务。要增大这个值，你必须同时增大ServerLimit （与MaxClient相同）。</p>
<p>MaxRequestsPerChild 10000<br />
//每个子进程在其生存期内允许伺服的最大请求数量，默认为10000.到达MaxRequestsPerChild的限制后，子进程将会结束。如果MaxRequestsPerChild为”0″，子进程将永远不会结束。</p>
<p><span style="color: #0000ff;">将MaxRequestsPerChild设置成非零值有两个好处：</span><br />
1.可以防止(偶然的)内存泄漏无限进行，从而耗尽内存。<br />
2.给进程一个有限寿命，从而有助于当服务器负载减轻的时候减少活动进程的数量。</p>
<p><span style="color: #0000ff;">工作方式：</span><br />
一个单独的控制进程(父进程)负责产生子进程，这些子进程用于监听请求并作出应答。Apache总是试图保持一些备用的(spare)或者是空闲的子进程用于迎接即将到来的请求。这样客户端就不需要在得到服务前等候子进程的产生。在Unix系统中，父进程通常以root身份运行以便邦定80端口，而 Apache产生的子进程通常以一个低特权的用户运行。User和Group指令用于设置子进程的低特权用户。运行子进程的用户必须要对它所服务的内容有读取的权限，但是对服务内容之外的其他资源必须拥有尽可能少的权限。</p>
<p><span style="color: #0000ff;"><strong><a href="http://ourapache.com/archives/tag/worker" class="st_tag internal_tag" rel="tag" title="标签 worker 下的日志">worker</a>.c模块(支持混合的多线程多进程的多路处理模块)</strong></span><br />
worker MPM 使用多个子进程，每个子进程有多个线程。每个线程在某个确定的时间只能维持一个连接。通常来说，在一个高流量的HTTP服务器上，Worker MPM是个比较好的选择，因为Worker MPM的内存使用比Prefork MPM要低得多。但worker MPM也由不完善的地方，如果一个线程崩溃，整个进程就会连同其所有线程一起”死掉”.由于线程共享内存空间，所以一个程序在运行时必须被系统识别为”每个线程都是安全的”。</p>
<p>ServerLimit 50<br />
ThreadLimit 200<br />
StartServers 5<br />
MaxClients 5000<br />
MinSpareThreads 25<br />
MaxSpareThreads 500<br />
ThreadsPerChild 100<br />
MaxRequestsPerChild 0</p>
<p><span style="color: #ff0000;">公式：<br />
ThreadLimit &gt;= ThreadsPerChild<br />
MaxClients &lt;= ServerLimit * ThreadsPerChild 必须是ThreadsPerChild的倍数<br />
MaxSpareThreads &gt;= MinSpareThreads+ThreadsPerChild</span></p>
<p><span style="color: #ff0000;">worker模块最大并发数为 MaxClients = 5000</span></p>
<p>ServerLimit 16<br />
//服务器允许配置的进程数上限。这个指令和ThreadLimit结合使用设置了MaxClients最大允许配置的数值。任何在重启期间对这个指令的改变都将被忽略，但对MaxClients的修改却会生效。</p>
<p>ThreadLimit 64<br />
//每个子进程可配置的线程数上限。这个指令设置了每个子进程可配置的线程数ThreadsPerChild上限。任何在重启期间对这个指令的改变都将被忽略，但对ThreadsPerChild的修改却会生效。默认值是”64″.</p>
<p>StartServers 3<br />
//服务器启动时建立的子进程数，默认值是”3″。</p>
<p>MinSpareThreads 75<br />
//最小空闲线程数,默认值是”75″。这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太少，子进程将产生新的空闲线程。</p>
<p>MaxSpareThreads 250<br />
//设置最大空闲线程数。默认值是”250″。这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太多，子进程将杀死多余的空闲线程。MaxSpareThreads的取值范围是有限制的。Apache将按照如下限制自动修正你设置的值：worker要求其大于等于 MinSpareThreads加上ThreadsPerChild的和</p>
<p>MaxClients 400<br />
//允许同时伺服的最大接入请求数量(最大线程数量)。任何超过MaxClients限制的请求都将进入等候队列。默认值是”400″ ,16(ServerLimit)乘以25(ThreadsPerChild)的结果。因此要增加MaxClients的时候，你必须同时增加 ServerLimit的值。</p>
<p>ThreadsPerChild 25<br />
//每个子进程建立的常驻的执行线程数。默认值是25。子进程在启动时建立这些线程后就不再建立新的线程了。</p>
<p>MaxRequestsPerChild 0<br />
//设置每个子进程在其生存期内允许伺服的最大请求数量。到达MaxRequestsPerChild的限制后，子进程将会结束。如果MaxRequestsPerChild为”0″，子进程将永远不会结束。</p>
<p><span style="color: #0000ff;">将MaxRequestsPerChild设置成非零值有两个好处：</span><br />
1.可以防止(偶然的)内存泄漏无限进行，从而耗尽内存。<br />
2.给进程一个有限寿命，从而有助于当服务器负载减轻的时候减少活动进程的数量。<br />
注意<br />
对于KeepAlive链接，只有第一个请求会被计数。事实上，它改变了每个子进程限制最大链接数量的行为。</p>
<p><span style="color: #0000ff;">工作方式：</span><br />
每个进程可以拥有的线程数量是固定的。服务器会根据负载情况增加或减少进程数量。一个单独的控制进程(父进程)负责子进程的建立。每个子进程可以建立 ThreadsPerChild数量的服务线程和一个监听线程，该监听线程监听接入请求并将其传递给服务线程处理和应答。Apache总是试图维持一个备用(spare)或是空闲的服务线程池。这样，客户端无须等待新线程或新进程的建立即可得到处理。在Unix中，为了能够绑定80端口，父进程一般都是以 root身份启动，随后，Apache以较低权限的用户建立子进程和线程。User和Group指令用于设置Apache子进程的权限。虽然子进程必须对其提供的内容拥有读权限，但应该尽可能给予它较少的特权。另外，除非使用了suexec ，否则，这些指令设置的权限将被CGI脚本所继承。</p>
<p>硬限制：</p>
<p>ServerLimi和ThreadLimit这两个指令决定了活动子进程数量和每个子进程中线程数量的硬限制。要想改变这个硬限制必须完全停止服务器然后再启动服务器(直接重启是不行的)。</p>
<p>Apache在编译ServerLimit时内部有一个硬性的限制，你不能超越这个限制。<br />
prefork MPM最大为”ServerLimit 200000″<br />
其它MPM(包括work MPM)最大为”ServerLimit 20000</p>
<p>Apache在编译ThreadLimit时内部有一个硬性的限制，你不能超越这个限制。<br />
mpm_winnt是”ThreadLimit 15000″<br />
其它MPM(包括work prefork)为”ThreadLimit 20000</p>
<p>注意<br />
使用ServerLimit和ThreadLimit时要特别当心。如果将ServerLimit和ThreadLimit设置成一个高出实际需要许多的值，将会有过多的共享内存被分配。当设置成超过系统的处理能力，Apache可能无法启动，或者系统将变得不稳定。</p>
<h3  class="related_post_title">相关文章</h3><ul class="related_post"><li>2009年08月16号 -- <a href="http://ourapache.com/archives/288" title="Apache两种常用工作模式比较:prefork和worker">Apache两种常用工作模式比较:prefork和worker</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/234" title="Apache的prefork模式和worker模式">Apache的prefork模式和worker模式</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/232" title="Apache Prefork和Worker模式的性能比较测试">Apache Prefork和Worker模式的性能比较测试</a></li><li>2009年03月30号 -- <a href="http://ourapache.com/archives/229" title="对Apache中并发控制参数prefork理解和调优">对Apache中并发控制参数prefork理解和调优</a></li><li>2009年02月6号 -- <a href="http://ourapache.com/archives/76" title="Apache 模块说明">Apache 模块说明</a></li><li>2009年01月22号 -- <a href="http://ourapache.com/archives/64" title="Apache连接数设置prefork">Apache连接数设置prefork</a></li><li>2009年01月22号 -- <a href="http://ourapache.com/archives/62" title="对apache中并发控制参数prefork理解和调优">对apache中并发控制参数prefork理解和调优</a></li></ul>
	标签：<a href="http://ourapache.com/archives/category/advanced" title="Apache高级应用" rel="tag">Apache高级应用</a>, <a href="http://ourapache.com/archives/tag/prefork" title="prefork" rel="tag">prefork</a>, <a href="http://ourapache.com/archives/tag/worker" title="worker" rel="tag">worker</a>, <a href="http://ourapache.com/archives/tag/%e6%a8%a1%e5%9d%97" title="模块" rel="tag">模块</a><br />
]]></content:encoded>
			<wfw:commentRss>http://ourapache.com/archives/222/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

