<FONT face=Arial>
<P class=lessonpagetitle style="MARGIN: auto 0cm"><STRONG>RTOS Basics: Synchronizing Resource access</STRONG></P>
<P><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">A RTOS typically defines a resource management to synchronize tasks while accessing resources.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 11pt; COLOR: blue; mso-bidi-font-size: 10.0pt">The definition of a <STRONG>resource</STRONG> in this context includes both hardware (e.g. memory, I/O port) and software (global functions and global variables).</SPAN><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt"> Resources are accessible to all tasks in a real time system, but obviously care must be taken to ensure that different tasks don’t attempt to use the same resource in conflicting ways. In preemptive systems, tasks can be preempted during a resource access. When that happens, another task might access the same resource and creates an inconsistent state because the resource access of the other task might be incomplete. Resource managers prevent the simultaneous access to a resource in preemptive systems by synchronizing the tasks to avoid this situation. Like task scheduling, there are several protocols for controlling resources. In this section we discuss three common ones: semaphore protocol, priority ceiling protocol, and highest locker's priority protocol.<o:p></o:p></SPAN></P>
<H3 style="MARGIN: auto 0cm"><SPAN style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial">Semaphore Protocol<o:p></o:p></SPAN></H3>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">With the semaphore method, a task that is about to use a resource sets a flag (semaphore) that says the resource is in use. A task can only get a resource if no other task holds this resource (assignment in FIFO-principle). While this is a valid, simple, and straightforward control system, it does have known problems. The problems with semaphores are priority inversion and deadlocks. </SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt"><o:p></o:p></SPAN> </P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">Priority inversion is the term for the scenario where a higher priority task fails to run when it should, but a lower priority task can run instead. </SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt"><o:p></o:p></SPAN> </P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">Figure 6 illustrates the priority inversion problem: Priority inversion can happen when a higher priority task (Task 3) has to wait on a resource which is occupied by a preempted low priority task (Task 1). A task with a priority between Task 1 and Task 3 (like Task 2) now has the chance to run in the meantime. By running this task, Task1 is prevented from releasing the resource which Task 3 is waiting on. The situation can only be resolved by a termination of Task2, but this means Task 2 runs before the higher priority task (priority inversion).<o:p></o:p></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt"> <v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><IMG alt="Figure 1-6: Occurrence of priority inversion" src="http://www.codewarrioru.com/coursecontent/rendered/qaregion/OSEKIntro/OSEKIntro_lesson01/OSEK_Intro_Fig01_06.jpg" type="graphic"><BR><SPAN class=illustrationname1><SPAN style="FONT-SIZE: 12pt"><EM><STRONG>Figure 1-6: Occurrence of priority inversion</STRONG></EM></SPAN></SPAN><FONT size=2> </FONT></P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt"></SPAN> </P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">Semaphores can also result in deadlocks. Assume two tasks share two resources and lock them in different orders. One task (Task A) locks the first resource using semaphore S1. Then, in the ordinary course of events a second task (Task

runs and locks the second resource with semaphore S2. It needs to use the first resource (S1), but cannot because it is locked by the first task (Task A). All it can do is pass control back to the first task (Task A), which then attempts to use the second resource (S2). It cannot use it because the second task (Task

has locked it, so it passes control back to the second resource. But the second resource cannot run, so it passes control back to the first task, endlessly. The two tasks are deadlocked because each is waiting on a semaphore locked by the other task. Figure 7 illustrates this situation.<o:p></o:p></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt"><IMG alt="Figure 1-7: Two tasks reaching deadlock" src="http://www.codewarrioru.com/coursecontent/rendered/qaregion/OSEKIntro/OSEKIntro_lesson01/OSEK_Intro_Fig01_07.jpg" type="graphic"><BR><SPAN class=illustrationname1><SPAN style="FONT-SIZE: 12pt"><EM><STRONG>Figure 1-7: Two tasks reaching deadlock</STRONG></EM></SPAN></SPAN><FONT size=2> </FONT></P>
<H3 style="MARGIN: auto 0cm"><SPAN style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial">Priority Ceiling Protocol<o:p></o:p></SPAN></H3>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">With the priority ceiling protocol, each resource gets its own ceiling priority. The ceiling priority of the resource is equivalent to the priority of the highest priority task that may use this resource. A task will get the resource’s ceiling priority during the access of that resource and get its old priority back after the access. All tasks below the ceiling priority cannot preempt other tasks, therefore all other tasks using the same resource cannot preempt. Preemption can only take place after the resource access is completed and the original task's priority is restored.</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt"><o:p></o:p></SPAN> </P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">This method prevents preemption, which is the primary cause of possible resource conflicts. Therefore problems like priority inversion and deadlocks cannot happen. The main disadvantage however, is that all lower priority tasks get locked out, even when they are not using the resource.</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt"><o:p></o:p></SPAN> </P>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">OSEK/VDX OS uses this approach for resource management.<o:p></o:p></SPAN></P>
<H3 style="MARGIN: auto 0cm"><SPAN style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial">Highest Locker's Priority Protocol<o:p></o:p></SPAN></H3>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">This protocol is similar to the priority ceiling protocol except with the possibility that a task, not using the resource and with a lower priority than the priority of a particular resource, can preempt the tasks with original lower priority using that resource. To achieve this, the scheduler needs to consider two task priorities, the original priority and the resource priority of a task.<o:p></o:p></SPAN></P>
<H3 style="MARGIN: auto 0cm"><SPAN style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial">Summary<o:p></o:p></SPAN></H3>
<P style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt">This section introduced you to some of the concepts involved with synchronizing access to shared resources. Some of these protocols are intricate with subtle side effects. The principal point to take away from this section is that an RTOS must control simultaneous access to shared resources, and that the mechanism used can have significant consequences with regard to how tasks are scheduled.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 12.0pt"><FONT face="Times New Roman"> <o:p></o:p></FONT></SPAN></P>
<P class=lessonpagetitle style="MARGIN: auto 0cm"></FONT></P>