Upload
janardhan-reddy
View
214
Download
0
Embed Size (px)
Citation preview
7/30/2019 ems reddy
1/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,
Inc.1
INTER-PROCESS COMMUNICATION
AND SYNCHRONISATION:
LessonLesson--11:11: Priority InversionPriority InversionProblem and Deadlock SituationsProblem and Deadlock Situations
7/30/2019 ems reddy
2/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,
Inc.2
1.1. Priority Inversion
7/30/2019 ems reddy
3/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,
Inc.3
Assume
Priorities of tasks be in an order such that
taskIhighest priority, taskJa lower, andtaskKthe lowest priority.
Only tasksIand Kshare the data andJdoes
not share data with K.
Also let tasksIand Kalone share a
semaphore sikand notJ.
7/30/2019 ems reddy
4/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,
Inc.4
Few tasks share a semaphoreFew tasks share a semaphore
Why do only a few tasks share a
semaphore? Can't all share a semaphore? AnswerWorst-case latency becomes too
high and may exceed the deadline if all
tasks are blocked when one task takes asemaphore.
The worst-case latency will be small only if
the time taken by the tasks that share theresources is relevant
7/30/2019 ems reddy
5/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,
Inc.5
At an instant t0, suppose task K takes sik , it
does not block taskJ and blocks only the
taskI. This happens because only tasks I and K
share the data andJdoes not and I is blocked
at instance t0 due to wait for some message
and sik.
Consider the problem that now arises on
selective sharing between KandI.
Priority Inversion SituationPriority Inversion Situation
7/30/2019 ems reddy
6/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,
Inc.6
At next instant t1, let task Kbecome ready
first on an interrupt.
Now, assume that at next instant t2, taskIbecomes ready on an interrupt or getting the
waiting message.
At this instant, Kis in the critical section.
Therefore, taskI cannot start at this instant
due to K being in the critical region.
Priority Inversion SituationPriority Inversion Situation
7/30/2019 ems reddy
7/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
7
Now, if at next instant t3, some action
(event) causes the unblocked higher than K
priority taskJto run. After instant t3, running task J does not
allow the highest priority task I to run
because K is not running, and therefore K
can't release sikthat it shares withI.
Priority Inversion SituationPriority Inversion Situation
7/30/2019 ems reddy
8/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
8
Further, the design of taskJmay be such that
even when sikis released by taskK, it may not
letIrun. [Jruns the codes as if it is in criticalsection all the time after executing DI.] TheJ
action is now as ifJhas higher priority than
I. This is because K, after entering the
critical section and taking the semaphore the
OS let the J run, did not share the priorityinformation about Ithat taskI is of higher
priority thanJ.
Priority Inversion SituationPriority Inversion Situation
7/30/2019 ems reddy
9/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
9
The priority information of another higher-
priority task I should have also been
inherited by K temporarily, if Kwaits forIbutJdoes not andJruns when Khas still not
finished the critical section codes.
This did not happen because the given OS
was such that it didnt provide for temporary
priority inheritance in such situations. Above situation is also called a priority
inversion problem
Priority Inversion SituationPriority Inversion Situation
7/30/2019 ems reddy
10/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
10
Some OSes provide for priority inheritance
in these situations and thus priority
inheritance problem does not occur whenusing them.
A mutex should be a mutually exclusive
Boolean function, by using which the critical
section is protected from interruption in such
a way that the problem of priority inversiondoes not arise.
Mutex is automatically provided in certain
RTOS so that it the priority inversion
OS Provision forOS Provision for temporary priority
inheritance in such situations
7/30/2019 ems reddy
11/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
11
Mutex is automatically provided with
priority inheritance by task on taking it incertain OSes so that it the priority inversion
problem does not arise and certain OSes
provides for selecting priority inheritance aswell as priority sealing options.
OS Provision forOS Provision for temporary priority
inheritance in such situations
7/30/2019 ems reddy
12/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
12
2.2. Deadlock Situation
7/30/2019 ems reddy
13/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
13
Priorities of tasks be such that taskHis of
highest priority, taskIa lower priority and
taskJthe lowest.
Two semaphores, SemTok1 and SemTok2.
TasksIandHhave a shared resourcethrough SemTok1 only.
TasksIandJhave two shared resources
through two semaphores, SemTok1 andSemTok2.
Assume
7/30/2019 ems reddy
14/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
14
At a next instant t1, being now of a
higher priority, the taskHinterrupts thetaskIandJafter it takes the semaphore
SemTok1, and thus blocks bothIandJ.
Deadlock Situation
7/30/2019 ems reddy
15/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
15
In between the time interval t0 and
t1, the SemTok1 was released but
SemTok2 was not released during the
run of taskJ. But the latter did not
matter as the tasksIandJdont shareSemTok2.
Deadlock Situation
7/30/2019 ems reddy
16/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
16
At an instant t2, ifHnow releases the
SemTok1, lets the taskItake it.
Even then it cannot run because it is alsowaiting for taskJto release the SemTok2.
The taskJis waiting at a next instant t3, for
eitherHorIto release the SemTok1 because
it needs this to again enter a critical section
in it. Neither taskIcan run after instant t3 nor
taskJ.
Deadlock Situation
7/30/2019 ems reddy
17/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
17
There is a circular dependency established
betweenIandJ.
Deadlock Situation Solution
7/30/2019 ems reddy
18/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
18
On the interrupt byH, the taskJ,before
exiting from the running state, should have
been put in queue-front so that later on, it
should first take SemTok1, and the taskIput
in queue next for the same token, then the
deadlock would not have occurred
Deadlock Situation solution
7/30/2019 ems reddy
19/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
19
SummarySummary
7/30/2019 ems reddy
20/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
20
We learnt Priority becomes inverted and deadlock
(circular dependency) develops in certain
situations when using semaphores.
Certain OSes provide the solution to this
problem of semaphore use by ensuring thatthese situations do not arise during the
concurrent processing of multitasking
operations .
7/30/2019 ems reddy
21/21
2008
Chapter-7 L11: "Embedded Systems - Architecture,
Programming and Design" , Raj Kamal, Publs.: McGraw-Hill,Inc.
21
End ofEnd ofLesson11 of Chapter 7 onLesson11 of Chapter 7 on
Priority Inversion Problem and DeadlockPriority Inversion Problem and Deadlock
SituationsSituations