<?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>Think Lamp &#187; flock</title>
	<atom:link href="http://www.think-lamp.com/tag/flock/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.think-lamp.com</link>
	<description>Everything Linux Apache Php Mysql</description>
	<lastBuildDate>Sun, 05 Sep 2010 00:50:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>HTTP sessions and background processes on Apache-PHP</title>
		<link>http://www.think-lamp.com/2009/04/http-sessions-and-background-processes-on-apache-php/</link>
		<comments>http://www.think-lamp.com/2009/04/http-sessions-and-background-processes-on-apache-php/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 21:08:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[background process]]></category>
		<category><![CDATA[file descriptor]]></category>
		<category><![CDATA[flock]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[session_start()]]></category>
		<category><![CDATA[session_write_close]]></category>

		<guid isPermaLink="false">http://www.think-lamp.com/?p=167</guid>
		<description><![CDATA[OK , so you are all excited about running a 10 minute process and you are happy that you have set the processing as a background process, while the user can surf along your website / CMS. If you are are using sessions, not quite smarty ! Consider this script [parentProcess.php] $backgroundCall = 'php childProcess.php [...]]]></description>
			<content:encoded><![CDATA[<p>OK , so you are all excited about running a 10 minute process and you are happy that<br />
you have set the processing as a background process, while the user can surf along your<br />
website / CMS. If you are are using sessions, not quite smarty !</p>
<p><span id="more-167"></span></p>
<p>Consider this script [parentProcess.php]</p>
<pre class="brush: php;">
$backgroundCall = 'php childProcess.php &amp;gt; /dev/null &amp;amp;';

shell_exec($backgroundCall );

echo &quot;Hello world ! My background job is fired&quot;;

// other code , redirects etc
</pre>
<p>When you call a background file with the &#8216;ampersand&#8217; in the end, it sure becomes a background file.<br />
But it does not mean that the user will get control back of the browser. If you are using sessions,<br />
the webpage will be busy until the background process finishes execution.</p>
<p>Why is that ?</p>
<p><span style="color: #800000;"><em>[There is bug described <a href="http://bugs.php.net/bug.php?id=10675" target="_blank">here</a> and it is still not solved even in the php 5.0+ versions.]</em></span></p>
<p style="text-align: justify;">What happens is that the child process which is called by the <strong>system() ,exec() or shell_exec()</strong> command<br />
holds the file lock on the parent process, thus locking the session file.<br />
Thus, the session file being locked <strong>session_start();</strong> function cannot access the file.<br />
This is due to to open file descriptors locking the session file.Your entire session will be locked, so none of the pages of the website can be opened ( which are under session control )<br />
unless you change the browser or delete cookies.</p>
<h2><span style="color: #ff0000;">Solution:</span></h2>
<p>There are places on the internet which provide C code and perl code to do that. But PHP has its own<br />
function , which sadly is ignored <strong>session_write_close();</strong> This function can be used to trigger the close of session. Thus instructing the session<br />
that there is nothing to write until the next session start. Triggering this before firing background process will not hold locks on<br />
the session file.</p>
<pre class="brush: php;">
$backgroundCall = 'php childProcess.php &amp;gt; /dev/null &amp;amp;';

session_write_close();

shell_exec($backgroundCall );

echo &quot;Hello world ! My background job is fired&quot;;

// other code , redirects etc</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.think-lamp.com%2F2009%2F04%2Fhttp-sessions-and-background-processes-on-apache-php%2F&amp;linkname=HTTP%20sessions%20and%20background%20processes%20on%20Apache-PHP"><img src="http://www.think-lamp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.think-lamp.com/2009/04/http-sessions-and-background-processes-on-apache-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
