📄 嵌入式系统vxworks介绍及编程.htm
字号:
name=90716></A><IMG class=figure height=151
src="嵌入式系统VxWorks介绍及编程.files/c-basic0.gif" width=353 border=0>
</DIV></DL></DIV>
<P style="LINE-HEIGHT: 150%">
<BLOCKQUOTE>
<P style="LINE-HEIGHT: 150%"><FONT lang=ZH-CN><STRONG>互斥进程<I>(<A
name=85180>Mutual Exclusion</A>)</I></STRONG></FONT>
<P style="LINE-HEIGHT: 150%"><FONT lang=ZH-CN>互斥</FONT><FONT
face=宋体><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">信号量<SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman">有效的内锁对共享资源的进入,与屏蔽中断(disabling
interrupts)和优先级锁定(preemptive locks)相比,</SPAN></SPAN></FONT><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">二进制信号量将互斥的范围限制在仅与其有关的资源上。从技术上说,创建一个信号量来保护(guarding)资源。信号量初始化位可用的(FULL)</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman">。</SPAN>
<P style="LINE-HEIGHT: 150%"><FONT lang=ZH-CN
face=System>当一个Semaphore创建时,指定了这个semaphore是用在解决互斥还是用来同步任务</FONT></P>
<OL type=A>
<LI>
<P style="LINE-HEIGHT: 150%"><FONT lang=ZH-CN
face=System>semBCreat( SEM_Q_FIFO, SEM_FULL) , SEM_FULL
指明用于任务间互斥.</FONT> </P></LI></OL>
<BLOCKQUOTE><A name=85183><B>
<P style="LINE-HEIGHT: 150%">SEM_ID semMutex;</B></A></P>
<P style="LINE-HEIGHT: 150%"><A name=85183><B>semMutex =
semBCreate (SEM_Q_PRIORITY, SEM_FULL);</B></A></P>
<P style="LINE-HEIGHT: 150%"> <FONT lang=ZH-CN
face=System> 当一个任务要进入资源,首先要得到一个信号量(take that
semaphore),只要有任务在使用这个信号量,其它的要进入资源的任务要停止执行(blocked from
execution),当这个任务完成了对资源的使用,它会释放信号量,允许另一个任务来使用资源。</FONT></P><B><A
name=85194>
<P style="LINE-HEIGHT: 150%">semTake (semMutex,
WAIT_FOREVER);</A></B></P>
<P style="LINE-HEIGHT: 150%"><B><A name=85194>. . <I
class=i>critical region, only accessible by a single task at a
time</I> . </A></B></P>
<P style="LINE-HEIGHT: 150%"><B><A name=85194>semGive
(semMutex);</A></B></P></BLOCKQUOTE>
<P style="LINE-HEIGHT: 150%"> </P>
<P style="LINE-HEIGHT: 150%"><STRONG><FONT lang=ZH-CN>同步协调进程<I>(<A
name=85199>Synchronization</A>)</I></FONT></STRONG> <STRONG><FONT
lang=ZH-CN> </FONT></STRONG></P></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<OL type=A start=2>
<LI>
<P style="LINE-HEIGHT: 150%"><FONT lang=ZH-CN
face=System>semBCreat(SEM_Q_FIFO,SEM_EMPTY), SEM_EMPTY
指明用于任务间同步.</FONT> </P></LI></OL></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN>/* includes */</FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN>#include "vxWorks.h" </FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN>#include "semLib.h" </FONT></SPAN></STRONG></EM></BIG></PRE><A
name=85208><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN>SEM_ID syncSem;</FONT></SPAN></STRONG></EM></BIG></A></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>/* ID of sync semaphore */</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>init ( int someIntNum )</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>{ /* connect interrupt service routine */ </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>intConnect (INUM_TO_IVEC (someIntNum), eventInterruptSvcRout, 0); </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>/* create semaphore */ </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>syncSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY);</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>/* spawn task used for synchronization. */</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>taskSpawn ("sample", 100, 0, 20000, task1, 0,0,0,0,0,0,0,0,0,0);</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>} </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>task1 (void) </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>{ ... </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>semTake (syncSem, WAIT_FOREVER);</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>/* wait for event to occur */</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>printf ("task 1 got the semaphore\n");</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>... </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>/* process event */</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>} </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>eventInterruptSvcRout (void)</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>{ ... </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>semGive (syncSem); </A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>/* let task 1 process event */</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>...</A></FONT></SPAN></STRONG></EM></BIG></PRE><PRE class=Code style="LINE-HEIGHT: 100%"><BIG><EM><STRONG><SPAN class=Code><FONT lang=ZH-CN><A name=85208>}</A></FONT></SPAN></STRONG></EM></BIG></PRE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE><FONT
lang=ZH-CN face=System>
<P style="LINE-HEIGHT: 150%">
semTake(semID,time out)--------有Semaphore空闲,就Take, 如果没有,由time
out 定,超时则向下执行</P></BLOCKQUOTE></BLOCKQUOTE></FONT>
<P style="LINE-HEIGHT: 150%"> </P>
<BLOCKQUOTE>
<BLOCKQUOTE>
<OL start=2>
<LI>
<P style="LINE-HEIGHT: 150%"><FONT face=宋体><B><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">互斥信号量</SPAN></B></FONT>
</P></LI></OL>
<BLOCKQUOTE>
<P style="LINE-HEIGHT: 150%"><FONT face=宋体><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman">互斥信号量是一个特殊的</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">二进制信号量,设计用于优先级继承,安全删除和回归。</SPAN></FONT></P>
<P style="LINE-HEIGHT: 150%"><FONT face=宋体><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman">互斥信号量的使用基本和</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">二进制信号量是类似的。但有以下不同:</SPAN></FONT></P>
<UL>
<LI>
<P style="LINE-HEIGHT: 150%"><FONT face=宋体><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman">仅仅被用做互斥。</SPAN></FONT>
</P>
<LI>
<P style="LINE-HEIGHT: 150%"><FONT face=宋体><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman">只能被使用它的任务释放.(It
can be given only by the task that took it.)</SPAN></FONT> </P>
<LI>
<P style="LINE-HEIGHT: 150%"><FONT face=宋体><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman">ISR
不能释放它。</SPAN></FONT> </P>
<LI>
<P style="LINE-HEIGHT: 150%"><FONT face=宋体><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman">不能使用函数<I
class=routine><B
class=routine>semFlush</B></I><B>( )</B>。</SPAN></FONT>
</P></LI></UL></BLOCKQUOTE></BLOCKQUOTE><FONT lang=ZH-CN face=System>
<P style="LINE-HEIGHT: 150%"></P>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P style="LINE-HEIGHT: 150%"><STRONG><FONT lang=ZH-CN face=宋体
size=4>优先级反转</FONT>(Priority Inversion) </STRONG></P>
<BLOCKQUOTE>
<P
style="LINE-HEIGHT: 150%">优先级反转是指一个任务等待比它优先级低的任务释放资源而被阻塞,如果这时有中等优先级的就绪任务,阻塞会进一步恶化。优先级继承技术可用来解决优先级反转问题。
</P>
<DL class=margin>
<DIV class=Anchor><A name=91094></A><IMG class=figure height=203
src="嵌入式系统VxWorks介绍及编程.files/c-basic9.gif" width=360 border=0>
</DIV></DL>
<P style="LINE-HEIGHT: 150%"> </P></BLOCKQUOTE>
<BLOCKQUOTE>
<P style="LINE-HEIGHT: 150%"><FONT size=3>Priority
</FONT>inversion arises when a higher-priority task is forced to
wait an indefinite period of time for a lower-priority task to
complete.</P></BLOCKQUOTE></FONT>
<P style="LINE-HEIGHT: 150%"><STRONG><FONT lang=ZH-CN face=宋体
size=4>优先级继承</FONT><FONT lang=ZH-CN face=System>(Priority
Inheritance)</FONT> </STRONG></P><FONT lang=ZH-CN face=System>
<BLOCKQUOTE>
<P
style="LINE-HEIGHT: 150%">优先级继承可用来解决优先级反转问题。当优先级反转发生时,优先级较低的任务被暂时地提高它的优先级,使得该任务能尽快执行,释放出优先级较高的任务所需要的资源。</P>
<H4 class=EntityTitle><A name=91101><FONT class=sans
face="Helvetica, sans-serif" size=-1>Priority
Inheritance</FONT></A></H4>
<DL class=margin>
<DIV class=Anchor><A name=91135></A><IMG class=figure height=110
src="嵌入式系统VxWorks介绍及编程.files/c-basica7.gif" width=360 border=0>
</DIV></DL>
<P style="LINE-HEIGHT: 150%"> </P></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 "inheriting" 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></FONT></BLOCKQUOTE>
<P style="LINE-HEIGHT: 150%"></P>
<OL start=3>
<LI>
<P style="LINE-HEIGHT: 150%"><B>计数信号量</B><I><B>(<A
name=85413>Counting Semaphores</A>)</B></I> </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 <B class=routine><I
class=routine>semCCreate</I></B><B>( )</B>
routine.</P></BLOCKQUOTE>
<H4 class=EntityTitle align=left><A name=91146><FONT class=sans
face="Helvetica, sans-serif" size=-1>Counting Semaphore
Example</FONT></A></H4>
<TABLE id=table2 cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD colSpan=3>
<HR class=tablerule>
</TD></TR>
<TR vAlign=center>
<TH>
<DIV class=CellHeading>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -