?? real-time experiment #7 priority inversion.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0076)http://www.rt.db.erau.edu/experiments/vx/priorityInversion/Experiment-7.html -->
<HTML><HEAD><TITLE>Real-Time Experiment #7: Priority Inversion</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>
<BODY>
<CENTER>
<H1>Embry-Riddle Real-Time Laboratory Experiment<BR>Experiment #7<BR>Priority
Inversion </H1></CENTER>
<HR SIZE=3>
<H2>Introduction</H2>Priority inversion occurs when a higher-priority task is
forced to wait an indefinite period for the completion of a lower priority task.
For example, <B>prioHigh, prioMedium,and prioLow</B> are task of high, medium,
and low priority, respectively. <B>prioLow</B> has acquired a resource by taking
its associated binary semaphore. When <B>prioHigh</B> preempts <B>prioLow</B>
and contends for the resource by taking the same semaphore, it becomes blocked.
If <B>prioHigh</B> would be blocked no longer than the time it normally takes
<B>prioLow</B> to finish with the resource, there would be no problem, because
the resource can't be preempted. However, the low priority task is vulnerable to
preemption by the medium priority task, <B>prioMedium</B>, which could prevent
<B>prioLow</B> from relinquishing the resource. This condition could persist,
blocking <B>prioHigh</B> for an extensive period of time.
<P></P>
<HR SIZE=3>
<H2>Objectives</H2>The following are the primary objectives of this experiment:
<UL>
<LI>To demonstrate VxWorks' priority inversion avoidance mechanisms. </LI></UL>
<P></P>
<HR SIZE=3>
<H2>Description</H2>To address the problem of priority inversion, VxWorks
provides an additional option when using mutual exclusion semaphores. This
option is <B>SEM_INVERSION_SAFE</B> which enables a priority inheritance
algorithm. This algorithm insures that the task that owns a resource executes at
the priority of the highest priority task blocked on that resource. When
execution is complete, the task relinquishes the resource and returns to its
normal priority. Therefore, the inheriting task is protected from preemption by
an intermediate priority task. This option must be used in conjunction with
<B>SEM_Q_PRIORITY</B>:
<P></P><B>semId = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE);</B>
<P></P><B>1. Example: </B>
<P></P>The example below illustrates a typical situation in which priority
inversion takes place. Here is the what happens:
<P></P>1. <B>prioLow</B> task locks the semaphore.
<P></P>2. <B>prioLow</B> task gets preempted by <B>prioMedium</B> task which
runs for a long time which results in the blocking of <B>prioLow</B>.
<P></P>3. <B>prioHigh</B> task preempts <B>prioMedium</B> task and tries to lock
the semaphore which is currently locked by <B>prioLow</B>.
<P></P>The situation is shown in the printout from running the program:
<P></P><PRE>------------------------------------------------------------------------------------
Low priority task locks semaphore
Medium task running
High priority task trys to lock semaphore
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
------------------------------------------Medium priority task exited
Low priority task unlocks semaphore
High priority task locks semaphore
High priority task unlocks semaphore
High priority task trys to lock semaphore
High priority task locks semaphore
High priority task unlocks semaphore
High priority task trys to lock semaphore
High priority task locks semaphore
High priority task unlocks semaphore
..........................................High priority task exited
Low priority task locks semaphore
Low priority task unlocks semaphore
Low priority task locks semaphore
Low priority task unlocks semaphore
..........................................Low priority task exited
------------------------------------------------------------------------------------
</PRE>
<P></P>Since both <B>prioLow</B> and <B>prioHigh</B> are both blocked,
<B>prioMedium</B> runs to completion(a very long time). By the time
<B>prioHigh</B> runs it is likely that it has missed its timing requirements.
<P></P>Here is what the code looks like:
<P></P><PRE>------------------------------------------------------------------------------------
/* includes */
#include "vxWorks.h"
#include "taskLib.h"
#include "semLib.h"
/* function prototypes */
void prioHigh(void);
void prioMedium(void);
void prioLow(void);
/* globals */
#define ITER 3
#define HIGH 102 /* high priority */
#define MEDIUM 103 /* medium priority */
#define LOW 104 /* low priority */
#define LONG_TIME 3000000
SEM_ID semMutex;
void inversion(void) /* function to create the three tasks */
{
int i, low, medium, high;
printf("\n\n....................##RUNNING##.........................\n\n\n");
/* create semaphore */
semMutex = semMCreate(SEM_Q_PRIORITY); /* priority based semaphore */
/* spawn the three tasks */
if((low = taskSpawn("task1",LOW,0x100,20000,(FUNCPTR)prioLow,0,0,0,0,0,0,0,
0,0,0)) == ERROR)
printf("taskSpawn prioHigh failed\n");
if((medium = taskSpawn("task2",MEDIUM,0x100,20000,(FUNCPTR)prioMedium,0,0,0,0,0,0,0,
0,0,0)) == ERROR)
printf("taskSpawn prioMedium failed\n");
if((high = taskSpawn("task3",HIGH,0x100,20000,(FUNCPTR)prioHigh,0,0,0,0,0,0,0,
0,0,0)) == ERROR)
printf("taskSpawn prioLow failed\n");
}
void prioLow(void)
{
int i,j;
for (i=0; i < ITER; i++)
{
semTake(semMutex,WAIT_FOREVER); /* wait indefinitely for semaphore */
printf("Low priority task locks semaphore\n");
for (j=0; j < LONG_TIME; j++);
printf("Low priority task unlocks semaphore\n");
semGive(semMutex); /* give up semaphore */
}
printf("..........................................Low priority task exited\n");
}
void prioMedium(void)
{
int i;
taskDelay(20);/* allow time for task with the lowest priority to seize semaphore */
for (i=0; i < LONG_TIME*10; i++)
{
if ((i % LONG_TIME) == 0)
printf("Medium task running\n");
}
printf("------------------------------------------Medium priority task exited\n");
}
void prioHigh(void)
{
int i,j;
taskDelay(30);/* allow time for task with the lowest priority to seize semaphore */
for (i=0; i < ITER; i++)
{
printf("High priority task trys to lock semaphore\n");
semTake(semMutex,WAIT_FOREVER); /* wait indefinitely for semaphore */
printf("High priority task locks semaphore\n");
for (j=0; j < LONG_TIME; j++);
printf("High priority task unlocks semaphore\n");
semGive(semMutex); /* give up semaphore */
}
printf("..........................................High priority task exited\n");
}
------------------------------------------------------------------------------------
</PRE>
<P></P>
<HR SIZE=3>
<H2>Procedures</H2>1. Copy the source code in the example and compile it.
<P></P>2. Load the object file onto the target machine.
<P></P>3. Run the examples by executing the main routine("inversion") of the
example on WindSh terminal.
<P></P>Note: Make sure you have redirected I/O, otherwise you won't see the
results of the <EM>printf()</EM> commands.
<HR SIZE=3>
<H2>Follow On Experiment</H2>Experiment 1. Modify the program so that the
problem with priority inversion is eliminated and the printout from the program
looks like the following:
<P></P><PRE>Low priority task locks semaphore
Medium task running
High priority task trys to lock semaphore
Low priority task unlocks semaphore
High priority task locks semaphore
High priority task unlocks semaphore
High priority task trys to lock semaphore
High priority task locks semaphore
High priority task unlocks semaphore
High priority task trys to lock semaphore
High priority task locks semaphore
High priority task unlocks semaphore
..........................................High priority task exited
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
Medium task running
------------------------------------------Medium priority task exited
Low priority task locks semaphore
Low priority task unlocks semaphore
Low priority task locks semaphore
Low priority task unlocks semaphore
..........................................Low priority task exited
</PRE>
<P></P>
<HR SIZE=3>
<H2>Additional Information</H2>Refer to VxWorks User's Manual and Reference
Manual.
<P></P>
<HR SIZE=3>
<CENTER>
<H4><A
href="http://www.rt.db.erau.edu/experiments/vx/toc/TableOfContents.html">Return
to Primary Table of Contents </A></H4></CENTER>
<HR SIZE=3>
<CENTER>Last Updated: 26 March 1997<BR><EM>Created by: Dan Eyassu</EM><BR><A
href="mailto:eyassud@db.erau.edu">eyassud@db.erau.edu</A><BR></CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -