<?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>Power by Result Search &#187; vxworks</title>
	<atom:link href="http://www.result-search.com/sty/category/technology/linux/vxworks-linux/feed" rel="self" type="application/rss+xml" />
	<link>http://www.result-search.com/sty</link>
	<description>Just another weblog</description>
	<lastBuildDate>Wed, 23 Jun 2010 03:12:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>VxWork介绍及编程</title>
		<link>http://www.result-search.com/sty/2009/04/26/vxwork-getting-start.html</link>
		<comments>http://www.result-search.com/sty/2009/04/26/vxwork-getting-start.html#comments</comments>
		<pubDate>Sun, 26 Apr 2009 07:45:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[vxworks]]></category>

		<guid isPermaLink="false">http://www.yaaahaaa.com/?p=108</guid>
		<description><![CDATA[from: http://www.embhelp.com/drew/mypage/VxWorks.htm#begin
大3的时候狂玩了一段时间的嵌入式，这次看到这些文章，再好好复习一下吧，。。。。
孔子曰：温故而知新！！
我知道为啥孔子这么说了，因为孔子看书看很多，但是没有深入理解，第二次看类似的内容的时候，学的也就更深入了！
温故而知新，其实就是这个道理。
一.嵌入式操作系统VxWorks简介
VxWorks操作系统是美国WindRiver公司于1983年设计开发的一种嵌入式实时操作系统（RTOS），是嵌入式开发环境的关键组成部分。良好的持续发展能力、高性能的内核以及友好的用户开发环境，在嵌入式实时操作系统领域占据一席之地。它以其良好的可靠性和卓越的实时性被广泛地应用在通信、军事、航空、航天等高精尖技术及实时性要求极高的领域中，如卫星通讯、军事演习、弹道制导、飞机导航等。在美国的 F-16、FA-18 战斗机、B-2 隐形轰炸机和爱国者导弹上，甚至连1997年4月在火星表面登陆的火星探测器上也使用到了VxWorks。
WindRiver公司网址
实时操作系统和分时操作系统的区别

 从操作系统能否满足实时性要求来区分，可把操作系统分成分时操作系统和实时操作系统。
 分时操作系统按照相等的时间片调度进程轮流运行，分时操作系统由调度程序自动计算进程的优先级，而不是由用户控制进程的优先级。这样的系统无法实时响应外部异步事件。
 实时操作系统能够在限定的时间内执行完所规定的功能，并能在限定的时间内对外部的异步事件作出响应。 分时系统主要应用于科学计算和一般实时性要求不高的场合。实时性系统主要应用于过程控制、数据采集、通信、多媒体信息处理等对时间敏感的场合。
 
VxWorks的特点


可靠性



 操作系统的用户希望在一个工作稳定，可以信赖的环境中工作，所以操作系统的可靠性是用户首先要考虑的问题。而稳定、可靠一直是VxWorks的一个突出优点。自从对中国的销售解禁以来，VxWorks以其良好的可靠性在中国赢得了越来越多的用户。 
 


实时性



 实时性是指能够在限定时间内执行完规定的功能并对外部的异步事件作出响应的能力。实时性的强弱是以完成规定功能和作出响应时间的长短来衡量的。


 VxWorks 的实时性做得非常好，其系统本身的开销很小，进程调度、进程间通信、中断处理等系统公用程序精练而有效，它们造成的延迟很短。VxWorks 提供的多任务机制中对任务的控制采用了优先级抢占（Preemptive Priority Scheduling）和轮转调度（Round-Robin                Scheduling）机制，也充分保证了可靠的实时性，使同样的硬件配置能满足更强的实时性要求，为应用的开发留下更大的余地。 
 


可裁减性



用户在使用操作系统时，并不是操作系统中的每一个部件都要用到。例如图形显示、文件系统以及一些设备驱动在某些嵌入系统中往往并不使用。
 VxWorks 由一个体积很小的内核及一些可以根据需要进行定制的系统模块组成。VxWorks 内核最小为 8kB，即便加上其它必要模块，所占用的空间也很小，且不失其实时、多任务的系统特征。由于它的高度灵活性，用户可以很容易地对这一操作系统进行定制或作适当开发，来满足自己的实际应用需要。 
 
对一个实时内核的要求   

一个实时操作系统内核需满足许多特定的实时环境所提出的基本要求，这些包括：
 

多任务：由于真实世界的事件的异步性，能够运行许多并发进程或任务是很重要的。多任务提供了一个较好的对真实世界的匹配，因为它允许对应于许多外部事件的多线程执行。系统内核分配CPU给这些任务来获得并发性。
 
抢占调度：真实世界的事件具有继承的优先级，在分配CPU的时候要注意到这些优先级。基于优先级的抢占调度，任务都被指定了优先级，在能够执行的任务（没有被挂起或正在等待资源）中，优先级最高的任务被分配CPU资源。换句话说，当一个高优先级的任务变为可执行态，它会立即抢占当前正在运行的较低优先级的任务。 
 
任务间的通讯与同步：在一个实时系统中，可能有许多任务作为一个应用的一部分执行。系统必须提供这些任务间的快速且功能强大的通信机制。内核也要提供为了有效地共享不可抢占的资源或临界区所需的同步机制。
 
任务与中断之间的通信：尽管真实世界的事件通常作为中断方式到来，但为了提供有效的排队、优先化和减少中断延时，我们通常希望在任务级处理相应的工作。所以需要杂任务级和中断级之间存在通信。
 


二.系统编程方法
 了解系统编程对程序员来说尤为重要。根据Drew个人的经验基本上操作系统编程都是类似的，认真读懂一种，就很容易的理解另一种。
下面是Drew翻的VxWorks programmer  [...]]]></description>
			<content:encoded><![CDATA[<p>from: http://www.embhelp.com/drew/mypage/VxWorks.htm#begin</p>
<p>大3的时候狂玩了一段时间的嵌入式，这次看到这些文章，再好好复习一下吧，。。。。</p>
<p>孔子曰：温故而知新！！</p>
<p>我知道为啥孔子这么说了，因为孔子看书看很多，但是没有深入理解，第二次看类似的内容的时候，学的也就更深入了！</p>
<p>温故而知新，其实就是这个道理。</p>
<p><strong><span style="font-family: 宋体; font-size: large;">一.嵌入式操作系统VxWorks简介</span><a name="begin"></a></strong></p>
<p style="line-height: 150%; margin-left: 20px; margin-right: 20px;"><span style="font-size: small;"><strong>VxWorks</strong>操作系统是美国WindRiver公司于1983年设计开发的一种嵌入式实时操作系统（RTOS），是嵌入式开发环境的关键组成部分。良好的持续发展能力、高性能的内核以及友好的用户开发环境，在嵌入式实时操作系统领域占据一席之地。</span><span style="font-family: System;">它以其良好的可靠性和卓越的实时性被广泛地应用在通信、军事、航空、航天等高精尖技术及实时性要求极高的领域中，如卫星通讯、军事演习、弹道制导、飞机导航等。在美国的</span><span style="font-family: 宋体;"> F-16</span><span style="font-family: System;">、</span><span style="font-family: 宋体;">FA-18 </span><span style="font-family: System;">战斗机、</span><span style="font-family: 宋体;">B-2 </span><span style="font-family: System;">隐形轰炸机和爱国者导弹上，甚至连</span><span style="font-family: 宋体;">1997</span><span style="font-family: System;">年</span><span style="font-family: 宋体;">4</span><span style="font-family: System;">月在火星表面登陆的火星探测器上也使用到了</span><span style="font-family: 宋体;">VxWorks</span><span style="font-family: System;">。</span></p>
<blockquote><p><a href="http://www.windriver.com/" target="_blank">WindRiver公司网址</a></p></blockquote>
<p><span style="font-family: 宋体; font-size: small;"><strong>实时操作系统和分时操作系统的区别</strong></span></p>
<blockquote>
<p style="line-height: 150%;"><span style="font-family: System;"> 从操作系统能否满足实时性要求来区分，可把操作系统分成分时操作系统和实时操作系统。</span></p>
<p style="line-height: 150%;"><span style="font-family: System;"> 分时操作系统按照相等的时间片调度进程轮流运行，分时操作系统由调度程序自动计算进程的优先级，而不是由用户控制进程的优先级。这样的系统无法实时响应外部异步事件。</span></p>
<p style="line-height: 150%;"><span style="font-family: System;"> 实时操作系统能够在限定的时间内执行完所规定的功能，并能在限定的时间内对外部的异步事件作出响应。</span> <span style="font-family: System;">分时系统主要应用于科学计算和一般实时性要求不高的场合。实时性系统主要应用于过程控制、数据采集、通信、多媒体信息处理等对时间敏感的场合。</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: medium;"><strong>VxWorks的特点</strong></span></p>
<ul>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: small;"><strong>可靠性</strong></span></p>
</li>
</ul>
<blockquote>
<p style="line-height: 150%;"><span style="font-family: System;"> 操作系统的用户希望在一个工作稳定，可以信赖的环境中工作，所以操作系统的可靠性是用户首先要考虑的问题。而稳定、可靠一直是VxWorks的一个突出优点。自从对中国的销售解禁以来，</span><span style="font-family: 宋体;">VxWorks</span><span style="font-family: System;">以其良好的可靠性在中国赢得了越来越多的用户。</span><span style="font-family: 宋体;"> </span></p>
<p><span style="font-family: 宋体;"> </span></p></blockquote>
<ul>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: small;"><strong>实时性</strong></span></p>
</li>
</ul>
<blockquote>
<p style="line-height: 150%;"><span style="font-family: System;"> 实时性是指能够在限定时间内执行完规定的功能并对外部的异步事件作出响应的能力。实时性的强弱是以完成规定功能和作出响应时间的长短来衡量的。</span></p>
</blockquote>
<blockquote>
<p style="line-height: 150%;"><span style="font-family: 宋体;"> VxWorks </span><span style="font-family: System;">的实时性做得非常好，其系统本身的开销很小，进程调度、进程间通信、中断处理等系统公用程序精练而有效，它们造成的延迟很短。</span><span style="font-family: 宋体;">VxWorks </span><span style="font-family: System;">提供的多任务机制中对任务的控制采用了优先级抢占（</span><span style="font-family: 宋体;">Preemptive Priority Scheduling</span><span style="font-family: System;">）和轮转调度（</span><span style="font-family: 宋体;">Round-Robin                Scheduling</span><span style="font-family: System;">）机制，也充分保证了可靠的实时性，使同样的硬件配置能满足更强的实时性要求，为应用的开发留下更大的余地。</span><span style="font-family: 宋体;"> </span></p>
<p><span style="font-family: 宋体;"> </span></p></blockquote>
<ul>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: small;"><strong>可裁减性</strong></span></p>
</li>
</ul>
<blockquote>
<p style="line-height: 150%;">用户在使用操作系统时，并不是操作系统中的每一个部件都要用到。例如图形显示、文件系统以及一些设备驱动在某些嵌入系统中往往并不使用。</p>
<p style="line-height: 150%;"><span style="font-family: 宋体;"> VxWorks </span><span style="font-family: System;">由一个体积很小的内核及一些可以根据需要进行定制的系统模块组成。</span><span style="font-family: 宋体;">VxWorks </span><span style="font-family: System;">内核最小为</span><span style="font-family: 宋体;"> 8kB</span><span style="font-family: System;">，即便加上其它必要模块，所占用的空间也很小，且不失其实时、多任务的系统特征。由于它的高度灵活性，用户可以很容易地对这一操作系统进行定制或作适当开发，来满足自己的实际应用需要。</span><span style="font-family: 宋体;"> </span></p>
<p><span style="font-family: 宋体;"> </span></p></blockquote>
<p style="line-height: 150%;"><strong><span style="font-family: 宋体; font-size: small;">对一个实时内核的要求</span><span style="font-family: 宋体;"><big><big> </big></big> </span> </strong></p>
<blockquote>
<p style="line-height: 150%;"><span style="font-family: System;">一个实时操作系统内核需满足许多特定的实时环境所提出的基本要求，这些包括：</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<blockquote>
<p style="line-height: 150%;"><strong><span style="font-family: 宋体; font-size: small;">多任务</span><span style="font-family: System;">：</span></strong><span style="font-family: System;">由于真实世界的事件的异步性，能够运行许多并发进程或任务是很重要的。多任务提供了一个较好的对真实世界的匹配，因为它允许对应于许多外部事件的多线程执行。系统内核分配<span style="font-family: 宋体;">CPU</span>给这些任务来获得并发性。</span></p>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: small;"><strong>抢占调度</strong></span><span style="font-family: System;">：真实世界的事件具有继承的优先级，在分配<span style="font-family: 宋体;">CPU</span>的时候要注意到这些优先级。基于优先级的抢占调度，任务都被指定了优先级，在能够执行的任务（没有被挂起或正在等待资源）中，优先级最高的任务被分配<span style="font-family: 宋体;">CPU</span>资源。换句话说，当一个高优先级的任务变为可执行态，它会立即抢占当前正在运行的较低优先级的任务。<span style="font-family: 宋体;"> </span></span></p>
<p><span style="font-family: System;"><span style="font-family: 宋体;"> </span></span></p>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: small;"><strong>任务间的通讯与同步</strong></span><span style="font-family: System;">：在一个实时系统中，可能有许多任务作为一个应用的一部分执行。系统必须提供这些任务间的快速且功能强大的通信机制。内核也要提供为了有效地共享不可抢占的资源或临界区所需的同步机制。</span></p>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: small;"><strong>任务与中断之间的通信</strong></span><span style="font-family: System;">：尽管真实世界的事件通常作为中断方式到来，但为了提供有效的排队、优先化和减少中断延时，我们通常希望在任务级处理相应的工作。所以需要杂任务级和中断级之间存在通信。</span></p>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;" align="right">
</blockquote>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: large;"><strong>二.系统编程方法</strong></span></p>
<p style="line-height: 150%;"><span style="font-family: 宋体;"> 了解系统编程对程序员来说尤为重要。根据Drew个人的经验基本上操作系统编程都是类似的，认真读懂一种，就很容易的理解另一种。</span></p>
<p style="line-height: 150%;"><span style="font-family: 宋体;">下面是Drew翻的VxWorks programmer  		guide中的基本内容，有一些内容是Drew个人的理解。理解这些内容对实时操作编程非常重要。</span></p>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">实时系统主要包括</span><span style="font-family: 宋体;">:</span><span style="font-family: System;">多任务调度（采用优先级抢占方式），任务间的同步和进程间通信机制</span><span style="font-family: 宋体;">。</span></p>
<p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">一个多任务环境允许实时应用程序以一套独立任务的方式构筑，每个任务拥有独立的执行线程和它自己的一套系统资源。进程间通信机制使得这些任务的行为同步、协调。</span> <span style="font-family: System;">wind使用中断驱动和优先级的方式。它缩短了上下文转换的时间开销和中断的时延。在</span> <span style="font-family: System;">VxWorks                中，任何例程都可以被启动为一个单独的任务，拥有它自己的上下文和堆栈。还有一些其它的任务机制可以使任务挂起、继续、删除、延时或改变优先级。</span></p>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">另一个重要内容是:硬件中断处理。硬件产生中断,统治系统调用相应的中断历程(ISR),位是系统得到尽快的响应,ISR在它自己独立的上下文和堆栈中运行.它的优先级高于任何任务优先级.</span></p>
<p><span style="font-family: System;"> </span></p>
<h4 class="EntityTitle"><span style="font-family: System;"><a name="93407"><span class="sans" style="font-family: Helvetica,sans-serif;">Task  State Transitions</span></a></span></h4>
<dl class="margin"><span style="font-family: System;"> </span></p>
<div class="Anchor"><span style="font-family: System;"> <a name="93492"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basica4.gif" border="0" alt="" width="310" height="265" /> </span></div>
</dl>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;"> </span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;"> </span></p>
<blockquote>
<p style="line-height: 150%;"><strong><span style="font-family: 宋体; font-size: medium;">中断延迟</span><span style="font-family: System;">(Interrupt Latency) </span> </strong><span style="font-family: System;">中断延迟是指从硬件中断发生到开始执行中断处理程序第一条指令之间的这段时间。 </span></p>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><strong><span style="font-family: 宋体; font-size: medium;">优先级驱动</span><span style="font-family: System;">(Priority-Driven) </span> </strong><span style="font-family: System;">优先级驱动是指多任务系统中，当前运行任务总是具有最高优先级的就绪任务。 </span></p>
</blockquote>
<ul>
<li>
<p style="line-height: 150%;"><strong><span style="font-size: large;">多任务调度</span></strong></p>
</li>
</ul>
<blockquote>
<p style="line-height: 150%;">两种方式: 优先抢占和轮转调度(Preemptive Priority,Round-Robin Scheduling).</p>
</blockquote>
<p><strong> </strong></p>
<blockquote><p><strong> </strong></p>
<p style="line-height: 150%;"><strong><span>优先抢占</span></strong><span style="font-family: System;">(Preemptive                Priority): 每一个任务都有一个优先级,系统核心保证优先级最高的任务运行于CPU.如果有任务优先级高于当前的任务优先级,系统立刻保存当前任务的上下文,切换到优先级高的上下文. </span></p>
<h4 class="EntityTitle"><a name="83848"><span class="sans" style="font-family: Helvetica,sans-serif;">Priority  Preemption</span></a></h4>
<dl class="margin">
<div class="Anchor"><a name="83845"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basic10.gif" border="0" alt="" width="360" height="141" /></div>
</dl>
<p style="line-height: 150%;">
<p style="line-height: 150%;">
</blockquote>
<blockquote>
<blockquote><p><strong> </strong></p>
<p style="line-height: 150%;"><strong><span>抢占(Preemptive):</span><span style="font-family: System;"> </span></strong><span>抢占是指当系统处于核心态运行时,                  允许任务的重新调度。换句话说就是指正在执行的任务可以被打断，让另一个任务运行。抢占提高了应用对异步事件的响应性能力。操作系统内核可抢占，并不是说任务调度在任何时候都可以发生。例如当一个任务正在通过一个系统调用访问共享数据时，重新调度和中断都被禁止.</span></p>
</blockquote>
<blockquote>
<p style="line-height: 150%;"><strong><span>任务上下文(Task Context):</span></strong><strong><span style="font-family: System;"> </span></strong><span>任务上下文是指任务运行的环境。例如，针对x86的CPU，任务上下文可包括程序计数器、堆栈指针、通用寄存器的内容.</span></p>
</blockquote>
<blockquote>
<p style="line-height: 150%;"><span><strong>上下文切换（Context Switching）:</strong> 多任务系统中，上下文切换是指CPU的控制权由运行任务转移到另外一个就绪任务时所发生的事件，当前运行任务转为就绪（或者挂起、删除）状态，另一个被选 定的就绪任务成为当前任务。上下文切换包括保存当前任务的运行环境，恢复将要运行任务的运行环境。上下文的内容依赖于具体的CPU.</span></p>
<p style="line-height: 150%;">
<p style="line-height: 150%;"><span><strong><big>轮转调度(</big>Round-Robin Scheduling):</strong>使所有相同优先级,状态为ready的任务公平分享CPU(分配一定的时间间隔,使个任务轮流享有CPU).</span></p>
<h4 class="EntityTitle"><a name="83915"><span class="sans" style="font-family: Helvetica,sans-serif;">Round-Robin      Scheduling</span></a></h4>
<dl class="margin">
<div class="Anchor" style="width: 423px; height: 137px;"><a name="83912"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basic13.gif" border="0" alt="" width="360" height="137" /></div>
</dl>
<p style="line-height: 150%;">
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">系统由256个优先级,从0到255,0为最高,255为最低.                任务在被创建时设定了优先级.也可用<big><strong><em>taskPrioritySet</em> ( )</strong></big> 来改变任务优先级.</span></p>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">任务的主要状态: READY,PEND,DELAY,SUSPEND&#8230;</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>ready--------&gt;pended -----------</strong><strong><em>semTake</em>(　)/<em>msgQReceive</em>(　)-</strong>其他任务</span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>ready--------&gt;delayed-----------</strong><strong><em>taskDelay</em>(　)</strong></span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>ready--------&gt;suspended---------</strong><strong><em>taskSuspend</em>(　)</strong></span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>pended-------&gt;ready-------------</strong><strong><em>semaGive</em>(　)/<em>msgQSend</em>(　)-</strong>其他任务</span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>pended-------&gt;suspended---------</strong><strong><em>taskSuspend</em>(　)</strong></span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>delayed------&gt;ready-------------</strong><strong><em>expired delay</em></strong></span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>delayed------&gt;suspended---------</strong><strong><em>taskSuspend</em>(　)</strong></span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>suspended----&gt;ready-------------</strong><strong><em>taskResume</em>(　)/<em>taskActivate</em>(　)</strong></span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span class="Code"><span><strong>suspended----&gt;pended------------</strong><strong><em>taskResume</em>(　)</strong></span></span></big></pre>
</blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><span><span class="Code"><strong>suspended----&gt;delayed-----------</strong><strong><em>taskResume</em>(　)</strong></span></span></big></pre>
<p class="Code" style="line-height: 150%;">
</blockquote>
<p><span style="font-family: System;"><strong></strong></span></p>
<blockquote><p><span style="font-family: System;"><strong> </strong></span></p>
<p style="line-height: 150%;"><span style="font-family: System;"><strong>轮转调度</strong></span> <span style="font-family: System;">(Round-Robin):                    轮转调度可以扩充到优先抢占方式中,当多个任务优先级相同的情况下,轮转调度算法使任务按平等的时间片运行于CPU,共享CPU.避免一个任务长时间占用CPU,而导致其他任务不能运行.可以用</span> <span style="font-family: System;"><strong><em>kernelTimeSlice</em>(　)</strong> 来定义时间长度.</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"><strong><em> </em></strong></span></p>
<p style="line-height: 150%;"><span style="font-family: System;"><strong><em>taskLock</em> ( )</strong>和</span> <span style="font-family: System;"><strong><em>taskUnlock</em> ( )</strong> 用来取消优先抢占方式</span> <span style="font-family: System;">和恢复优先抢占方式.</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">注意: 一个任务可以调用<strong><em>taskDelete</em> ( )</strong> 删除另一个任务,但是如果一个当前正在运行的任务被删除后,该任务的内存没有释放,而其他任务不知道,依然在等待,结果导致系统stop.用</span> <span style="font-family: System;"><strong><em>taskSafe</em> ( )</strong> 和</span> <span style="font-family: System;"><strong><em>taskUnsafe</em> ( )</strong> 来保证正在运行的任务不被删除.</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">用法如下:</span></p>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><strong><span style="font-family: Courier New;">taskSafe ();<br />
semTake (semId, WAIT_FOREVER);<br />
/* Block until semaphore available */<br />
. .</span><span style="font-family: System;"> </span><span style="font-family: Courier New;">. .</span><span style="font-family: System;"> </span><span style="font-family: Courier New;"><em>critical region </em>.<br />
semGive (semId);</span><span style="font-family: System;"> </span><span style="font-family: Courier New;">semGive                  (semId);</span><span style="font-family: System;"><br />
</span><span style="font-family: Courier New;">/* Release semaphore */<br />
<a name="BM84245">taskUnsafe ();</a></span></strong></p>
<p><strong> </strong></p>
<p style="line-height: 150%;"><strong> </strong></p>
<p><strong> </strong></p></blockquote>
<p><strong> </strong></p>
<ul>
<li>
<p style="line-height: 150%;"><big><big><strong><span>任务间的同步和进程间协调</span></strong> </big></big></p>
</li>
</ul>
<p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">信号量作为任务间同步和互斥的机制。在</span> <span style="font-family: System;">wind                核中有几种类型的信号量，它们分别针对不同的应用需求：二进制信号量、计数信号量、互斥信号量和</span> <span style="font-family: System;">POSIX                信号量。所有的这些信号量是快速和高效的，它们除了被应用在开发设计过程中外，还被广泛地应用在VxWorks                高层应用系统中。对于进程间通信，wind                核也提供了诸如消息队列、管道、套接字和信号等机制。</span></p>
</blockquote>
<p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">任务间的同步和进程间协调的几种方式:</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<ol>
<li>
<ol><span style="font-family: System;"></p>
<li>
<p style="line-height: 150%;">内存共享(Shared Memory),对简单的数据共享而言.</p>
</li>
<li>
<p style="line-height: 150%;">信号量(Semaphore),基本的互斥和同步.</p>
</li>
<li>
<p style="line-height: 150%;">消息队列(Message queues)和管道(Pipe),单个CPU中,任务间的信息传递.</p>
</li>
<li>
<p style="line-height: 150%;">套结字(Socket)和远程调用(Remote procedure calls),相对于网络任务间的通信.</p>
</li>
<li>
<p style="line-height: 150%;">信号(Signals),出错处理(Exception handling).</p>
</li>
<p></span></ol>
</li>
<p><span style="font-family: System;"> </span><span style="font-family: System;"> </span></ol>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;">
<p style="line-height: 150%;"><strong> <span style="font-family: 宋体;">内存共享(Shared     Memory)</span></strong> <span style="font-family: 宋体;"><strong> </strong></span></p>
<blockquote>
<blockquote>
<p style="line-height: 150%;"><span style="font-family: 宋体;">任务间通信最通常的方式是通过共享的数据结构进行通信,因为所有VxWorks的任务存在于一个单一的线性地址空间，<span style="font-size: 12pt; font-family: 宋体;">任务间共享数据。全局变量、线性队列、环形队列、链表、指针都可被运行在不同上下文的代码所指向。</span></span></p>
<h4 class="EntityTitle"><a name="84955"><span class="sans" style="font-family: Helvetica,sans-serif;">Shared   Data Structures</span></a></h4>
<dl class="margin">
<div class="Anchor"><a name="84952"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basica5.gif" border="0" alt="" width="299" height="129" /></div>
</dl>
<p style="line-height: 150%;">
</blockquote>
<p style="line-height: 150%;"><span style="font-family: 宋体; font-size: medium;"><strong>互斥</strong></span><span style="font-family: System; font-size: medium;"><strong>(Mutual Exclusion)</strong></span></p>
<p style="line-height: 150%;"><span style="font-family: System;">互斥是用来控制多任务对共享数据进行串行访问的同步机制。在多任务应用中，当两个或多个任务同时访问共享数据时，可能会造成数据破坏。互斥使它们串行地访问数据，从而达到保护数据的目的.</span></p>
<p style="line-height: 150%;"><span style="font-family: System;">解决互斥的几种方法:</span></p>
<p style="line-height: 150%;"><span style="font-family: System;">1. 关闭中断的方法(intLock):                能解决任务和中断ISR之间产生的互斥. </span></p>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><a name="84963"><strong><em>f</em>uncA () </strong></a></big></pre>
<pre class="Code" style="line-height: 100%;"><big><strong><a name="84963">{   int lock = intLock();</a></strong></big></pre>
<pre class="Code" style="line-height: 100%;"><big><strong><a name="84963">. .   <em class="i">critical region that cannot be interrupted</em> .</a></strong></big></pre>
<pre class="Code" style="line-height: 100%;"><big><a name="84963"><strong>intUnlock (lock);   }</strong></a></big></pre>
<p style="line-height: 150%;"><span style="font-family: System;">但在实时系统中采取这个办法会影响系统对外部中断及时响应和处理的能力.</span></p>
</blockquote>
<p style="line-height: 150%;"><span style="font-family: System;">2. 关闭系统优先级(taskLock):                关闭系统优先级,这样在当前任务执行时,除了中断外,不会有其他优先级高的任务来抢占CPU,影响当前程序运行. </span></p>
<blockquote><p><strong><a name="84977"></a></strong></p>
<p style="line-height: 100%;"><strong><a name="84977">funcA () </a></strong></p>
<p style="line-height: 100%;"><strong><a name="84977">{ taskLock ();</a></strong></p>
<p style="line-height: 100%;"><strong><a name="84977">. .  <em class="i">critical region that cannot be                  interrupted </em>.</a></strong></p>
<p style="line-height: 100%;"><strong><a name="84977">taskUnlock (); }</a></strong></p>
</blockquote>
<p style="line-height: 150%;"><span style="font-family: System;">这种方法阻止了高优先级的任务抢先运行,在实时系统中也是不适合的,除非关闭优先级的时间特别短.</span></p>
</blockquote>
<p style="line-height: 150%;"><span style="font-family: System;"> </span></p>
<blockquote>
<p style="line-height: 150%;"><span style="font-size: medium;"><strong><span style="font-family: 宋体;">信号量</span></strong><span style="font-family: System;"><strong>(Semaphore):</strong> </span></span><span style="font-family: System;"> 信号量是</span><span>解决<strong>互斥</strong></span><span style="font-family: System;">和</span><strong><span>同步协调进程</span></strong><span style="font-family: System;">最好的方法</span></p>
<blockquote>
<p class="ae" style="line-height: 150%;"><span style="font-family: &quot;Courier New&quot;;" lang="EN-US">VxWorks</span><span style="font-family: 宋体;">信号量提供最快速的任务间通信机制，它主要用于解决任务间的互斥和同步。针对不同类型的问题，有以下三种信号量：</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"> </span></p>
<p class="ae" style="margin-left: 0cm; text-indent: 0cm; line-height: 150%;"><span style="font-family: Wingdings;" lang="EN-US">Ÿ<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span><span style="font-family: 宋体;">二进制信号量<span style="font-family: 宋体;">（</span><span lang="EN-US"><span><em class="term">binary)</em> </span>使用最快捷、最广泛，主要用于同步或互斥；</span></span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"> </span></p>
<p class="ae" style="margin-left: 0cm; text-indent: 0cm; line-height: 150%;"><span style="font-family: Wingdings;" lang="EN-US">Ÿ<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span><span style="font-family: 宋体;">互斥信号量<span lang="EN-US"><span>(<em class="term">mutual    exclusion</em>) </span><span>特殊的</span></span>二进制信号量，<span lang="EN-US">主要用于优先级继承、安全删除和回溯；</span></span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"> </span></p>
<p class="ae" style="margin-left: 0cm; text-indent: 0cm; line-height: 150%;"><span style="font-family: Wingdings;" lang="EN-US">Ÿ<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span><span style="font-family: 宋体;">计数器信号量(counting)       和二进制信号量类似，保持信号量被释放（gaven)的次数</span><span style="font-family: &quot;Courier New&quot;;" lang="EN-US"> ,主要用于保护一个资源的多个例程（multiple instances of a       resource）</span></p>
<p style="line-height: 150%;"><strong><span style="font-family: 宋体;">信号量控制，函数介绍:</span></strong></p>
<blockquote>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">semBCreate</em></strong><strong>( ) </strong>分配并初始化一个<span style="font-family: 宋体;">二进制信号量</span></p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">semMCreate</em></strong><strong>( ) </strong>分配并初始化一个互斥<span style="font-family: 宋体;">信号量</span></p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">semCCreate</em></strong><strong>( ) </strong>分配并初始化一个<span style="font-family: 宋体;">计数信号量</span></p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">semDelete</em></strong><strong>( ) </strong>终止一个自由的信号量</p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">emTake</em></strong><strong>( ) </strong>占有一个信号量</p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">semGive</em></strong><strong>( ) </strong>释放一个信号量</p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">semFlush</em></strong><strong>( ) </strong>解锁所有等待信号量的任务</p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">semBCreate</em></strong><strong>( )</strong>,         <strong class="routine"><em class="routine">semMCreate</em></strong><strong>( )</strong>, and         <strong class="routine"><em class="routine">semCCreate</em></strong><strong>( )</strong>返回一个信号量<strong>ID</strong>作为其它后续任务使用该信号量的的句柄。当一个信号量被创建，它的队列（queue)类型就被确定。等待信号量的任务队列以优先级的高低排列(<strong class="symbol_UC">SEM_Q_PRIORITY</strong>)，或者一先到先得的方式排列<strong>（SEM_Q_FIFO).</strong></p>
<ul>
<li>
<p style="line-height: 150%;"><span style="font-family: System;">当一个Semaphore创建时,指定了任务队列的种类</span></p>
</li>
</ul>
<ol type="A">
<li>
<p style="line-height: 150%;"><span style="font-family: System;">semBCreat( SEM_Q_PRIORITY, SEM_FULL),               SEM_Q_PRIORITY                      指明处于等待状态的任务在等待队列中以优先级的顺序排列</span></p>
</li>
<li>
<p style="line-height: 150%;"><span style="font-family: System;">semBCreat(SEM_Q_FIFO,SEM_FULL),               SEM_Q_FIFO指明                      处于等待状态的任务在等待队列中以先进先出的顺序排列</span></p>
</li>
</ol>
<p style="line-height: 150%;"><strong class="symbol_UC"> </strong></p>
</blockquote>
<ol>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><strong><span style="font-family: 宋体;">二进制信号量<span style="font-family: 宋体;">（</span><span lang="EN-US"><em class="term">binary)</em></span></span><span> </span></strong></span></p>
</li>
</ol>
<div class="frame">
<h4 class="EntityTitle"><a name="90612"><span class="sans" style="font-family: Helvetica,sans-serif;">Taking       a Semaphore</span></a></h4>
<dl class="margin">
<div class="Anchor"><a name="90651"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basica6.gif" border="0" alt="" width="351" height="141" /></div>
</dl>
</div>
<div class="frame">
<h4 class="EntityTitle"><a name="90676"><span class="sans" style="font-family: Helvetica,sans-serif;"> Giving       a Semaphore</span></a></h4>
<dl class="margin">
<div class="Anchor" style="width: 421px; height: 151px;"><a name="90716"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basic0.gif" border="0" alt="" width="353" height="151" /></div>
</dl>
</div>
<p style="line-height: 150%;">
<blockquote>
<p style="line-height: 150%;"><span><strong>互斥进程<em>（<a name="85180">Mutual Exclusion</a>）</em></strong></span></p>
<p style="line-height: 150%;"><span>互斥</span><span style="font-family: 宋体;"><span style="font-family: 宋体;">信号量<span style="font-family: 宋体;">有效的内锁对共享资源的进入，与屏蔽中断(disabling         interrupts)和优先级锁定（preemptive locks）相比，</span></span></span><span style="font-family: 宋体;">二进制信号量将互斥的范围限制在仅与其有关的资源上。从技术上说，创建一个信号量来保护(guarding)资源。信号量初始化位可用的（FULL）</span><span style="font-family: 宋体;">。</span></p>
<p style="line-height: 150%;"><span style="font-family: System;">当一个Semaphore创建时,指定了这个semaphore是用在解决互斥还是用来同步任务</span></p>
<ol type="A">
<li>
<p style="line-height: 150%;"><span style="font-family: System;">semBCreat( SEM_Q_FIFO, SEM_FULL) ,  SEM_FULL                      指明用于任务间互斥.</span></p>
</li>
</ol>
<blockquote><p><a name="85183"></a></p>
<p style="line-height: 150%;"><a name="85183"><strong>SEM_ID semMutex;</strong></a></p>
<p style="line-height: 150%;"><a name="85183"><strong>semMutex = semBCreate (SEM_Q_PRIORITY, SEM_FULL);</strong></a></p>
<p style="line-height: 150%;"><span style="font-family: System;"> 当一个任务要进入资源，首先要得到一个信号量（take           that           semaphore），只要有任务在使用这个信号量，其它的要进入资源的任务要停止执行（blocked           from           execution），当这个任务完成了对资源的使用，它会释放信号量，允许另一个任务来使用资源。</span></p>
<p><strong><a name="85194"></a></strong></p>
<p style="line-height: 150%;"><strong><a name="85194">semTake (semMutex, WAIT_FOREVER);</a></strong></p>
<p style="line-height: 150%;"><strong><a name="85194">. .  <em class="i">critical region, only accessible by a                    single task at a time</em> . </a></strong></p>
<p style="line-height: 150%;"><strong><a name="85194">semGive (semMutex);</a></strong></p>
</blockquote>
<p style="line-height: 150%;">
<p style="line-height: 150%;"><strong><span>同步协调进程<em>（<a name="85199">Synchronization</a>）</em></span></strong> <strong><span> </span></strong></p>
</blockquote>
<blockquote>
<blockquote>
<ol type="A">
<li>
<p style="line-height: 150%;"><span style="font-family: System;">semBCreat(SEM_Q_FIFO,SEM_EMPTY),  SEM_EMPTY                      指明用于任务间同步.</span></p>
</li>
</ol>
</blockquote>
</blockquote>
<blockquote>
<blockquote>
<blockquote>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span>/* includes */</span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span>#include "vxWorks.h" </span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span>#include "semLib.h" </span></span></strong></em></big></pre>
<p><a name="85208"></a></p>
<pre class="Code" style="line-height: 100%;"><a name="85208"><big><em><strong><span class="Code"><span>SEM_ID syncSem;</span></span></strong></em></big></a></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">/* ID of sync semaphore */</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">init ( int someIntNum )</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">{ /* connect interrupt service routine */ </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">intConnect (INUM_TO_IVEC (someIntNum), eventInterruptSvcRout, 0); </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">/* create semaphore */ </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">syncSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY);</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">/* spawn task used for synchronization. */</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">taskSpawn ("sample", 100, 0, 20000, task1, 0,0,0,0,0,0,0,0,0,0);</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">} </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">task1 (void) </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">{ ... </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">semTake (syncSem, WAIT_FOREVER);</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">/* wait for event to occur */</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">printf ("task 1 got the semaphore\n");</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">... </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">/* process event */</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">} </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">eventInterruptSvcRout (void)</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">{ ... </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">semGive (syncSem); </a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">/* let task 1 process event */</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">...</a></span></span></strong></em></big></pre>
<pre class="Code" style="line-height: 100%;"><big><em><strong><span class="Code"><span><a name="85208">}</a></span></span></strong></em></big></pre>
</blockquote>
</blockquote>
</blockquote>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;"> semTake(semID,time        out)&#8212;&#8212;&#8211;有Semaphore空闲,就Take, 如果没有,由time out 定,超时则向下执行</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;">
<blockquote>
<blockquote>
<ol>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><strong><span style="font-family: 宋体;">互斥信号量</span></strong></span></p>
</li>
</ol>
<blockquote>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><span style="font-family: 宋体;">互斥信号量是一个特殊的</span><span style="font-family: 宋体;">二进制信号量，设计用于优先级继承，安全删除和回归。</span></span></p>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><span style="font-family: 宋体;">互斥信号量的使用基本和</span><span style="font-family: 宋体;">二进制信号量是类似的。但有以下不同：</span></span></p>
<ul>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><span style="font-family: 宋体;">仅仅被用做互斥。</span></span></p>
</li>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><span style="font-family: 宋体;">只能被使用它的任务释放.(It               can be given only by the task that took it.)</span></span></p>
</li>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><span style="font-family: 宋体;">ISR               不能释放它。</span></span></p>
</li>
<li>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><span style="font-family: 宋体;">不能使用函数<em class="routine"><strong class="routine">semFlush</strong></em><strong>( )</strong>。</span></span></p>
</li>
</ul>
</blockquote>
</blockquote>
<p><span style="font-family: System;"> </span><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;"><strong><span style="font-family: 宋体; font-size: medium;">优先级反转</span>(Priority        Inversion) </strong></span></p>
<p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;">优先级反转是指一个任务等待比它优先级低的任务释放资源而被阻塞，如果这时有中等优先级的就绪任务，阻塞会进一步恶化。优先级继承技术可用来解决优先级反转问题。 </span></p>
<p><span style="font-family: System;"> </span></p>
<dl class="margin"><span style="font-family: System;"> </span></p>
<div class="Anchor"><span style="font-family: System;"> <a name="91094"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basic9.gif" border="0" alt="" width="360" height="203" /> </span></div>
<p><span style="font-family: System;"> </span></p>
</dl>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;"> </span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<blockquote><p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;"><span style="font-size: small;">Priority </span> inversion arises when a higher-priority task is forced to wait an indefinite          period of time for a lower-priority task to complete.</span></p>
<p><span style="font-family: System;"> </span></p></blockquote>
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><strong><span style="font-family: 宋体; font-size: medium;">优先级继承</span><span style="font-family: System;">(Priority Inheritance)</span> </strong></p>
<blockquote>
<p style="line-height: 150%;">优先级继承可用来解决优先级反转问题。当优先级反转发生时，优先级较低的任务被暂时地提高它的优先级，使得该任务能尽快执行，释放出优先级较高的任务所需要的资源。</p>
<h4 class="EntityTitle"><a name="91101"><span class="sans" style="font-family: Helvetica,sans-serif;">Priority     Inheritance</span></a></h4>
<dl class="margin">
<div class="Anchor"><a name="91135"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basica7.gif" border="0" alt="" width="360" height="110" /></div>
</dl>
<p style="line-height: 150%;">
</blockquote>
<blockquote>
<p style="line-height: 150%;">The mutual-exclusion semaphore has the option SEM_INVERSION_SAFE, which enables a          priority-inheritance algorithm. The priority-inheritance protocol assures that a task that          owns a resource executes at the priority of the highest-priority task blocked on that          resource. Once the task priority has been elevated, it remains at the higher level until          all mutual-exclusion semaphores that the task owns are released; then the task returns to          its normal, or standard, priority. Hence, the &#8220;inheriting&#8221; task is protected          from preemption by any intermediate-priority tasks. This option must be used in          conjunction with a priority queue (SEM_Q_PRIORITY).</p>
</blockquote>
</blockquote>
<ol>
<li>
<p style="line-height: 150%;"><strong>计数信号量</strong><em><strong>（<a name="85413">Counting      Semaphores</a>）</strong></em></p>
</li>
</ol>
<blockquote>
<p style="line-height: 150%;">计数信号量是任务同步和互斥的另一种实现方式.计数信号 量除了保留信号量被释放的次数以外和二进制信号量是一样的。每次信号量被释放（gaven)一次，计数增加；每次信号量被占用（taken)一次，计数减 少;当计数减少为0时，要求得到信号量的任务被阻塞（blocked)。二进制信号量是如果一个信号量被释放，有一个任务阻塞等待，则这个任务就被 unblock.而计数信号量如果一个信号量被释放，没有任务阻塞等待，则计数增加。这说明一个被释放两次的计数信号量可以被占用(taken)两次,没 有阻塞。</p>
<p style="line-height: 150%;"><a name="85488"></a>Counting semaphores are     useful for guarding multiple copies of resources. For example, the use of five     tape drives might be coordinated using a counting semaphore with an initial     count of 5, or a ring buffer with 256 entries might be implemented using a     counting semaphore with an initial count of 256. The initial count is     specified as an argument to the <strong class="routine"><em class="routine">semCCreate</em></strong><strong>( )</strong> routine.</p>
</blockquote>
<h4 class="EntityTitle"><a name="91146"><span class="sans" style="font-family: Helvetica,sans-serif;">Counting  Semaphore Example</span></a></h4>
<table id="table2" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="3">
<hr class="tablerule" /></td>
</tr>
<tr valign="center">
<th>
<div class="CellHeading">
<p align="left"><strong><a name="91152"></a><span class="sans" style="font-family: Helvetica,sans-serif;">Semaphore            Call</span></strong></p>
</div>
</th>
<th>
<div class="CellHeadingC">
<p align="left"><a name="91154"></a><strong><span class="sans" style="font-family: Helvetica,sans-serif;">Count            after Call</span></strong></p>
</div>
</th>
<th>
<div class="CellHeading">
<p align="left"><strong><a name="91156"></a><span class="sans" style="font-family: Helvetica,sans-serif;">Resulting            Behavior</span></strong></p>
</div>
</th>
</tr>
<tr>
<td colspan="3">
<hr class="tablerule2" /></td>
</tr>
<tr valign="top">
<td>
<div class="CellBody">
<p align="left"><a name="91158"></a><strong class="routine"><em class="routine">semCCreate</em></strong><strong>( )</strong></p>
</div>
</td>
<td>
<div class="CellBodyC">
<p align="left"><a name="91160"></a>3</p>
</div>
</td>
<td>
<div class="CellBody">
<p align="left"><a name="91162"></a>Semaphore initialized with initial             count of 3.</p>
</div>
</td>
</tr>
<tr valign="top">
<td>
<div class="CellBody">
<p align="left"><a name="91164"></a><strong class="routine"><em class="routine">semTake</em></strong><strong>( )</strong></p>
</div>
</td>
<td>
<div class="CellBodyC">
<p align="left"><a name="91166"></a>2</p>
</div>
</td>
<td>
<div class="CellBody">
<p align="left"><a name="91168"></a>Semaphore taken.</p>
</div>
</td>
</tr>
<tr valign="top">
<td>
<div class="CellBody">
<p align="left"><a name="91170"></a><strong class="routine"><em class="routine">semTake</em></strong><strong>( )</strong></p>
</div>
</td>
<td>
<div class="CellBodyC">
<p align="left"><a name="91172"></a>1</p>
</div>
</td>
<td>
<div class="CellBody">
<p align="left"><a name="91174"></a>Semaphore taken.</p>
</div>
</td>
</tr>
<tr valign="top">
<td>
<div class="CellBody">
<p align="left"><a name="91176"></a><strong class="routine"><em class="routine">semTake</em></strong><strong>( )</strong></p>
</div>
</td>
<td>
<div class="CellBodyC">
<p align="left"><a name="91178"></a>0</p>
</div>
</td>
<td>
<div class="CellBody">
<p align="left"><a name="91180"></a>Semaphore taken.</p>
</div>
</td>
</tr>
<tr valign="top">
<td>
<div class="CellBody">
<p align="left"><a name="91182"></a><strong class="routine"><em class="routine">semTake</em></strong><strong>( )</strong></p>
</div>
</td>
<td>
<div class="CellBodyC">
<p align="left"><a name="91184"></a>0</p>
</div>
</td>
<td>
<div class="CellBody">
<p align="left"><a name="91186"></a>Task blocks waiting for semaphore             to be available.</p>
</div>
</td>
</tr>
<tr valign="top">
<td>
<div class="CellBody">
<p align="left"><a name="91188"></a><strong class="routine"><em class="routine">semGive</em></strong><strong>( )</strong></p>
</div>
</td>
<td>
<div class="CellBodyC">
<p align="left"><a name="91190"></a>0</p>
</div>
</td>
<td>
<div class="CellBody">
<p align="left"><a name="91192"></a>Task waiting is given semaphore.</p>
</div>
</td>
</tr>
<tr valign="top">
<td>
<div class="CellBody">
<p align="left"><a name="91194"></a><strong class="routine"><em class="routine">semGive</em></strong><strong>( )</strong></p>
</div>
</td>
<td>
<div class="CellBodyC">
<p align="left"><a name="91196"></a>1</p>
</div>
</td>
<td>
<div class="CellBody">
<p align="left"><a name="91198"></a>No task waiting for semaphore;             count incremented.</p>
</div>
</td>
</tr>
<tr>
<td colspan="3">
<hr class="tablerule" /></td>
</tr>
<tr valign="center">
<td colspan="3"></td>
</tr>
</tbody>
</table>
<p style="line-height: 150%;">
</blockquote>
</blockquote>
<p style="line-height: 150%;">
<p><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><span style="font-family: System;"> </span><span style="font-family: 宋体; font-size: medium;"><strong>消息队列(Message queues)</strong></span></p>
<blockquote>
<blockquote>
<p style="line-height: 150%;"><span style="font-size: small;"><span style="font-family: 宋体;">现实的实时应用由一系列互相独立又协同工作的任务组成。信号量为任务间同步和联锁提供了高效机制。在VxWorks中，用于但一CPU任务之间通信主要（primary）的机制是消息队列。</span></span></p>
<h4 class="EntityTitle"><a name="85857"><span class="sans" style="font-family: Helvetica,sans-serif;">Full             Duplex Communication Using Message Queues</span></a></h4>
<dl class="margin">
<div class="Anchor"><a name="85854"></a> <img class="figure" src="http://www.embhelp.com/drew/mypage/guide/images/c-basic11.gif" border="0" alt="" width="313" height="139" /></div>
</dl>
<p style="line-height: 150%;">
<p style="line-height: 150%;"><span style="font-size: small;"><span style="font-family: 宋体;">消息队列允许一定数量不同长度的消息进行排列。任何任务或中断服务程序(ISR)能够发送消息给消息队列。任何任务可以从消息队列接受消息。多任务可以从同意消息队列发送和接受消息。两个任务之间的全双工（Full-duplex）通信需要针对不同方向的两个消息队列。</span></span></p>
<p style="line-height: 150%;"><strong><span style="font-family: 宋体;">消息队列函数介绍</span></strong><span style="font-family: System;"> </span></p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">msgQCreate</em></strong><strong>( ) </strong>创建斌初始化一个消息队列</p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">msgQDelete</em></strong><strong>( ) </strong>终止并释放一个消息队列</p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">msgQSend</em></strong><strong>( )</strong> 发送一个消息到消息队列</p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">msgQReceive</em></strong><strong>( ) </strong>从消息队列接受一个消息</p>
<p style="line-height: 150%;">消息队列是由函数<span style="font-family: 宋体;"><strong>msgQCreate            (MAX_MSGS, MAX_MSG_LEN, MSG_Q_PRIORITY)</strong></span>创建。它的参数<span style="font-family: 宋体;"><strong>MAX_MSGS</strong></span>指定了消息队列中可允许最多可以排列的消息数和每个消息允许的最大的字节数<span style="font-family: 宋体;"><strong>MAX_MSG_LEN</strong></span>。</p>
<p style="line-height: 150%;">一个任务或中断服务程序(ISR)用函数<strong class="routine"><em class="routine">msgQSend</em></strong><strong>( )</strong>发送一个消息到消息队列。如果没有任务等待消息队列的消息，这个消息被添加消息缓存的队列里。如果某些任务已经在等待消息队列中的消息，消息立刻被传递给第一个等待的消息的任务。</p>
<p style="line-height: 150%;">一个任务用函数<strong class="routine"><em class="routine">msgQReceive</em></strong><strong>( )</strong>从消息队列得到一个消息。如果消息队列缓存中有消息存在，第一个消息立刻出列并回到调用处(caller).如果没有消息存在，则任务（calling              task）停止（blocks)并被添加到等待消息的任务队列中。这个等待的任务队列按照优先级或先进先出（FIFO)规则排列，这个规则有消息队列创建时所指定。</p>
<p style="line-height: 150%;"><strong>等待时间限制（time out)</strong></p>
<p style="line-height: 150%;"><strong class="routine"><em class="routine">msgQSend</em></strong><strong>( )</strong> 和 <strong class="routine"><em class="routine">msgQReceive</em></strong><strong>( )</strong>都有时间限制参数。当发送一个消息，如果消息队列缓存这时没有空间，这个参数指定允许等待的时间（ticks数），直到队列缓存有空间来接收消息。当接收消息时，如果消息队列没有消息，这个参数指定允许等待的时间（ticks数），直到消息队列有消息。</p>
<p style="line-height: 150%;">
<pre class="Code" style="line-height: 150%;"><strong><a name="85925"><span style="font-size: medium;"><em>/* In this example, task t1 creates the message queue and sends a message
 * to task t2. Task t2 receives the message from the queue and simply
 * displays the message.
 */</em></span></a></strong><span style="font-size: medium;"><em>

<strong><a name="85927">/* includes */
#include "vxWorks.h"
#include "msgQLib.h"</a></strong>

<strong><a name="85930">/* defines */
#define MAX_MSGS (10)
#define MAX_MSG_LEN (100)</a></strong>

<strong><a name="85933">MSG_Q_ID myMsgQId;</a></strong>

<strong><a name="85935">task2 (void)
    {
    char msgBuf[MAX_MSG_LEN];</a></strong>

<strong><a name="85938">    /* get message from queue; if necessary wait until msg is available */
    if (msgQReceive(myMsgQId, msgBuf, MAX_MSG_LEN, WAIT_FOREVER) == ERROR)
        return (ERROR);</a></strong>

<strong><a name="85942">    /* display message */
    printf ("Message from task 1:\n%s\n", msgBuf);
    }</a></strong>

<strong><a name="85946">#define MESSAGE "Greetings from Task 1"
task1 (void)
    {
    /* create message queue */
    if ((myMsgQId = msgQCreate (MAX_MSGS, MAX_MSG_LEN, MSG_Q_PRIORITY))
        == NULL)
        return (ERROR);</a></strong>

<strong><a name="85951">    /* send a normal priority message, blocking if queue is full */
    if (msgQSend (myMsgQId, MESSAGE, sizeof (MESSAGE), WAIT_FOREVER,
                  MSG_PRI_NORMAL) == ERROR)
        return (ERROR);
    }</a></strong></em></span></pre>
<p style="line-height: 150%;">
</blockquote>
<p style="line-height: 150%;"><strong><span style="font-family: 宋体;"><span style="font-size: medium;">管道（<em><a name="86337">Pipes</a>）</em></span></span></strong></p>
<blockquote>
<p style="line-height: 150%;"><span style="font-family: 宋体;"><span style="font-size: medium;">管道对消息队列提供了一个可供选择的接口，<span style="font-family: Courier New;" lang="EN-US">VxWorks</span>的<span style="font-family: Courier New;" lang="EN-US">I/O</span>系统。管道是虚拟的I/O设备，由驱动<strong class="library">pipeDrv</strong>管理。函数<em class="routine"><strong class="routine">pipeDevCreate</strong></em><strong>()</strong>创建一个管道设备，这个调用指定管道的名字，能被排列的最多的消息数，和每个消息允许的长度。</span></span></p>
<p style="line-height: 150%;"><strong><a name="86340"><em class="textVariable">status</em> = pipeDevCreate (&#8220;<em class="textVariable">/pipe/name</em>&#8220;, <em class="textVariable">max_msgs</em>,            <em class="textVariable">max_length</em>);</a></strong></p>
<p style="line-height: 150%;">被创建的管道是一个通常命名（named)的I/O设备,任务能用标准的I/O函数打开，读，写管道，并能调用ioctl例程。<span style="font-family: 宋体;"><span style="font-size: medium;">当任务试图从一个空的管道中读取数据，或向一个满的管道中写入数据时，任务被阻塞。和消息队列一样，ISR可以向管道写入，但不能从管道读取。</span></span></p>
<p style="line-height: 150%;"><span style="font-size: medium;"><span style="font-family: 宋体;">做为I/O设备，管道提供了消息队列所没有的重要特性，调用<em><strong>select()</strong></em></span></span></p>
</blockquote>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.result-search.com/sty/2009/04/26/vxwork-getting-start.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
