﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-清幽静谷-最新评论</title><link>http://www.cppblog.com/zjl-1026-2001/CommentsRSS.aspx</link><description /><language>zh-cn</language><pubDate>Mon, 19 Apr 2010 00:30:24 GMT</pubDate><lastBuildDate>Mon, 19 Apr 2010 00:30:24 GMT</lastBuildDate><generator>cnblogs</generator><item><title>re: Linux下用信号量实现对共享内存的访问保护(二)</title><link>http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108778.html#108784</link><dc:creator>luckycat</dc:creator><author>luckycat</author><pubDate>Wed, 03 Mar 2010 06:41:00 GMT</pubDate><guid>http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108778.html#108784</guid><description><![CDATA[我觉共享内存的这种&quot;非显示删除自保留性&quot;是很有用的特性而不是问题，也许当时设计共享内存机制时这种特性是有意提供的.<br>设想一个简单的例子:<br>我们使用一块有访问控制保护的内存缓冲区来存储进程或线程之间共用的的数据.<br>并且我们需要保证数据的安全性，即使server倒掉，这块缓冲区里面的数据也不能丢失.<br>1. 对于多线程的情况，当一个线程core掉之后(比如因为segment fault)，线程所对应的进程将不能幸免.<br>在这种情况下，如果我们使用的是一般的&quot;内存缓冲区&quot;而不是共享内存，那么当进程退出后，<br>这块缓冲区对应的内存空间将会被系统回收重新分配给其它进程使用，缓冲区中对应的数据也就丢失了.<br>但是如果我们换用共享内存来作为线程间交换数据的缓冲区，我们就能很好的解决这个问题.<br>2. 在多进程的情况下，不但能满足上述特性，而且共享内存也是进程间数据交换的一种高效方式.<br>至于共享内存的手动删除问题，我的做法是，在生成IPC对象时，将对应的ftok生成的key值写入到一个文件中，<br>生成类似于下面的shell脚本，这样当我们需要手动删除IPC时也很方便:<br><br>ipcrm -M xxx<br>ipcrm -Q xxx<br>ipcrm -S xxx<br><br>相比于通过socket来作为进程间通信的方式，共享内存的最大不足在于&quot;不能跨机器共用&quot;.<br>after all, there is no silver bullet :)<img src ="http://www.cppblog.com/zjl-1026-2001/aggbug/108784.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zjl-1026-2001/" target="_blank">luckycat</a> 2010-03-03 14:41 <a href="http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108778.html#108784#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: Linux下用信号量实现对共享内存的访问保护(一)</title><link>http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html#108775</link><dc:creator>沙漠里的海豚</dc:creator><author>沙漠里的海豚</author><pubDate>Wed, 03 Mar 2010 05:19:00 GMT</pubDate><guid>http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html#108775</guid><description><![CDATA[呵呵，你说的没错，我上面的程序在这一点上只是用一种取巧的方式来做的，是一个demo版本，还会在后续更多的介绍中进行完善。<br><br>非常感谢你提出的建议，也欢迎你继续关注，一起交流问题，分享经验。<img src ="http://www.cppblog.com/zjl-1026-2001/aggbug/108775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zjl-1026-2001/" target="_blank">沙漠里的海豚</a> 2010-03-03 13:19 <a href="http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html#108775#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: Linux下用信号量实现对共享内存的访问保护(一)</title><link>http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html#108773</link><dc:creator>luckycat</dc:creator><author>luckycat</author><pubDate>Wed, 03 Mar 2010 05:15:00 GMT</pubDate><guid>http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html#108773</guid><description><![CDATA[你的代码中是通过判断信号量的值为0来作为对共享内存读取操作的依据，即是如下代码:<br> wait_v(semid);<br> printf(&quot;Message geted is: %s \n&quot;,shm + 1);<br> <br>但实际上这里有一个潜在的问题:<br>即如果 wait_v(semid); 成功后，在执行接下来的printf(&quot;Message geted is: %s \n&quot;,shm + 1)之前，进程被挂起.<br>那么此时 server 进程可能会重新获取这个信号量并对共享内存中的数据进行写操作(当然，你这里用server sleep的时间远大于client sleep的时间来解决这个问题)<br>当挂起的进程重新被调度投入运行后，此时printf(&quot;Message geted is: %s \n&quot;,shm + 1)的数据实际上就不是wait_v(semid)成功后共享内存中对应的数据.<br><br>我觉得对于这种典型的 &quot;provider/consumer&quot; 模型，一种更好的做法是<br>// for provider               // for consumer<br>P( write_sem );              P( read_sem );<br><br>// write operation           // read operation<br>	<br>V( read_sem );               V( write_sem );<br><br><br>当然，这里我们需要使用 write_sem 和 read_sem 两个信号量.<img src ="http://www.cppblog.com/zjl-1026-2001/aggbug/108773.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zjl-1026-2001/" target="_blank">luckycat</a> 2010-03-03 13:15 <a href="http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html#108773#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>