﻿<?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++博客-quitewalk-随笔分类-操作系统</title><link>http://www.cppblog.com/quite/category/664.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 21 May 2008 20:13:54 GMT</lastBuildDate><pubDate>Wed, 21 May 2008 20:13:54 GMT</pubDate><ttl>60</ttl><item><title>关于信号量机制解决进程同步问题</title><link>http://www.cppblog.com/quite/archive/2005/12/27/2196.html</link><dc:creator>quite</dc:creator><author>quite</author><pubDate>Tue, 27 Dec 2005 14:56:00 GMT</pubDate><guid>http://www.cppblog.com/quite/archive/2005/12/27/2196.html</guid><wfw:comment>http://www.cppblog.com/quite/comments/2196.html</wfw:comment><comments>http://www.cppblog.com/quite/archive/2005/12/27/2196.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/quite/comments/commentRss/2196.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/quite/services/trackbacks/2196.html</trackback:ping><description><![CDATA[<P>一、关于信号量机制解决进程同步问题：<BR>1、整型信号量；<BR>2、记录型信号量：采用记录型数据结构（资源数目，在等待该资源的进程链表）；能够实现让权等待（if(S.value&lt;0)&nbsp;&nbsp; block(S.L)）;<BR>3、AND 型信号量：一次申请若干种资源，每种资源仅申请一个（从能够申请的资源种类上时行扩充）；<BR>4、信号量集：一次申请若干种资源，每种资源可申请若干个，并且可控制资源分配下限。</P>
<P>二、用记录型信号量解决生产者--消费者问题</P>
<P>semaphore mutem=1;<BR>semaphore empty=n;<BR>semaphore full=0;</P>
<P>item array[n];</P>
<P>int in=0;<BR>int out=0;</P>
<P>producer()<BR>{<BR>&nbsp;while(ture)<BR>&nbsp;{<BR>&nbsp;&nbsp;produce an item in next_product;</P>
<P>&nbsp;&nbsp;waite(empty);<BR>&nbsp;&nbsp;waite(mutex);</P>
<P>&nbsp;&nbsp;array[in]=next_product;<BR>&nbsp;&nbsp;in=(in+1)%n;</P>
<P>&nbsp;&nbsp;signal(mutex);<BR>&nbsp;&nbsp;signal(full);<BR>&nbsp;}<BR>}</P>
<P><BR>consumer()<BR>{<BR>&nbsp;while(ture)<BR>&nbsp;{<BR>&nbsp;&nbsp;waite(full);<BR>&nbsp;&nbsp;waite(mutex);</P>
<P>&nbsp;&nbsp;mext_consumer=array[out];<BR>&nbsp;&nbsp;out=(out+1)%n;</P>
<P>&nbsp;&nbsp;signal(mutex);<BR>&nbsp;&nbsp;signal(empty);<BR>&nbsp;}<BR>}</P>
<P>&nbsp;</P>
<P><BR>三、用AND 型信号量解决生产者--消费者问题</P>
<P>semaphore mutex=1;<BR>semaphore empty=n;<BR>semaphore full=0;</P>
<P>item array[n];</P>
<P>int in=0;<BR>int out=0;</P>
<P>producer()<BR>{<BR>&nbsp;while(ture)<BR>&nbsp;{<BR>&nbsp;&nbsp;produce an item in next_product;</P>
<P>&nbsp;&nbsp;Swaite(empty,mutex);</P>
<P>&nbsp;&nbsp;array[in]=next_product;<BR>&nbsp;&nbsp;in=(in+1)%n;</P>
<P>&nbsp;&nbsp;Ssignal(mutex,full);<BR>&nbsp;}<BR>}</P>
<P>consumer()<BR>{<BR>&nbsp;while(ture)<BR>&nbsp;{<BR>&nbsp;&nbsp;Swaite(full,mutex);</P>
<P>&nbsp;&nbsp;next_consumer=array[out];<BR>&nbsp;&nbsp;out=(out+1)%n;</P>
<P>&nbsp;&nbsp;Ssignal(mutex,empty);</P>
<P>&nbsp;&nbsp;consum the product in next_consumer;</P>
<P>&nbsp;}</P>
<P>}</P>
<P>四、用记录型的信号量和信号量集结合使用来解决生产者--消费者问题（这里，每个生产者一次生产 m 个产品放入缓冲区，每个消费者一次取一个产品）<BR>&nbsp;&nbsp; </P>
<P>semaphore mutex=1;<BR>semaphore empty=n;<BR>semaphore full=0;</P>
<P>item array[n];</P>
<P>int in=0;<BR>int out=0;<BR>int m=3;<BR>int next_product[m];</P>
<P>producer()<BR>{<BR>&nbsp;while(ture)<BR>&nbsp;{<BR>&nbsp;&nbsp;produce_m_products();</P>
<P>&nbsp;&nbsp;Swaite(empty,m,m;&nbsp; mutex);</P>
<P>&nbsp;&nbsp;put_m_products();</P>
<P>&nbsp;&nbsp;Ssignal(full,m;&nbsp; mutex);</P>
<P>&nbsp;}<BR>}</P>
<P>consumer()<BR>{<BR>&nbsp;while(ture)<BR>&nbsp;{<BR>&nbsp;&nbsp;Swaite(full,mutex);</P>
<P>&nbsp;&nbsp;next_consumer=array[out];<BR>&nbsp;&nbsp;out=(out+1)%n;</P>
<P>&nbsp;&nbsp;Signal(mutex,empty);</P>
<P>&nbsp;&nbsp;consuming....;<BR>&nbsp;}<BR>}</P>
<P>produce_m_products()<BR>{<BR>&nbsp;int i;<BR>&nbsp;<BR>&nbsp;while(i=0;i&lt;m;i++)<BR>&nbsp;&nbsp;produce an item in next_producct[i];</P>
<P>}</P>
<P>put_m_product()<BR>{<BR>&nbsp;int i;</P>
<P>&nbsp;for(i=0;i&lt;m;i++)<BR>&nbsp;{<BR>&nbsp;&nbsp;array[in]=next_product[i];<BR>&nbsp;&nbsp;in=(in+1)%n;<BR>&nbsp;}<BR>}</P><img src ="http://www.cppblog.com/quite/aggbug/2196.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/quite/" target="_blank">quite</a> 2005-12-27 22:56 <a href="http://www.cppblog.com/quite/archive/2005/12/27/2196.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>进程通信</title><link>http://www.cppblog.com/quite/archive/2005/12/27/2197.html</link><dc:creator>quite</dc:creator><author>quite</author><pubDate>Tue, 27 Dec 2005 14:56:00 GMT</pubDate><guid>http://www.cppblog.com/quite/archive/2005/12/27/2197.html</guid><wfw:comment>http://www.cppblog.com/quite/comments/2197.html</wfw:comment><comments>http://www.cppblog.com/quite/archive/2005/12/27/2197.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/quite/comments/commentRss/2197.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/quite/services/trackbacks/2197.html</trackback:ping><description><![CDATA[<P>一、进程通信<BR>1&gt; 共享存储器系统<BR>&nbsp; *共享数据结构通信，如生产者--消费考问题中的有界缓冲区；<BR>&nbsp; *共享存储区：通信的进程，在存储区中划出一块<BR>2&gt;消息传递系统<BR>&nbsp; *直接通信方式：利用操作系统提供的通信命令&nbsp; send,receive;<BR>&nbsp; *间接通信方式（信箱通信）<BR>&nbsp;&nbsp; a.私用信箱；<BR>&nbsp;&nbsp; b.公用信箱；<BR>&nbsp;&nbsp; c.共享信箱<BR>3&gt;管道通信</P>
<P>二。进程同步方式<BR>1、紧密同步：发送进程阻塞，接收进程也阻塞，（无缓冲，平时阻塞，直到有消息传递时）；<BR>2、发送进程不阻塞，接收进程阻塞：用的最广，客户机/服务器，相当于发送进程和接收进程；<BR>3、发送进程和接收进程都不阻塞：较常见，以消息队列联系的两进程。</P>
<P>&nbsp;</P><img src ="http://www.cppblog.com/quite/aggbug/2197.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/quite/" target="_blank">quite</a> 2005-12-27 22:56 <a href="http://www.cppblog.com/quite/archive/2005/12/27/2197.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于进程控制块</title><link>http://www.cppblog.com/quite/archive/2005/12/27/2194.html</link><dc:creator>quite</dc:creator><author>quite</author><pubDate>Tue, 27 Dec 2005 14:53:00 GMT</pubDate><guid>http://www.cppblog.com/quite/archive/2005/12/27/2194.html</guid><wfw:comment>http://www.cppblog.com/quite/comments/2194.html</wfw:comment><comments>http://www.cppblog.com/quite/archive/2005/12/27/2194.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/quite/comments/commentRss/2194.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/quite/services/trackbacks/2194.html</trackback:ping><description><![CDATA[<P>关于进程控制块</P>
<P>一、进程控制块的组织方式：<BR>1、链接方式<BR>2、索引方式</P>
<P>二、进程控制块中的信息：<BR>1、进程标志：分为内部标志（进程号）和外部标志（用户在程序中使用）<BR>2、进程控制信息：<BR>&nbsp; a.程序和数据的地址；<BR>&nbsp; b.进程同步和通信机制；<BR>&nbsp; c.资源清单；<BR>&nbsp; d.下一个进程块的链接指针。<BR>3、进程调度信息：<BR>&nbsp; a.进程状态；<BR>&nbsp; b.进程优先级；<BR>&nbsp; c.用于进程调度的其它信息；<BR>&nbsp; d.事件（阻塞原因）。<BR>4、处理机状态：<BR>&nbsp; a.通用寄存器；<BR>&nbsp; b.指令寄存器；<BR>&nbsp; c.程序状态字（PSW）；<BR>&nbsp; d.用户指针栈（系统栈，存放进程和系统调用参数在址）；<BR>&nbsp; </P><img src ="http://www.cppblog.com/quite/aggbug/2194.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/quite/" target="_blank">quite</a> 2005-12-27 22:53 <a href="http://www.cppblog.com/quite/archive/2005/12/27/2194.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于设备分配</title><link>http://www.cppblog.com/quite/archive/2005/12/25/2074.html</link><dc:creator>quite</dc:creator><author>quite</author><pubDate>Sun, 25 Dec 2005 05:18:00 GMT</pubDate><guid>http://www.cppblog.com/quite/archive/2005/12/25/2074.html</guid><wfw:comment>http://www.cppblog.com/quite/comments/2074.html</wfw:comment><comments>http://www.cppblog.com/quite/archive/2005/12/25/2074.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/quite/comments/commentRss/2074.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/quite/services/trackbacks/2074.html</trackback:ping><description><![CDATA[<P>关于设备分配用到的数据结构：<BR><BR>1。系统设备表（SDT）：系统维护的数据结构，对于单用户的系统，仅有一张。其中共中央办公厅，每个设备占一个表目，包括设备类型、设备标识符（逻辑设备名）、设备控制表及设备驱动程序入口等。<BR>2。通道控制表：系统中所有通道的记录。<BR>3。控制器控制表：系统中所有控制器的记录。<BR>4。设备控制表：系统中所有设备的记录。<BR></P>
<P>设备分配过程：<BR><BR>系统根据I/O请求分配设备时，首先查找系统设备表（系统设备表中记录了系统中全部设备的情况），从中找出该设备的设备控制器表（设备控制器表设备的直接控制者），分配设备，然后系统再到设备控制表中找上一级的控制器（控制器控制表是设备控制器的直接控制者），分配控制器后，再从控制器中找再上一级的通道（通道是设备控制器的直接控制者）。只有在设备、控制器、通道三者都分配成功时，本次设备分配才算成功。</P><img src ="http://www.cppblog.com/quite/aggbug/2074.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/quite/" target="_blank">quite</a> 2005-12-25 13:18 <a href="http://www.cppblog.com/quite/archive/2005/12/25/2074.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于缓冲</title><link>http://www.cppblog.com/quite/archive/2005/12/25/2073.html</link><dc:creator>quite</dc:creator><author>quite</author><pubDate>Sun, 25 Dec 2005 05:16:00 GMT</pubDate><guid>http://www.cppblog.com/quite/archive/2005/12/25/2073.html</guid><wfw:comment>http://www.cppblog.com/quite/comments/2073.html</wfw:comment><comments>http://www.cppblog.com/quite/archive/2005/12/25/2073.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/quite/comments/commentRss/2073.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/quite/services/trackbacks/2073.html</trackback:ping><description><![CDATA[关于缓冲：<BR><BR>1。单缓冲：两个进程，一个缓冲区，任一时刻只能实现单方向的数据传输。<BR><BR>2。双缓冲（缓冲对换）：两台机器，每台都设置两个缓冲区，一个用作发送缓冲区，一个用作接收缓冲区。<BR><BR>3。循环缓冲（多缓冲）：仅适用于特定的I/O进程和计算进程，属于专用缓冲，当系统较大时，将有许多这样的循环缓冲。<BR>4。公用缓冲池：目前广泛流行，系统中所有进程共同使用。<img src ="http://www.cppblog.com/quite/aggbug/2073.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/quite/" target="_blank">quite</a> 2005-12-25 13:16 <a href="http://www.cppblog.com/quite/archive/2005/12/25/2073.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于I/O控制方式</title><link>http://www.cppblog.com/quite/archive/2005/12/25/2072.html</link><dc:creator>quite</dc:creator><author>quite</author><pubDate>Sun, 25 Dec 2005 05:15:00 GMT</pubDate><guid>http://www.cppblog.com/quite/archive/2005/12/25/2072.html</guid><wfw:comment>http://www.cppblog.com/quite/comments/2072.html</wfw:comment><comments>http://www.cppblog.com/quite/archive/2005/12/25/2072.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/quite/comments/commentRss/2072.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/quite/services/trackbacks/2072.html</trackback:ping><description><![CDATA[<P>关于I/O控制方式：<BR>1。程序I/O方式：<BR>&nbsp;&nbsp;&nbsp; 由于CPU中无中断机构，I/O设备无法向CPU报告它已完成了一个字符的输入操作，导致CPU不停地检查它要取的数据是否已达寄存器。<BR>2。中断驱动方式：<BR>&nbsp;&nbsp;&nbsp; 设备控制器负责向CPU汇报数据是否已到达（以中断方式）；以字（节）为单位进行I/O的；CPU以字（节）为单位进行干预。<BR>3。DMA方式：<BR>&nbsp;&nbsp;&nbsp; 引入设备DMA控制器，在其控制下，以数据块为单位，将数据从设备直接送入内存或相反；实质上还是以字（节）为单位进行传输，不过这种传输可以连续进行而已，形成数据块。</P>
<P>&nbsp;</P><img src ="http://www.cppblog.com/quite/aggbug/2072.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/quite/" target="_blank">quite</a> 2005-12-25 13:15 <a href="http://www.cppblog.com/quite/archive/2005/12/25/2072.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>