<?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>Horatio Caine&#039;s Blog &#187; processes</title>
	<atom:link href="http://hxcaine.com/blog/tag/processes/feed/" rel="self" type="application/rss+xml" />
	<link>http://hxcaine.com/blog</link>
	<description>A blog of life, ideas and code</description>
	<lastBuildDate>Thu, 19 Jun 2014 00:46:16 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Can&#8217;t create thread (11) (ThreadError) in Ruby</title>
		<link>http://hxcaine.com/blog/2013/08/14/cant-create-thread-11-threaderror-in-ruby/</link>
		<comments>http://hxcaine.com/blog/2013/08/14/cant-create-thread-11-threaderror-in-ruby/#comments</comments>
		<pubDate>Wed, 14 Aug 2013 17:27:13 +0000</pubDate>
		<dc:creator><![CDATA[Horatio Caine]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[eventmachine]]></category>
		<category><![CDATA[processes]]></category>
		<category><![CDATA[pthread]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://hxcaine.com/blog/?p=307</guid>
		<description><![CDATA[I have been working on some networked code in Ruby which uses EventMachine. This is part of my work as a Research Associate at the University of Birmingham. I recently had a headache with threading/processes whereby I would get this error during thread creation: [crayon-69f4a2442653c637206272/] Here is the code I was running: [crayon-69f4a24426544735131978/] I knew [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I have been working on some networked code in Ruby which uses EventMachine. This is part of my work as a Research Associate at the University of Birmingham. I recently had a headache with threading/processes whereby I would get this error during thread creation:</p><pre class="crayon-plain-tag">Can't create thread (11) (ThreadError) in Ruby</pre><p>Here is the code I was running:</p><pre class="crayon-plain-tag"># Launch a child process
fork do
    create_server
end

...

# Somewhere inside create_server
Thread.new do
    # Do some work
end</pre><p>I knew I wasn&#8217;t creating too many threads and I knew I wasn&#8217;t running out of memory. After some serious digging, and two hundred browser tabs later,  I found the reason: zombie/defunct processes.</p>
<p>The &#8217;11&#8217; in the error code is actually not from Ruby at all, but from the <pre class="crayon-plain-tag">pthread</pre> class which is used to create new threads in Linux (among other OSes). This is the numerical value for the error code <pre class="crayon-plain-tag">EAGAIN</pre>, returned by the function <pre class="crayon-plain-tag">pthread_create</pre>, which occurs when:</p>
<blockquote><p>Insufficient resources to create another thread, or a system-imposed limit on the number of threads was encountered</p></blockquote>
<p>I had used Ruby&#8217;s <pre class="crayon-plain-tag">Thread.list.size</pre> to ensure I wasn&#8217;t using too many threads, so what could it be?<span id="more-307"></span></p>
<p>Well, during runtime I happened to check how many ruby-related threads were running, and I found a huge list of processes listed as <pre class="crayon-plain-tag">&lt;defunct&gt;</pre>, with the <pre class="crayon-plain-tag">Z+</pre> (zombie) flag attached. This tipped me off. The threads I was creating were not dying until I killed the originating process itself. This seemed strange since I knew for a fact the work had been completed in both the originating process and the spawned threads. However, a &#8216;zombie&#8217; process in Unix is a process which is dead but remains in the process table so that its parent can read its exit status.</p>
<p>Since, in my code, the parent was forking a new process and then ignoring the child&#8217;s exit status, the child process would wait forever. It simply required adding a <pre class="crayon-plain-tag">Process.wait(pid)</pre> to ensure the (already-finished) process could be removed from the process table (and by extension its child threads), and allow more threads to be created.</p>
<p>Here&#8217;s how the code should have looked:</p><pre class="crayon-plain-tag"># Launch a child process
pid = fork do
    create_server
end

# Any time before the parent process completes execution
Process.wait(pid)

...

# Somewhere inside create_server
Thread.new do
    # Do some work
end</pre><p>Since I launched several child processes, I simply stored their pids until the end of the parent process code and called &#8216;wait&#8217; for them all (which only took some tens of milliseconds). I hope this helps somebody out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://hxcaine.com/blog/2013/08/14/cant-create-thread-11-threaderror-in-ruby/feed/</wfw:commentRss>
		<slash:comments>60</slash:comments>
		</item>
	</channel>
</rss>
