<?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>chys&#039;s random notes &#187; multithread</title>
	<atom:link href="http://en.chys.info/tag/multithread/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.chys.info</link>
	<description>Study more problems; Talk less of isms.</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:56:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A hack to strace -f</title>
		<link>http://en.chys.info/2011/03/a-hack-to-strace-f/</link>
		<comments>http://en.chys.info/2011/03/a-hack-to-strace-f/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 13:22:20 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[multithread]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=886</guid>
		<description><![CDATA[I have a multithreaded program which I would like to strace for debugging purpose. My program sometimes calls (fork and exec) an external program, which in turn calls a setuid program. Because my program is multithreaded, I cannot omit the “-f” flag (also trace child threads and processes) when using strace. And because all children, [...]<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>I have a multithreaded program which I would like to <a href="http://en.wikipedia.org/wiki/Strace">strace</a> for debugging purpose. My program sometimes calls (fork and exec) an external program, which in turn calls a <a href="http://www.aquaphoenix.com/ref/gnu_c_library/libc_438.html">setuid program</a>.</p>
<p>Because my program is multithreaded, I cannot omit the “<code>-f</code>” flag (also trace child threads and processes) when using strace. And because all children, including the setuid program, are traced, setuid fails. (Yes, I am aware that strace claims it is possible to trace setuid programs, but the trick does not work for me, probably because the setuid program is not directly executed by strace.)</p>
<p>Fortunately, the <code><a href="http://linux.die.net/man/2/clone">clone</a></code> system call has many useful flags. It works fine for me when I substitute calls to <code><a href="http://linux.die.net/man/2/fork">fork()</a></code> with:</p>
<blockquote><p><code>(pid_t) syscall (__NR_clone, CLONE_UNTRACED|SIGCHLD, NULL);<br />
</code></p></blockquote>
<p>(Yes, <code>SIGCHLD</code>, not <code>CLONE_SIGCHLD</code>. It&#8217;s not a typo.)</p>
<p>I guess there may be better solutions, without modifying the program being traced?</p>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2011/03/a-hack-to-strace-f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>man 3 sleep</title>
		<link>http://en.chys.info/2010/10/man-3-sleep/</link>
		<comments>http://en.chys.info/2010/10/man-3-sleep/#comments</comments>
		<pubDate>Sun, 17 Oct 2010 14:17:32 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[multithread]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=843</guid>
		<description><![CDATA[The man page of sleep(3) used to say: sleep() makes the calling process sleep until seconds seconds have elapsed or a signal arrives which is not ignored. [Old version; color added] Now it says: sleep() makes the calling thread sleep until seconds seconds have elapsed or a signal arrives which is not ignored. [New version] [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2010/03/errno-as-thread-identifier/' rel='bookmark' title='&lt;code&gt;&amp;errno&lt;/code&gt; as thread identifier'><code>&amp;errno</code> as thread identifier</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The man page of <code>sleep</code>(3) used to say:</p>
<blockquote><p><strong>sleep</strong>() makes the calling <font color="red">process</font> sleep until <em>seconds</em> seconds have elapsed or a signal arrives which is not ignored. [<a href="http://linux.die.net/man/3/sleep">Old version</a>; color added]</p></blockquote>
<p>Now it says:</p>
<blockquote><p><strong>sleep</strong>() makes the calling <font color="red">thread</font> sleep until <em>seconds</em> seconds have elapsed or a signal arrives which is not ignored. [<a href="http://www.kernel.org/doc/man-pages/online/pages/man3/sleep.3.html">New version</a>]</p></blockquote>
<p>Alas, they finally did the right thing &#8211; we have waited for at least 5 years. This change happened some time between versions 3.23 and 3.26.  </p>
<p>There is indeed no reason to use &#8220;process&#8221; anymore, at least since the emergence of Linux 2.6 and <a href="http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library">NPTL</a>. As far as I know, this very sentence has confused many newbie Linux programmers who are not familiar with the history of Linux multithreading, and has led some to firmly believe there is no &#8220;real&#8221; thread or threads are actually processes under Linux, a statement which was probably right for the obsolete implementation <a href="http://en.wikipedia.org/wiki/LinuxThreads">LinuxThreads</a>, but definitely wrong today.</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2010/03/errno-as-thread-identifier/' rel='bookmark' title='&lt;code&gt;&amp;errno&lt;/code&gt; as thread identifier'><code>&amp;errno</code> as thread identifier</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2010/10/man-3-sleep/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&amp;errno as thread identifier</title>
		<link>http://en.chys.info/2010/03/errno-as-thread-identifier/</link>
		<comments>http://en.chys.info/2010/03/errno-as-thread-identifier/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 06:47:23 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[multithread]]></category>
		<category><![CDATA[portability]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=773</guid>
		<description><![CDATA[Thread identifiers may have different types in different systems &#8211; pthread_t under POSIX; uintptr_t or HANDLE under Windows. They are usually integers or pointers, but POSIX actually allows structures though not usually found. This leads to a little difficulty in portability. A trick, used by OpenSSL (if not overridden by calling CRYPTO_set_id_callback), is to use [...]<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Thread identifiers may have different types in different systems &#8211; <code>pthread_t</code> under POSIX; <code>uintptr_t</code> or <code>HANDLE</code> under Windows. They are usually integers or pointers, but POSIX actually allows structures though not usually found. This leads to a little difficulty in portability.</p>
<p>A trick, used by <a href="http://www.openssl.org">OpenSSL</a> (if not overridden by calling <a href="http://linux.die.net/man/3/crypto_set_id_callback"><code>CRYPTO_set_id_callback</code></a>), is to use <code>&amp;errno</code> as the thread identifier.</p>
<p>Per C standard, <code>errno</code> must be a modifiable lvalue, and thus we can safely take its address &#8211; <code>&amp;errno</code>. And under any practically usable thread implementation, <code>errno</code> must be <a href="http://en.wikipedia.org/wiki/Thread-local_storage">thread-local</a>, which means <code>&amp;errno</code> is different in different threads.</p>
<p><strong>Note</strong>: It seems OpenSSL defaults to <code>&amp;errno</code> only since 0.9.8m. Earlier versions always require the user to call <code>CRYPTO_set_id_callback</code>.</p>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2010/03/errno-as-thread-identifier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Acquire and Release Semantics</title>
		<link>http://en.chys.info/2010/03/acquire-and-release-semantics/</link>
		<comments>http://en.chys.info/2010/03/acquire-and-release-semantics/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 09:42:40 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[multithread]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=768</guid>
		<description><![CDATA[The concept of acquire and release semantics is important for multi-threaded programs that run on more than one physical core or processor. MSDN has a clear and concise explanation of then. Consider the following code example: a++; b++; c++; From another processor&#8217;s point of view, the preceding operations can appear to occur in any order. [...]<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>The concept of acquire and release semantics is important for multi-threaded programs that run on more than one <em>physical</em> core or processor. MSDN has <a href="http://msdn.microsoft.com/en-us/library/aa490209.aspx">a clear and concise explanation</a> of then.</p>
<blockquote><p>Consider the following code example:</p>
<blockquote><p><code>a++;</code><br />
<code>b++;</code><br />
<code>c++;</code></p></blockquote>
<p>From another processor&#8217;s point of view, the preceding operations can appear to occur in any order. For example, the other processor might see the increment of <code>b</code> before the increment of <code>a</code>.</p>
<p>&#8230;</p>
<p>[T]he <code>InterlockedIncrementAcquire</code> routine uses acquire semantics to increment a variable. If you rewrote the preceding code example as follows:</p>
<blockquote><p><code>InterlockedIncrementAcquire(&#038;a);</code><br />
<code>b++;</code><br />
<code>c++;</code></p></blockquote>
<p>other processors would always see the increment of <code>a</code> before the increments of <code>b</code> and <code>c</code>.</p>
<p>Likewise, the <code>InterlockedIncrementRelease</code> routine uses release semantics to increment a variable. If you rewrote the code example once again, as follows:</p>
<blockquote><p><code>a++;</code><br />
<code>b++;</code><br />
<code>InterlockedIncrementRelease(&#038;c);</code></p></blockquote>
<p>other processors would always see the increments of <code>a</code> and <code>b</code> before the increment of <code>c</code>.</p></blockquote>
<p>The operation of acquiring a lock must have acquire semantics; and the operation of releasing a lock must have release semantics. This is probably where they get their names.</p>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2010/03/acquire-and-release-semantics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

