<?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>梁剑的Blog &#187; Linux</title>
	<atom:link href="http://icomes.net/category/%e6%8a%80%e6%9c%af%e7%ac%94%e8%ae%b0/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://icomes.net</link>
	<description>做有趣的事，做有用的人</description>
	<lastBuildDate>Sat, 05 Nov 2011 18:23:06 +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>设置Putty Window Title动态变化的方法</title>
		<link>http://icomes.net/2010/04/01/%e8%ae%be%e7%bd%aeputty-window-title%e7%9a%84%e6%96%b9%e6%b3%95/</link>
		<comments>http://icomes.net/2010/04/01/%e8%ae%be%e7%bd%aeputty-window-title%e7%9a%84%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 07:08:06 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[putty]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=483</guid>
		<description><![CDATA[原以为在Putty里设置几个宏就行， 结果一个Hardcode的输入框和几个Disable的复选框之外，找不到其他有用的东西。 答案还是在Google里：） 需要在Server上设置一个环境变量， 现在的写法是： PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"'; 不单Putty，SecureCRT之类的终端应该也适用吧。 可以加到~/.profile，每次登陆自动生效。<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2008%2F08%2F23%2Fwindows%25E4%25B8%258B%25E7%2594%25A8xmanger3%25E8%25BF%259E%25E6%258E%25A5ubuntu-804%25E7%259A%2584x-window%2F&from=http%3A%2F%2Ficomes.net%2F2010%2F04%2F01%2F%25E8%25AE%25BE%25E7%25BD%25AEputty-window-title%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Windows下用Xmanger3连接Ubuntu 8.04的X Window</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2008%2F06%2F13%2F%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584windows%25E6%2589%25B9%25E5%25A4%2584%25E7%2590%2586%2F&from=http%3A%2F%2Ficomes.net%2F2010%2F04%2F01%2F%25E8%25AE%25BE%25E7%25BD%25AEputty-window-title%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">简单的windows批处理</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2007%2F09%2F16%2Fwindows%25E7%259A%2584%25E4%25BC%2591%25E7%259C%25A0%25E5%2591%25BD%25E4%25BB%25A4%2F&from=http%3A%2F%2Ficomes.net%2F2010%2F04%2F01%2F%25E8%25AE%25BE%25E7%25BD%25AEputty-window-title%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Windows的休眠命令</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2008%2F06%2F06%2Fwindows%25E6%2594%25AF%25E6%258C%2581%25E7%25AB%25AF%25E5%258F%25A3%25E6%2598%25A0%25E5%25B0%2584%2F&from=http%3A%2F%2Ficomes.net%2F2010%2F04%2F01%2F%25E8%25AE%25BE%25E7%25BD%25AEputty-window-title%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Windows支持端口映射</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>原以为在Putty里设置几个宏就行，<br />
结果一个Hardcode的输入框和几个Disable的复选框之外，找不到其他有用的东西。</p>
<p>答案还是在Google里：）<br />
需要在Server上设置一个环境变量，<br />
现在的写法是：</p>
<pre>
<code>
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"';
</code>
</pre>
<p>不单Putty，SecureCRT之类的终端应该也适用吧。<br />
可以加到~/.profile，每次登陆自动生效。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2008%2F08%2F23%2Fwindows%25E4%25B8%258B%25E7%2594%25A8xmanger3%25E8%25BF%259E%25E6%258E%25A5ubuntu-804%25E7%259A%2584x-window%2F&from=http%3A%2F%2Ficomes.net%2F2010%2F04%2F01%2F%25E8%25AE%25BE%25E7%25BD%25AEputty-window-title%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Windows下用Xmanger3连接Ubuntu 8.04的X Window</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2008%2F06%2F13%2F%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584windows%25E6%2589%25B9%25E5%25A4%2584%25E7%2590%2586%2F&from=http%3A%2F%2Ficomes.net%2F2010%2F04%2F01%2F%25E8%25AE%25BE%25E7%25BD%25AEputty-window-title%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">简单的windows批处理</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2007%2F09%2F16%2Fwindows%25E7%259A%2584%25E4%25BC%2591%25E7%259C%25A0%25E5%2591%25BD%25E4%25BB%25A4%2F&from=http%3A%2F%2Ficomes.net%2F2010%2F04%2F01%2F%25E8%25AE%25BE%25E7%25BD%25AEputty-window-title%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Windows的休眠命令</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2008%2F06%2F06%2Fwindows%25E6%2594%25AF%25E6%258C%2581%25E7%25AB%25AF%25E5%258F%25A3%25E6%2598%25A0%25E5%25B0%2584%2F&from=http%3A%2F%2Ficomes.net%2F2010%2F04%2F01%2F%25E8%25AE%25BE%25E7%25BD%25AEputty-window-title%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Windows支持端口映射</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2010/04/01/%e8%ae%be%e7%bd%aeputty-window-title%e7%9a%84%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>一个简单的进程池实现</title>
		<link>http://icomes.net/2009/10/11/%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e8%bf%9b%e7%a8%8b%e6%b1%a0%e5%ae%9e%e7%8e%b0/</link>
		<comments>http://icomes.net/2009/10/11/%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e8%bf%9b%e7%a8%8b%e6%b1%a0%e5%ae%9e%e7%8e%b0/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 10:43:49 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术笔记]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=448</guid>
		<description><![CDATA[实在很简陋，许多控制功能都没有实现：） 不过也能表现大致的框架吧。 /* * A simple process pool */ #include &#60;iostream&#62; #include &#60;queue&#62; #include &#60;vector&#62; #include &#60;algorithm&#62; &#160; #include &#60;sys/wait.h&#62; #include &#60;assert.h&#62; #include &#60;stdlib.h&#62; #include &#60;unistd.h&#62; #include &#60;signal.h&#62; &#160; #include &#60;sys/mman.h&#62; #include &#60;sys/select.h&#62; &#160; using namespace std; &#160; int iProcessCount = 0; int *pDispatchCount; &#160; void InterruptHanler&#40;int iSigno&#41; &#123; //ToDo: kill all children cerr &#60;&#60; [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Linux下进程与线程的比较（1）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F03%2F29%2Flinux%25E4%25B8%258B%25E8%25BF%259B%25E7%25A8%258B%25E4%25B8%258E%25E7%25BA%25BF%25E7%25A8%258B%25E7%259A%2584%25E6%25AF%2594%25E8%25BE%2583%25EF%25BC%25881%25EF%25BC%2589%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Linux下进程与线程的比较（1）</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Linux下的进程同步机制 – 记录锁" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Linux下的进程同步机制 – 记录锁</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Another Hello World is running on my iPhone!" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F08%2F30%2Fanother-hello-world-is-running-on-my-iphone%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/29/15015234.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Another Hello World is running on my iPhone!</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Ubuntu 8.04 Server在VMWare中无法启动的解决办法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2008%2F06%2F29%2Fubuntu-804-server%25E5%259C%25A8vmware%25E4%25B8%25AD%25E6%2597%25A0%25E6%25B3%2595%25E5%2590%25AF%25E5%258A%25A8%25E7%259A%2584%25E8%25A7%25A3%25E5%2586%25B3%25E5%258A%259E%25E6%25B3%2595%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/29/15017346.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Ubuntu 8.04 Server在VMWare中无法启动的解决办法</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>实在很简陋，许多控制功能都没有实现：）<br />
不过也能表现大致的框架吧。</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*
 * A simple process pool
 */</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;queue&gt;</span>
<span style="color: #339900;">#include &lt;vector&gt;</span>
<span style="color: #339900;">#include &lt;algorithm&gt;</span>
&nbsp;
<span style="color: #339900;">#include &lt;sys/wait.h&gt;</span>
<span style="color: #339900;">#include &lt;assert.h&gt;</span>
<span style="color: #339900;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339900;">#include &lt;unistd.h&gt;</span>
<span style="color: #339900;">#include &lt;signal.h&gt;</span>
&nbsp;
<span style="color: #339900;">#include &lt;sys/mman.h&gt;</span>
<span style="color: #339900;">#include &lt;sys/select.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> iProcessCount <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span>pDispatchCount<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> InterruptHanler<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> iSigno<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">//ToDo: kill all children</span>
	<span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Dispatch Count: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> iProcessCount<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #000080;">&lt;&lt;</span> pDispatchCount<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000dd;">exit</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">//ToDo: replace two pipes with socketpair</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span>argv<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> argc <span style="color: #000080;">==</span> <span style="color: #0000dd;">2</span> <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		iProcessCount <span style="color: #000080;">=</span> <span style="color: #0000dd;">atoi</span><span style="color: #008000;">&#40;</span>argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> iProcessCount <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>
		iProcessCount <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>iPipeFD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#91;</span>iProcessCount<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>iNotifyPipeFD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#91;</span>iProcessCount<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;;</span>
	pDispatchCount <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>mmap<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> iProcessCount, PROT_READ <span style="color: #000040;">|</span> PROT_WRITE, MAP_SHARED <span style="color: #000040;">|</span> MAP_ANONYMOUS, <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	queue<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> queIdle<span style="color: #008080;">;</span>
	vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> vecBusy<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> iProcessCount<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pipe<span style="color: #008000;">&#40;</span>iPipeFD<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;pipe error&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>	
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pipe<span style="color: #008000;">&#40;</span>iNotifyPipeFD<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;pipe error&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>	
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> iProcessCount<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		pid_t pid <span style="color: #000080;">=</span> fork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pid <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cerr</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;fork error&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pid <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>	<span style="color: #666666;">//child</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">int</span> iCount <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0000ff;">char</span> sBuf<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">16</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
				read<span style="color: #008000;">&#40;</span> iPipeFD<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, sBuf, <span style="color: #0000dd;">15</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> getpid<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">++</span>iCount <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #000080;">&lt;&lt;</span> sBuf <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
				pDispatchCount<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
&nbsp;
				<span style="color: #666666;">//Do your work here</span>
				<span style="color: #0000ff;">int</span> iMax <span style="color: #000080;">=</span> <span style="color: #0000dd;">atoi</span><span style="color: #008000;">&#40;</span>sBuf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				usleep<span style="color: #008000;">&#40;</span>iMax <span style="color: #000040;">%</span> <span style="color: #0000dd;">10000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
				write<span style="color: #008000;">&#40;</span> iNotifyPipeFD<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>, <span style="color: #FF0000;">&quot;1&quot;</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		queIdle.<span style="color: #007788;">push</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">signal</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">SIGINT</span>, InterruptHanler <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> iCounter <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> 
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> queIdle.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #666666;">//Dispatch work</span>
			<span style="color: #0000ff;">char</span> sBuf<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">16</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">int</span> iRand <span style="color: #000080;">=</span> <span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">snprintf</span><span style="color: #008000;">&#40;</span>sBuf, <span style="color: #0000dd;">16</span>, <span style="color: #FF0000;">&quot;%d&quot;</span>, iRand<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000ff;">int</span> iIndex <span style="color: #000080;">=</span> queIdle.<span style="color: #007788;">front</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			queIdle.<span style="color: #007788;">pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			write<span style="color: #008000;">&#40;</span> iPipeFD<span style="color: #008000;">&#91;</span>iIndex<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>, sBuf, <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>sBuf<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;dispatch &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sBuf <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; to &quot;</span> <span style="color: #000080;">&lt;&lt;</span> iIndex <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
			vecBusy.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>iIndex<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">++</span>iCounter <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>: queIdle.size = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> queIdle.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; vecBusy.size = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> vecBusy.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #666666;">//if( vecBusy.size() )</span>
		<span style="color: #008000;">&#123;</span>
			fd_set set<span style="color: #008080;">;</span>
			FD_ZERO<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>set<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000ff;">int</span> iMaxFD <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>	
			<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> vecBusy.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0000ff;">int</span> iIndex <span style="color: #000080;">=</span> vecBusy<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				FD_SET<span style="color: #008000;">&#40;</span> iNotifyPipeFD<span style="color: #008000;">&#91;</span>iIndex<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, <span style="color: #000040;">&amp;</span>set<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				iMaxFD <span style="color: #000080;">=</span> max<span style="color: #008000;">&#40;</span> iMaxFD, iNotifyPipeFD<span style="color: #008000;">&#91;</span>iIndex<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #0000ff;">struct</span> timeval timeout<span style="color: #008080;">;</span>
			timeout.<span style="color: #007788;">tv_sec</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
			timeout.<span style="color: #007788;">tv_usec</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
			select<span style="color: #008000;">&#40;</span>iMaxFD <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span>, <span style="color: #000040;">&amp;</span>set, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #000040;">&amp;</span>timeout<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> it <span style="color: #000080;">=</span> vecBusy.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> vecBusy.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0000ff;">int</span> iIndex <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>it<span style="color: #008080;">;</span>
				<span style="color: #0000ff;">int</span> iReadFD <span style="color: #000080;">=</span> iNotifyPipeFD<span style="color: #008000;">&#91;</span>iIndex<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> FD_ISSET<span style="color: #008000;">&#40;</span> iReadFD, <span style="color: #000040;">&amp;</span>set <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#123;</span>
					queIdle.<span style="color: #007788;">push</span><span style="color: #008000;">&#40;</span>iIndex<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
					it <span style="color: #000080;">=</span> vecBusy.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span>
				<span style="color: #0000ff;">else</span>
				<span style="color: #008000;">&#123;</span>
					it<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Linux下进程与线程的比较（1）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F03%2F29%2Flinux%25E4%25B8%258B%25E8%25BF%259B%25E7%25A8%258B%25E4%25B8%258E%25E7%25BA%25BF%25E7%25A8%258B%25E7%259A%2584%25E6%25AF%2594%25E8%25BE%2583%25EF%25BC%25881%25EF%25BC%2589%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Linux下进程与线程的比较（1）</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Linux下的进程同步机制 – 记录锁" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Linux下的进程同步机制 – 记录锁</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="新主题iBlog2" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F09%2F15%2F%25E6%2596%25B0%25E4%25B8%25BB%25E9%25A2%2598iblog2%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/29/15014886.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">新主题iBlog2</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="用visio画的UML图例" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F10%2F18%2F%25E7%2594%25A8visio%25E7%2594%25BB%25E7%259A%2584uml%25E5%259B%25BE%25E4%25BE%258B%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/29/15015128.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">用visio画的UML图例</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/10/11/%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e8%bf%9b%e7%a8%8b%e6%b1%a0%e5%ae%9e%e7%8e%b0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux内核Credits列表里的中国人</title>
		<link>http://icomes.net/2009/08/09/linux%e5%86%85%e6%a0%b8credits%e5%88%97%e8%a1%a8%e9%87%8c%e7%9a%84%e4%b8%ad%e5%9b%bd%e4%ba%ba/</link>
		<comments>http://icomes.net/2009/08/09/linux%e5%86%85%e6%a0%b8credits%e5%88%97%e8%a1%a8%e9%87%8c%e7%9a%84%e4%b8%ad%e5%9b%bd%e4%ba%ba/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 07:40:59 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=422</guid>
		<description><![CDATA[闲来无事，翻看了Linux的CREDITS。 里面只有三个中国人，其中两个是台湾的，剩下一个在北京，写过Freescale的USB驱动。<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F03%2F29%2Flinux%25E4%25B8%258B%25E8%25BF%259B%25E7%25A8%258B%25E4%25B8%258E%25E7%25BA%25BF%25E7%25A8%258B%25E7%259A%2584%25E6%25AF%2594%25E8%25BE%2583%25EF%25BC%25881%25EF%25BC%2589%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Linux下进程与线程的比较（1）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Linux下的进程同步机制 – 记录锁</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F02%2F10%2F%25E5%2588%2586%25E4%25BA%25AB%25E7%2589%259B%25E4%25BA%25BA%25E5%2586%2599%25E7%259A%2584%25E4%25B8%25A4%25E4%25B8%25AAppt%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">分享牛人写的两个ppt</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F07%2F25%2F%25E5%2588%25A9%25E7%2594%25A8ssh-tunnel%25E5%25BB%25BA%25E7%25AB%258B%25E6%259C%25AC%25E5%259C%25B0sock%25E4%25BB%25A3%25E7%2590%2586%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">利用ssh tunnel建立本地sock代理</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>闲来无事，翻看了Linux的<a href="http://lxr.linux.no/linux+v2.6.30.4/CREDITS" target="_blank">CREDITS</a>。</p>
<p>里面只有三个中国人，其中两个是台湾的，剩下一个在北京，写过Freescale的USB驱动。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F03%2F29%2Flinux%25E4%25B8%258B%25E8%25BF%259B%25E7%25A8%258B%25E4%25B8%258E%25E7%25BA%25BF%25E7%25A8%258B%25E7%259A%2584%25E6%25AF%2594%25E8%25BE%2583%25EF%25BC%25881%25EF%25BC%2589%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Linux下进程与线程的比较（1）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Linux下的进程同步机制 – 记录锁</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F02%2F10%2F%25E5%2588%2586%25E4%25BA%25AB%25E7%2589%259B%25E4%25BA%25BA%25E5%2586%2599%25E7%259A%2584%25E4%25B8%25A4%25E4%25B8%25AAppt%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">分享牛人写的两个ppt</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F04%2F06%2Fubunt904%25E7%25BB%2588%25E4%25BA%258E%25E5%258F%2591%25E5%25B8%2583%25E4%25BA%2586%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Ubunt9.04终于发布了</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/08/09/linux%e5%86%85%e6%a0%b8credits%e5%88%97%e8%a1%a8%e9%87%8c%e7%9a%84%e4%b8%ad%e5%9b%bd%e4%ba%ba/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux下的进程同步机制 &#8211; 记录锁</title>
		<link>http://icomes.net/2009/04/18/linux%e4%b8%8b%e7%9a%84%e8%bf%9b%e7%a8%8b%e5%90%8c%e6%ad%a5%e6%9c%ba%e5%88%b6-%e8%ae%b0%e5%bd%95%e9%94%81/</link>
		<comments>http://icomes.net/2009/04/18/linux%e4%b8%8b%e7%9a%84%e8%bf%9b%e7%a8%8b%e5%90%8c%e6%ad%a5%e6%9c%ba%e5%88%b6-%e8%ae%b0%e5%bd%95%e9%94%81/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 12:53:16 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[同步]]></category>
		<category><![CDATA[锁]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=368</guid>
		<description><![CDATA[当可能出现几个进程争用（读、写）同一个Critical Section的时候，加锁是常用的做法。 Linux加锁的方法，除了经典的IPC（Semophore）之外，记录锁（Record Locking）提供了更简单的方法。 其实记录锁的名字叫文件锁会比较贴切一点，因为其加锁和解锁都是通过对文件的操作完成的。 文件锁的粒度大可到整个文件，小可到一个字节，长度可变，但都可以说是对应一个Record（逻辑意义上）。 对锁的控制是通过调用fcntl实现的，基本的方式如下： fcntl&#40;fd, operation, flock&#41;; fd是某个文件的句柄，该fd需要以与type相匹配的方式open operation是操作类型 F_GETLK 读取锁信息 F_SETLK 设置锁，在锁已经被占用的情况下，马上返回错误，有点类似于pthread的trylock F_SETLKW 设置锁，如果锁被其他进程占用，则阻塞 flock是个struct，用来传递锁的详细信息 short int l_type 锁的类型，可以是F_RDLCK、 F_WRLCK、F_UNLCK，分别对应读锁、写锁和解锁 short int l_whence 与l_start一起决定锁的起始位置，SEEK_SET、SEEK_CUR、SEEK_END分别对应文件的开始、当前位置和末尾，和fseek、lseek里的含义一致 off_t l_start 起始位置 off_t l_len 长度，0表示从l_start到文件的末尾。据说某些实现支持负数 pid_t l_pid 拥有锁的进程，operation为GETLK的时候会被设置 fd所对应的文件，本身不需要有数据。 由operation的取值，flock的定义可以看出，其实记录锁非常灵活。 它既可以实现排他锁（F_WRLCK），也可以实现共享锁（F_RDLCK）； 同时也支持同步锁（F_SETLKW）和异步锁（F_SETLK）。 记录锁的另一个好处时，进程退出时，会自动释放掉自己所占用的锁。 这就避免了进程异常退出时资源无法回收的问题。 速度也是需要考虑的因素，根据测试，记录锁相对最慢； 但综合考虑易用性和灵活性，我认为这样的速度损耗是可以接受的。 flock(2)是另一种实现文件锁的方法，详情可以man 2 flock。<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F03%2F29%2Flinux%25E4%25B8%258B%25E8%25BF%259B%25E7%25A8%258B%25E4%25B8%258E%25E7%25BA%25BF%25E7%25A8%258B%25E7%259A%2584%25E6%25AF%2594%25E8%25BE%2583%25EF%25BC%25881%25EF%25BC%2589%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Linux下进程与线程的比较（1）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一个简单的进程池实现</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Linux内核Credits列表里的中国人</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F08%2F29%2Fthe_productive_programme_mechanics%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">The Productive Programmer – 机制</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>当可能出现几个进程争用（读、写）同一个Critical Section的时候，加锁是常用的做法。<br />
Linux加锁的方法，除了经典的IPC（Semophore）之外，记录锁（Record Locking）提供了更简单的方法。</p>
<p>其实记录锁的名字叫文件锁会比较贴切一点，因为其加锁和解锁都是通过对文件的操作完成的。<br />
文件锁的粒度大可到整个文件，小可到一个字节，长度可变，但都可以说是对应一个Record（逻辑意义上）。</p>
<p>对锁的控制是通过调用fcntl实现的，基本的方式如下：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">fcntl<span style="color: #008000;">&#40;</span>fd, operation, flock<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>fd是某个文件的句柄，该fd需要以与type相匹配的方式open<br />
operation是操作类型</p>
<ul>
<li>F_GETLK  读取锁信息</li>
<li>F_SETLK   设置锁，在锁已经被占用的情况下，马上返回错误，有点类似于pthread的trylock</li>
<li>F_SETLKW 设置锁，如果锁被其他进程占用，则阻塞</li>
</ul>
<p>flock是个struct，用来传递锁的详细信息</p>
<ul>
<li>short int l_type       锁的类型，可以是F_RDLCK、 F_WRLCK、F_UNLCK，分别对应读锁、写锁和解锁</li>
<li>short int l_whence  与l_start一起决定锁的起始位置，SEEK_SET、SEEK_CUR、SEEK_END分别对应文件的开始、当前位置和末尾，和fseek、lseek里的含义一致</li>
<li>off_t l_start             起始位置</li>
<li>off_t l_len               长度，0表示从l_start到文件的末尾。据说某些实现支持负数</li>
<li>pid_t l_pid              拥有锁的进程，operation为GETLK的时候会被设置</li>
</ul>
<p>fd所对应的文件，本身不需要有数据。</p>
<p>由operation的取值，flock的定义可以看出，其实记录锁非常灵活。<br />
它既可以实现排他锁（F_WRLCK），也可以实现共享锁（F_RDLCK）；<br />
同时也支持同步锁（F_SETLKW）和异步锁（F_SETLK）。</p>
<p>记录锁的另一个好处时，进程退出时，会自动释放掉自己所占用的锁。<br />
这就避免了进程异常退出时资源无法回收的问题。</p>
<p>速度也是需要考虑的因素，根据测试，记录锁相对最慢；<br />
但综合考虑易用性和灵活性，我认为这样的速度损耗是可以接受的。</p>
<p>flock(2)是另一种实现文件锁的方法，详情可以man 2 flock。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F03%2F29%2Flinux%25E4%25B8%258B%25E8%25BF%259B%25E7%25A8%258B%25E4%25B8%258E%25E7%25BA%25BF%25E7%25A8%258B%25E7%259A%2584%25E6%25AF%2594%25E8%25BE%2583%25EF%25BC%25881%25EF%25BC%2589%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Linux下进程与线程的比较（1）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F10%2F11%2F%25E4%25B8%2580%25E4%25B8%25AA%25E7%25AE%2580%25E5%258D%2595%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E6%25B1%25A0%25E5%25AE%259E%25E7%258E%25B0%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一个简单的进程池实现</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F08%2F09%2Flinux%25E5%2586%2585%25E6%25A0%25B8credits%25E5%2588%2597%25E8%25A1%25A8%25E9%2587%258C%25E7%259A%2584%25E4%25B8%25AD%25E5%259B%25BD%25E4%25BA%25BA%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Linux内核Credits列表里的中国人</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F08%2F29%2Fthe_productive_programme_mechanics%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F18%2Flinux%25E4%25B8%258B%25E7%259A%2584%25E8%25BF%259B%25E7%25A8%258B%25E5%2590%258C%25E6%25AD%25A5%25E6%259C%25BA%25E5%2588%25B6-%25E8%25AE%25B0%25E5%25BD%2595%25E9%2594%2581%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">The Productive Programmer – 机制</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/04/18/linux%e4%b8%8b%e7%9a%84%e8%bf%9b%e7%a8%8b%e5%90%8c%e6%ad%a5%e6%9c%ba%e5%88%b6-%e8%ae%b0%e5%bd%95%e9%94%81/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>/etc/fstab</title>
		<link>http://icomes.net/2009/04/11/etcfstab/</link>
		<comments>http://icomes.net/2009/04/11/etcfstab/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 12:34:52 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[分区]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=365</guid>
		<description><![CDATA[保存下来，以备不时之需 # /dev/sda8 UUID=2cf88d46-8b6e-4eb3-bc2f-5f0680983299 / ext3 relatime,errors=remount-ro 0 1 # /dev/sda11 UUID=5822ad0d-4fc6-428f-9334-0a5d022a808d /usr ext3 relatime,errors=remount-ro 0 1 # /dev/sda7 UUID=1c2431ce-68cf-4c10-89c9-f86a52389c9a /data ext3 relatime,errors=remount-ro 0 1 # /dev/sda9 UUID=5c73fb68-6e3e-49e6-b7a8-0f21e9479aaa /home ext3 relatime 0 2 # /dev/sda10 UUID=99e2d59c-28e5-47fb-a3c7-3ea3ca10aebb none swap sw 0 0<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="浏览器战争" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F01%2F13%2F%25E6%25B5%258F%25E8%25A7%2588%25E5%2599%25A8%25E6%2588%2598%25E4%25BA%2589%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F11%2Fetcfstab%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/29/15015466.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">浏览器战争</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="利用ssh tunnel建立本地sock代理" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F07%2F25%2F%25E5%2588%25A9%25E7%2594%25A8ssh-tunnel%25E5%25BB%25BA%25E7%25AB%258B%25E6%259C%25AC%25E5%259C%25B0sock%25E4%25BB%25A3%25E7%2590%2586%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F11%2Fetcfstab%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/29/15015597.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用ssh tunnel建立本地sock代理</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="阅读空间的GM脚本" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2009%2F01%2F17%2F%25E9%2598%2585%25E8%25AF%25BB%25E7%25A9%25BA%25E9%2597%25B4%25E7%259A%2584gs%25E8%2584%259A%25E6%259C%25AC%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F11%2Fetcfstab%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/29/15015260.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">阅读空间的GM脚本</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="The Last Lecture" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ficomes.net%2F2011%2F10%2F15%2Fthe-last-lecture%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F04%2F11%2Fetcfstab%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/10/15/9228539.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">The Last Lecture</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>保存下来，以备不时之需</p>
<pre>
# /dev/sda8
UUID=2cf88d46-8b6e-4eb3-bc2f-5f0680983299 /               ext3    relatime,errors=remount-ro 0       1
# /dev/sda11
UUID=5822ad0d-4fc6-428f-9334-0a5d022a808d /usr            ext3    relatime,errors=remount-ro 0       1
# /dev/sda7
UUID=1c2431ce-68cf-4c10-89c9-f86a52389c9a /data            ext3    relatime,errors=remount-ro 0       1
# /dev/sda9
UUID=5c73fb68-6e3e-49e6-b7a8-0f21e9479aaa /home           ext3    relatime        0       2
# /dev/sda10
UUID=99e2d59c-28e5-47fb-a3c7-3ea3ca10aebb none            swap    sw              0       0
</pre>
]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/04/11/etcfstab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DropBox在Ubuntu 9.04下不能工作的解决方法</title>
		<link>http://icomes.net/2009/04/07/dropbox%e5%9c%a8ubuntu-904%e4%b8%8b%e4%b8%8d%e8%83%bd%e5%b7%a5%e4%bd%9c%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/</link>
		<comments>http://icomes.net/2009/04/07/dropbox%e5%9c%a8ubuntu-904%e4%b8%8b%e4%b8%8d%e8%83%bd%e5%b7%a5%e4%bd%9c%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 14:56:05 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[dropbox]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=353</guid>
		<description><![CDATA[升级到9.04之后，面板上的DropBox图标点击没有了反应。 直觉上是DropBox不支持Jaunty， 到DropBox的网站，没有发现支持Jaunty的版本。 遇到解决不了问题的时候，有两件事我的确定的： 1. 我不会是世界上第一个遇到这样问题的人； 2. 问题的答案通过Google可以找到。 这次也不例外，下面就是结果：） cd ~ wget http://dl.getdropbox.com/u/17/dropbox-lnx.x86-0.6.491.tar.gz rm -r .dropbox-dist/ tar xzf dropbox-lnx.x86-0.6.491.tar.gz killall nautilus 这是来源 这只是临时解决方法，相信DropBox很快会有新版本发布。]]></description>
			<content:encoded><![CDATA[<p>升级到9.04之后，面板上的DropBox图标点击没有了反应。<br />
直觉上是DropBox不支持Jaunty，<br />
到DropBox的网站，没有发现支持Jaunty的版本。</p>
<p>遇到解决不了问题的时候，有两件事我的确定的：<br />
1. 我不会是世界上第一个遇到这样问题的人；<br />
2. 问题的答案通过Google可以找到。</p>
<p>这次也不例外，下面就是结果：）</p>
<pre>
cd ~
wget http://dl.getdropbox.com/u/17/dropbox-lnx.x86-0.6.491.tar.gz
rm -r .dropbox-dist/
tar xzf dropbox-lnx.x86-0.6.491.tar.gz
killall nautilus
</pre>
<p>这是<a href="http://adammichaelroach.com/blog/033009/get-dropbox-working-ubuntu-jaunty-beta" taget="_blank">来源</a></p>
<p>这只是临时解决方法，相信DropBox很快会有新版本发布。</p>
]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/04/07/dropbox%e5%9c%a8ubuntu-904%e4%b8%8b%e4%b8%8d%e8%83%bd%e5%b7%a5%e4%bd%9c%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ubunt9.04终于发布了</title>
		<link>http://icomes.net/2009/04/06/ubunt904%e7%bb%88%e4%ba%8e%e5%8f%91%e5%b8%83%e4%ba%86/</link>
		<comments>http://icomes.net/2009/04/06/ubunt904%e7%bb%88%e4%ba%8e%e5%8f%91%e5%b8%83%e4%ba%86/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 02:33:21 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=349</guid>
		<description><![CDATA[不是官方网站上声称的4.23，而是今天：） 升级中……]]></description>
			<content:encoded><![CDATA[<p>不是官方网站上声称的4.23，而是今天：）<br />

<a href='http://icomes.net/2009/04/06/ubunt904%e7%bb%88%e4%ba%8e%e5%8f%91%e5%b8%83%e4%ba%86/ubuntu-904-update/' title='ubuntu-904-update'><img width="150" height="150" src="http://icomes.net/wp-content/uploads/2009/04/ubuntu-904-update-150x150.png" class="attachment-thumbnail" alt="ubuntu-904-update" title="ubuntu-904-update" /></a>
<br />
升级中……</p>
]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/04/06/ubunt904%e7%bb%88%e4%ba%8e%e5%8f%91%e5%b8%83%e4%ba%86/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>删除多余换行的awk命令</title>
		<link>http://icomes.net/2009/02/03/%e5%88%a0%e9%99%a4%e5%a4%9a%e4%bd%99%e6%8d%a2%e8%a1%8c%e7%9a%84awk%e5%91%bd%e4%bb%a4/</link>
		<comments>http://icomes.net/2009/02/03/%e5%88%a0%e9%99%a4%e5%a4%9a%e4%bd%99%e6%8d%a2%e8%a1%8c%e7%9a%84awk%e5%91%bd%e4%bb%a4/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 14:56:23 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[电子书]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=303</guid>
		<description><![CDATA[找到了txt版本的天龙八部，准备放到手机上，但发现这个版本略有瑕疵。 可能是为了在固定宽度的显示器上显示的缘故，往往到了一定字数，文章就会折行。 萧峰道：“不行！”突然拍出一掌，击向木几，只听得劈拍一声响，木几碎成 数块，匕首随而跌落，凛然说道：“杀母大仇，岂可当作买卖交易？此仇能报便报， 如不能报，则我父子毕命于此便了。这等肮脏之事，岂是我萧氏父子所屑为？” 慕容博仰天大笑，朗声说道：“我素闻萧峰萧大侠才略盖世，识见非凡，殊不 知今日一见，竟虽个不明大义、徒逞意气的一勇之夫。嘿嘿，可笑啊可笑！” 萧峰知他是以言语相激，冷冷的道：“萧峰是英雄豪杰也罢，是凡夫俗子也罢， 总不能中你圈套，成为手中的杀人之刀。” 慕容博道：“食君之禄，忠君之事。你是大辽国这臣，欲只记得父母私仇，不 思尽忠报国，如何对得起大辽？” 萧峰蹭上一步，昂然说到：“你可曾见过边关之上、宋辽相互仇杀的惨状？可 曾见过宋人辽人妻离子散、家破人亡的情景？宋辽之间好容易罢兵数十年，倘若刀 兵再起，契丹铁骑侵入南朝，你可知将有多少宋人惨遭横死？多少辽人死于非命？” 他说到这里，想起当日雁门关外宋兵和辽兵相互打草谷的残酷情状，越说越响，又 道：“兵凶战危，世间岂有必胜之事？大宋兵多财足，只须有一二名将，率兵奋战， 大辽、吐蕃联手，未必便能取胜。咱们打一个血流成河，尸骨如山，欲让你慕容氏 来乘机兴复燕国，我对大辽尽忠报国，是在保土安民，而不是为了一己的荣华富贵， 因而杀人取地、建功立业。” 忽听得长窗外一个苍老的声音说道：“善哉，善哉！萧居士宅心仁厚，如此以 天下苍生为念，当真是菩萨心肠。” 这样的格式在640*480的屏幕上可能很好，但是在iPhone上看殊为不爽。 解决方法： awk 'BEGIN{ while(getline){ if( $0 !~ /^$/ ) printf $0; else printf &#34;\n&#34; } } END{printf &#34;\n&#34;}' filename 找解决方法的过程中，发现了两个附加产品： 下载txt电子书的网站（不是论坛，无需注册，虽然广告很多） GNU的awk手册（比种种学习笔记要好得多） 曾尝试用sed，不果。 Update: 20090206 针对中文书，优化了一下 for f in *; do awk 'BEGIN{ [...]]]></description>
			<content:encoded><![CDATA[<p>找到了txt版本的天龙八部，准备放到手机上，但发现这个版本略有瑕疵。<br />
可能是为了在固定宽度的显示器上显示的缘故，往往到了一定字数，文章就会折行。</p>
<blockquote><p>萧峰道：“不行！”突然拍出一掌，击向木几，只听得劈拍一声响，木几碎成<br />
数块，匕首随而跌落，凛然说道：“杀母大仇，岂可当作买卖交易？此仇能报便报，<br />
如不能报，则我父子毕命于此便了。这等肮脏之事，岂是我萧氏父子所屑为？”</p>
<p>慕容博仰天大笑，朗声说道：“我素闻萧峰萧大侠才略盖世，识见非凡，殊不<br />
知今日一见，竟虽个不明大义、徒逞意气的一勇之夫。嘿嘿，可笑啊可笑！”</p>
<p>萧峰知他是以言语相激，冷冷的道：“萧峰是英雄豪杰也罢，是凡夫俗子也罢，<br />
总不能中你圈套，成为手中的杀人之刀。”</p>
<p>慕容博道：“食君之禄，忠君之事。你是大辽国这臣，欲只记得父母私仇，不<br />
思尽忠报国，如何对得起大辽？”</p>
<p>萧峰蹭上一步，昂然说到：“你可曾见过边关之上、宋辽相互仇杀的惨状？可<br />
曾见过宋人辽人妻离子散、家破人亡的情景？宋辽之间好容易罢兵数十年，倘若刀<br />
兵再起，契丹铁骑侵入南朝，你可知将有多少宋人惨遭横死？多少辽人死于非命？”<br />
他说到这里，想起当日雁门关外宋兵和辽兵相互打草谷的残酷情状，越说越响，又<br />
道：“兵凶战危，世间岂有必胜之事？大宋兵多财足，只须有一二名将，率兵奋战，<br />
大辽、吐蕃联手，未必便能取胜。咱们打一个血流成河，尸骨如山，欲让你慕容氏<br />
来乘机兴复燕国，我对大辽尽忠报国，是在保土安民，而不是为了一己的荣华富贵，<br />
因而杀人取地、建功立业。”</p>
<p>忽听得长窗外一个苍老的声音说道：“善哉，善哉！萧居士宅心仁厚，如此以<br />
天下苍生为念，当真是菩萨心肠。”</p></blockquote>
<p>这样的格式在640*480的屏幕上可能很好，但是在iPhone上看殊为不爽。<br />
解决方法：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'BEGIN{ while(getline){ if( $0 !~ /^$/ ) printf $0; else printf &quot;\n&quot; } }  END{printf &quot;\n&quot;}'</span>  filename</pre></div></div>

<p>找解决方法的过程中，发现了两个附加产品：</p>
<ol>
<li><a href="http://www.bookdown.com.cn/" target="_blank">下载txt电子书的网站</a>（不是论坛，无需注册，虽然广告很多）</li>
<li><a href="http://www.gnu.org/manual/gawk/html_node/index.html" target="_blank">GNU的awk手册</a>（比种种学习笔记要好得多）</li>
</ol>
<p>曾尝试用sed，不果。</p>
<p>Update: 20090206<br />
针对中文书，优化了一下</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> f <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'BEGIN{ while(getline){ if( $0 !~ /^[ \t\xa1]+/ ) printf $0; else printf &quot;\r\n%s&quot;, $0 } }  END{printf &quot;\n&quot;}'</span>  <span style="color: #007800;">$f</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$f</span>.new; <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>xA1A1是全角的\t</p>
]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/02/03/%e5%88%a0%e9%99%a4%e5%a4%9a%e4%bd%99%e6%8d%a2%e8%a1%8c%e7%9a%84awk%e5%91%bd%e4%bb%a4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>最简单的ls实现</title>
		<link>http://icomes.net/2008/12/24/%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84ls%e5%ae%9e%e7%8e%b0/</link>
		<comments>http://icomes.net/2008/12/24/%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84ls%e5%ae%9e%e7%8e%b0/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 09:13:46 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[coreutils]]></category>
		<category><![CDATA[dir]]></category>

		<guid isPermaLink="false">http://tridot.cn/?p=224</guid>
		<description><![CDATA[我肯定不是第一个不知道ls是怎么实现的人。 一般大家都知道怎么在linux下打开一个文件，然后读取里面的内容； 不过怎么样遍历一个目录，可能不少人还不太清楚用什么API。 下面是一个简单的ls demo程序，可以演示操作目录的模式。 #include &#60;sys/types.h&#62; #include &#60;dirent.h&#62; #include &#60;stdio.h&#62; #include &#60;sys/stat.h&#62; #include &#60;unistd.h&#62; &#160; int ls&#40;char *sPathName&#41; &#123; DIR *dir = opendir&#40;sPathName&#41;; if&#40; dir == NULL &#41; &#123; printf&#40;&#34;open %s fail.\n&#34;, sPathName&#41;; return -1; &#125; chdir&#40;sPathName&#41;; struct dirent *file; while&#40; file = readdir&#40; dir &#41; &#41; &#123; #ifdef _DIRENT_HAVE_D_TYPE //如果是普通文件 if&#40; file-&#38;gt;d_type &#38;amp; [...]]]></description>
			<content:encoded><![CDATA[<p>我肯定不是第一个不知道ls是怎么实现的人。<br />
一般大家都知道怎么在linux下打开一个文件，然后读取里面的内容；<br />
不过怎么样遍历一个目录，可能不少人还不太清楚用什么API。</p>
<p>下面是一个简单的ls demo程序，可以演示操作目录的模式。</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;sys/types.h&gt;</span>
<span style="color: #339900;">#include &lt;dirent.h&gt;</span>
<span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;sys/stat.h&gt;</span>
<span style="color: #339900;">#include &lt;unistd.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> ls<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>sPathName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    DIR <span style="color: #000040;">*</span>dir <span style="color: #000080;">=</span> opendir<span style="color: #008000;">&#40;</span>sPathName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> dir <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span> <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;open %s fail.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, sPathName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    chdir<span style="color: #008000;">&#40;</span>sPathName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">struct</span> dirent <span style="color: #000040;">*</span>file<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> file <span style="color: #000080;">=</span> readdir<span style="color: #008000;">&#40;</span> dir <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#ifdef _DIRENT_HAVE_D_TYPE</span>
        <span style="color: #666666;">//如果是普通文件</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> file<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>d_type <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> DT_REG <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">struct</span> stat status<span style="color: #008080;">;</span>
            stat<span style="color: #008000;">&#40;</span>file<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>d_name, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>status<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%20s last modified time: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, file<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>d_name, <span style="color: #0000ff;">int</span><span style="color: #008000;">&#40;</span>status.<span style="color: #007788;">st_mtime</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #339900;">#else</span>
&nbsp;
<span style="color: #339900;">#endif</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> c, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span>v<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> ls<span style="color: #008000;">&#40;</span>v<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>ls的详细实现，可以在<a href="www.gnu.org/software/coreutils/" target="_blank">这里</a>找到。</p>
]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2008/12/24/%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84ls%e5%ae%9e%e7%8e%b0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>试用libwww</title>
		<link>http://icomes.net/2008/12/16/%e8%af%95%e7%94%a8libwww/</link>
		<comments>http://icomes.net/2008/12/16/%e8%af%95%e7%94%a8libwww/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 14:45:56 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[lib]]></category>
		<category><![CDATA[libwww]]></category>

		<guid isPermaLink="false">http://tridot.cn/?p=209</guid>
		<description><![CDATA[晚上用了一个小时试用libwww。 在ubuntu下编译遇到了不少问题，无论是编译库本身还是一些demo。 糟糕的体验，以及自从03年后这个库就没有更新过，所以决定放弃。 转向curl。]]></description>
			<content:encoded><![CDATA[<p>晚上用了一个小时试用libwww。</p>
<p>在ubuntu下编译遇到了不少问题，无论是编译库本身还是一些demo。<br />
糟糕的体验，以及自从03年后这个库就没有更新过，所以决定放弃。</p>
<p>转向curl。</p>
]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2008/12/16/%e8%af%95%e7%94%a8libwww/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

