<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Quick guide to Linux daemons in PHP</title>
	<atom:link href="http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/</link>
	<description>stuff that pops into my head ... technical stuff.</description>
	<lastBuildDate>Mon, 30 Aug 2010 02:57:37 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: adrian</title>
		<link>http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/comment-page-1/#comment-26</link>
		<dc:creator>adrian</dc:creator>
		<pubDate>Mon, 20 Jul 2009 13:38:48 +0000</pubDate>
		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=39#comment-26</guid>
		<description>Tony,

You&#039;re saying that the parent never responds to signals during that loop? It sounds silly, but what happens if you put some other operations in that while loop, like a for-loop? I&#039;m not convinced sleep(1) is a &quot;tickable&quot; statement...

Also, try substituting your while loop, for a while(true) loop, then call waitpid without no-hang.... see what happens then.</description>
		<content:encoded><![CDATA[<p>Tony,</p>
<p>You&#8217;re saying that the parent never responds to signals during that loop? It sounds silly, but what happens if you put some other operations in that while loop, like a for-loop? I&#8217;m not convinced sleep(1) is a &#8220;tickable&#8221; statement&#8230;</p>
<p>Also, try substituting your while loop, for a while(true) loop, then call waitpid without no-hang&#8230;. see what happens then.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony</title>
		<link>http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/comment-page-1/#comment-18</link>
		<dc:creator>Tony</dc:creator>
		<pubDate>Sun, 19 Jul 2009 05:27:34 +0000</pubDate>
		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=39#comment-18</guid>
		<description>Is there a way to manually trigger ticks in 5.2? Even Jaunty isn&#039;t using 5.3 yet, and I don&#039;t want to try to compile a PHP version and later have problems when I upgrade my OS.  I have a forked daemon working just fine, but in one loop waiting for a newly forked child to finish, the daemon never responds to signals it&#039;s sent.  Here&#039;s the loop that waits for the child to finish:

while (pcntl_waitpid($pid2, $cstatus, WNOHANG) == 0) {
    sleep(1);
}

I&#039;ve confirmed the loop runs fine by adding an echo in the function, yet during this process the daemon never responds to any signals sent to it.

To clarify: 
Application - forks daemon
Daemon - forks child process, waits for it to return.  Child process assumes a different uid/gid which is why the fork.

Daemon responds to all signals up until the child fork, at which point it won&#039;t respond anymore.</description>
		<content:encoded><![CDATA[<p>Is there a way to manually trigger ticks in 5.2? Even Jaunty isn&#8217;t using 5.3 yet, and I don&#8217;t want to try to compile a PHP version and later have problems when I upgrade my OS.  I have a forked daemon working just fine, but in one loop waiting for a newly forked child to finish, the daemon never responds to signals it&#8217;s sent.  Here&#8217;s the loop that waits for the child to finish:</p>
<p>while (pcntl_waitpid($pid2, $cstatus, WNOHANG) == 0) {<br />
    sleep(1);<br />
}</p>
<p>I&#8217;ve confirmed the loop runs fine by adding an echo in the function, yet during this process the daemon never responds to any signals sent to it.</p>
<p>To clarify:<br />
Application &#8211; forks daemon<br />
Daemon &#8211; forks child process, waits for it to return.  Child process assumes a different uid/gid which is why the fork.</p>
<p>Daemon responds to all signals up until the child fork, at which point it won&#8217;t respond anymore.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/comment-page-1/#comment-11</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Wed, 08 Jul 2009 08:13:46 +0000</pubDate>
		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=39#comment-11</guid>
		<description>Very interesting presentation! Thanks.</description>
		<content:encoded><![CDATA[<p>Very interesting presentation! Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://dev.fuzzee.co.uk/2009/07/quick-guide-to-linux-daemons-in-php/comment-page-1/#comment-10</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Tue, 07 Jul 2009 13:56:57 +0000</pubDate>
		<guid isPermaLink="false">http://dev.fuzzee.co.uk/?p=39#comment-10</guid>
		<description>Couple of additional notes:

With PHP5.3+ declare(ticks=1) is deprecated, that means that the signal handlers are NOT registered to be called. You have to manually trigger them by adding to your primary loop the following:

Before:
while ( $var === true ) { // do something cool }

After:
while ( $var === true &amp;&amp; pcntl_signal_dispatch() ) { // do something cool }

pcntl_signal_dispatch() will then fire all signal handlers. See http://bugs.php.net/bug.php?id=47198 and http://www.php.net/manual/en/function.pcntl-signal-dispatch.php for further details.

Inside your main process loop it is extremely important to check for signals before AND after your main process. That will allow you to cleanly shutdown, restart etc your daemon process.

If processing very large XML files i.e. &gt;100MB use xmlreader() and run that process separately NOT as a daemon; use a daemon though to control when the files are processed.

Avoid at all costs reciprocal references in long running scripts e.g. passing $this into sub-objects so you have references between objects. You will run out of memory as the objects will never be destroyed (this is possibly fixed in PHP 5.3). Similarly avoid using simplexml foreach loops - they leak memory too. Use for loops instead (which don&#039;t).

As has been mentioned in the article already, and really needs re-iterating: NEVER open a resource before forking the process. Do only basic initialisation and then only when your process has forked should you create db connections, file pointers etc. This will avoid any nasty hard to track bugs with already opened resources dropping out of scope or being prematurely killed.</description>
		<content:encoded><![CDATA[<p>Couple of additional notes:</p>
<p>With PHP5.3+ declare(ticks=1) is deprecated, that means that the signal handlers are NOT registered to be called. You have to manually trigger them by adding to your primary loop the following:</p>
<p>Before:<br />
while ( $var === true ) { // do something cool }</p>
<p>After:<br />
while ( $var === true &amp;&amp; pcntl_signal_dispatch() ) { // do something cool }</p>
<p>pcntl_signal_dispatch() will then fire all signal handlers. See <a href="http://bugs.php.net/bug.php?id=47198" rel="nofollow">http://bugs.php.net/bug.php?id=47198</a> and <a href="http://www.php.net/manual/en/function.pcntl-signal-dispatch.php" rel="nofollow">http://www.php.net/manual/en/function.pcntl-signal-dispatch.php</a> for further details.</p>
<p>Inside your main process loop it is extremely important to check for signals before AND after your main process. That will allow you to cleanly shutdown, restart etc your daemon process.</p>
<p>If processing very large XML files i.e. &gt;100MB use xmlreader() and run that process separately NOT as a daemon; use a daemon though to control when the files are processed.</p>
<p>Avoid at all costs reciprocal references in long running scripts e.g. passing $this into sub-objects so you have references between objects. You will run out of memory as the objects will never be destroyed (this is possibly fixed in PHP 5.3). Similarly avoid using simplexml foreach loops &#8211; they leak memory too. Use for loops instead (which don&#8217;t).</p>
<p>As has been mentioned in the article already, and really needs re-iterating: NEVER open a resource before forking the process. Do only basic initialisation and then only when your process has forked should you create db connections, file pointers etc. This will avoid any nasty hard to track bugs with already opened resources dropping out of scope or being prematurely killed.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
