Вы находитесь на странице: 1из 3

LAB4

PRIORITYINHERITANCEPROTOCOL/PRIORITYCEILINGPROTOCOL

Introduction:

In real time systems, the problems due to priority inversion can be solved by using resource access
protocolslikePriorityInheritanceProtocol(PIP)orPriorityCeilingProtocol(PCP).IntheXilkernel,these
protocols are not implemented as part of the kernel. However, we have APIs that allows us to
dynamicallychangethepriorityofthethreadsduringruntime.TheseAPIscanbeusedtoimplement
thepriorityinheritancemechanismforthreads.

Objectives:

TodevelopcodewhichperformPriorityInheritanceProtocol
TodevelopcodewhichperformPriorityCeilingProtocol

Inputs:

ThedualcoresystemthatwasdevelopedinLab3.
The operating systems for both the Microblaze cores will be the Xilkernel. Ensure that all
settings required for Xilkernel are done for both the processors through the Board Support
PackageSettings.Thereareadditionalpointerstotakenote:

From Board Support Package Settings, enable pthreads, timers and mutexes via the
Xilkernelconfigurationpageifyourequirethesefacilities.
Modify the linker script so that the code memory spaces of both processors reside in
nonoverlappingaddressranges.
Ensurethatmain_progissetasapriority0staticthreadtoberunontheXilkernel.
If you prefer to debug through the JTAG interface, ensure that stdin, stdout and
xmdstub_peripheralunderBoardSupportPackageSettingsaresettomdm_0(ifyour
MicroblazeDebugModulehastheidentifiermdm_0),andunderRunConfigurations,
setSTDIOConnectiontouseJTAGUARTport.

PriorityInheritanceProtocol:

ThesourcecodetobeusedforPriorityInheritanceProtocolispip.c

CreateanewsoftwareapplicationprojectusingthisCcodeassource.Youmayuse
microblaze_0astheassociatedprocessorforthis.

SourcecodeDescription:

Threethreadsarecreatedbytheprogram,withprioritiesspecifiedas1,2and3respectively.

Here,thethreadsaresuchthatthread1hashighestprioritywhilethread3haslowestpriority.
1


Also,thread1and3areallowedmutuallyexclusiveaccesstotheUARTterminalwhilethread2
canaccesstheUARTanytime.Suchascenarioiscreatedtodemonstratethepriorityinversion
problem.

Whenthread3lockstheUARTresource,thread1willbedirectlyblockedbythread3during
anyaccesstotheUART.However,thread2willcontinuetopreemptthread3andexecuteas
showninthescreenshotbelow.Thisistheproblemofpriorityinversionwherealowerpriority
thread2isallowedtoexecuteoverahigherprioritythread1whichremainsblockedbythread3.

Wecandynamicallyalterthepriorityofthethreadsduringruntimesoasimplementapriority
inheritancemechanismthroughwhichthethread3inheritsthepriorityoftheblockedthread1
duringtheexecutionofitscriticalsection.Thus,wecanensurethatthread2willnotbeableto
preemptthread3duringitsexecution.Oncetheexecutionofthread3iscomplete,thread1is
automatically allowed to run since it has higher priority over thread 2 and also the resource
requestedbythread1isnowunlocked.
Functioncallswhichcandynamicallyalterthreadprioritiesareshownbelow:(documentation
isxilkernel_v5_00_a.pdf,page10)
1.

int pthread_getschedparam(pthread_t thread, int *policy,


struct sched_param *param)

2.

int pthread_setschedparam(pthread_t thread, int policy,


const struct sched_param *param)

LabAssessment
1.
2.

YouarerequiredtoimplementthePriorityInheritanceProtocolusingfunctioncalls
pip_mutex_lock()/pip_mutex_unlock().
Accompanyyourcodewithanannotateddiagram,eitherhandwrittenorcomputer
drawn,atimelinewhichshowsthethreadsthatarecurrentlycontrollingtheresource.


PriorityCeilingProtocol:

Priority Ceiling Protocol is used to solve the


issue of deadlocks in scheduling and also to
reduce the blocking time of a process to
duration of utmost one critical section of a
lowerprioritythread.
Use the source code pcp.c to implement
the priority ceiling protocol. You may use
microblaze_0foritsimplementation.
SourceCodeDescription:

Threethreadsarecreatedbytheprogram,in
order of decreasing priority. There are three
resources A, B and C. Thread 1 uses only the
resource B, while Threads 2 and 3 uses the
resource A and C. Each resource is a structure
objectwithtwoelements:valueandceiling.The
valueisusedinthecalculationsandtheceiling
referstothepriorityceilingoftheresource.

Therearethreefunctions,sub() calledbyThread1,add2 calledbythread2andadd3 calledby


thread3.

Whenthecodeisexecuted,atsomepointthereoccursadeadlockbetweenthread2and3.Atthis
point,onlythethread1willcontinuetoexecute.Thedeadlockoccursduetothefactthatthread3locks
theresourceCfirstandthengoestolocktheresourceA,whilethread2lockstheresourceAfirstand
thengoestolocktheresourceC.Atsomepoint,thethread2startsexecutingjustbeforethread3gets
resourceAlocked.Atthispoint,thedeadlockoccurs.

LabAssessment
3.

4.

YouarerequiredtoimplementthePriorityCeilingProtocol,makinguseofthePriority
InheritanceProtocolandadditionalceilingparametersfortheresources.Namethetwo
functionsaspcp_mutex_lock()/pcp_mutex_unlock().
Accompanyyourcodewithanannotateddiagram,eitherhandwrittenorcomputer
drawn,atimelinewhichshowsthethreadsthatarecurrentlycontrollingtheresource.