<?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; 锁</title>
	<atom:link href="http://icomes.net/tag/%e9%94%81/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>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%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 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%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  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%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 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%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  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>
	</channel>
</rss>

