ecs150 Fall 2007 : Operating System #3: Priority Inversion (a paper on the class website)

1 / 66

# ecs150 Fall 2007 : Operating System #3: Priority Inversion (a paper on the class website) - PowerPoint PPT Presentation

ecs150 Fall 2007 : Operating System #3: Priority Inversion (a paper on the class website). Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ [email protected] Priority Scheduling.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' ecs150 Fall 2007 : Operating System #3: Priority Inversion (a paper on the class website)' - rhonda

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### ecs150 Fall 2007:Operating System#3: Priority Inversion(a paper on the class website)

Dr. S. Felix Wu

Computer Science Department

University of California, Davis

http://www.cs.ucdavis.edu/~wu/

[email protected]

ecs150, fall 2007

Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority (smallest integer  highest priority).
• Preemptive
• Non-preemptive
• SJF is a priority scheduling scheme where priority is the predicted next CPU burst time.
• FCFS is a priority scheduling scheme where priority is the arrival time.
• Lottery scheduling is a probabilistic priority scheduling scheme where the priority is the ticket number.

ecs150, fall 2007

“Fixed” Priority
• What is it?
• The process sticks with the origin assigned priority.
• A good or bad idea?
• Have we learned the lesson from HW#2?
• What other possible policy?
• Dynamic policy.
• Problem  Starvation – low priority processes may never execute.
• Solution  Aging – as time progresses increase the priority of the process.

ecs150, fall 2007

1

RR

0

0

:

:

.

256 different priorities

64 scheduling classes

1

0

1

ecs150, fall 2007

Real-Time Scheduling
• Hard real-time systems – required to complete a critical task within a guaranteed amount of time.
• Resource reservation- guarantees on time completion or rejects process
• Soft real-time computing – requires that critical processes receive priority over less fortunate ones.

ecs150, fall 2007

Unexpected Effectsbetween two OS control mechanisms
• Real-time priority scheduling
• Responsiveness: if a higher priority thread appears, serve it asap.
• Mutual exclusion
• Integrity: if a higher priority thread wants to enter a critical section being hold by a lower priority thread, it has to wait for the lower priority thread to leave “the critical section”.

ecs150, fall 2007

1

RR

0

0

:

:

.

256 different priorities

64 scheduling classes

1

0

1

ecs150, fall 2007

• Thread τ1 L L L Rx L
• Thread τ2 L L ... L
• Thread τ3 L L L RxL ... L
• L: local CPU burst
• R: resource required (Mutual Exclusion)

ecs150, fall 2007

critical section

Example
• Suppose that threads τ1 and τ3 share some data.
• each task executes the following code:
• do local work (L)
• sem_wait(s) (P(x))
• access shared resource (R)
• sem_signal(s) (V(x))
• do more local work (L)

ecs150, fall 2007

L L L

Blocking

Blocked!

τ1

R L

τ2

τ3

L L L R

R

t+4

t+6

0

t

t+3

ecs150, fall 2007

L L L

Blocked!

τ1

τ2

τ3

L L L R

0

t

t+2

t+3

ecs150, fall 2007

L L L

...

L L

Unbounded Priority Inversion

Blocked!

τ1

R L

τ2

τ3

L L L R

R

t+253

t+254

0

t

t+2

t+3

ecs150, fall 2007

L L L

Unbounded Priority Inversion

Blocked!

τ1

R L

τ2-1

L

τ2-2

L

τ2-n

L

τ3

L L L R

R

t+2530

t+2540

0

t

t+2

t+3

ecs150, fall 2007

The problem..
• Do we have Priority Inversion in the FreeBSD kernel with XYZ scheduling policy?
• As long as we have priority and mutual exclusion at the same time, we will have some form of priority inversion.
• How to resolve it? trade-off?

ecs150, fall 2007

L L L

R

dynamic 3 = 1

Priority Inheritance

Blocked!

τ1

R L

L ... L

τ2

τ3

L L L R

L ... L

0

t

t+2

t+3

t+4

t+6

ecs150, fall 2007

Priority Inheritance Protocols
• L. Sha, R. Rajkumar, J. Lehoczky, “Priority Inheritance Protocols: An Approach to Real-Time Synchronization”, IEEE Transactions on Computers, Vol. 39, No. 9, pp. 1175-1185, 1990

ecs150, fall 2007

The MARS Pathfinder Problem
• “But a few days into the mission, not long after Pathfinder started gathering meteorological data, the spacecraft began experiencing total system resets, each resulting in losses of data. The press reported these failures in terms such as "software glitches" and "the computer was trying to do too many things at once".” …

ecs150, fall 2007

The MARS Pathfinder Problem
• “VxWorks provides preemptive priority scheduling of threads. Tasks on the Pathfinder spacecraft were executed as threads with priorities that were assigned in the usual manner reflecting the relative urgency of these tasks.”
• “Pathfinder contained an "information bus", which you can think of as a shared memory area used for passing information between different components of the spacecraft. A bus management task ran frequently with high priority to move certain kinds of data in and out of the information bus. Access to the bus was synchronized withmutual exclusion locks (mutexes).”

ecs150, fall 2007

High priority: retrieval of data from shared memory

Low priority: thread collecting meteorological data

“The meteorological data gathering task ran as an infrequent, low priority thread, and used the information bus to publish its data. When publishing its data, it would acquire a mutex, do writes to the bus, and release the mutex. If an interrupt caused the information bus thread to be scheduled while this mutex was held, and if the information bus thread then attempted to acquire this same mutex in order to retrieve published data, this would cause it to block on the mutex, waiting until the meteorological thread released the mutex before it could continue. The spacecraft also contained a communications task that ran with medium priority.”

ecs150, fall 2007

“Most of the time this combination worked fine. However, very infrequently it was possible for an interrupt to occur that caused the (medium priority) communications task to be scheduled during the short interval while the (high priority) information bus thread was blocked waiting for the (low priority) meteorological data thread. In this case, the long-running communications task, having higher priority than the meteorological task, would prevent it from running, consequently preventing the blocked information bus task from running. After some time had passed, a watchdog timer would go off, notice that the data bus task had not been executed for some time, conclude that something had gone drastically wrong, and initiate a total system reset. This scenario is a classic case of priority inversion.”

ecs150, fall 2007

Priority inheritance also solved the Mars Pathfinder problem: the VxWorks operating system used in the pathfinder implements a flag for the calls to mutex primitives. This flag allows priority inheritance to be set to “on”. When the software was shipped, it was set to “off”.

The problem on Mars was corrected by using the debugging facilities of VxWorks to change the flag to “on”, while the Pathfinder was already on the Mars [Jones, 1997].

ecs150, fall 2007

Basic Priority Inheritance
• For each resource (semaphore), a list of blocked threads must be stored in a priority queue.
• A thread τi uses its assigned priority, unless it is in its critical section and blocks some higher priority threads, in which case, thread τi uses ( inherits ) the highest dynamic priority of all the threads it blocks.
• Priority inheritance is transitive; that is, if thread τi blocks τj and τj blocks τk , then τi can inherit the priority of τk.

ecs150, fall 2007

mutex priority inheritance

waiting queue

t

t

t

t

priority

ecs150, fall 2007

waiting queue

M1

t

t

t

priority

t

waiting queue

M2

t

t

t

priority

ecs150, fall 2007

Transitive Priority

waiting queue

waiting queue

M2

M1

t

t

t

t

t

t

t

priority

priority

ecs150, fall 2007

Types of Blocking
• Direct - thread τ1 and τ2 use a shared resource. If the low priority thread is in its critical section, then it directly blocks the high priority thread.
• Indirect (push-through) - if a low priority thread inherits the priority of a high priority thread, a medium priority thread can be blocked while the low priority thread is in its critical section.

ecs150, fall 2007

Properties of Priority Inheritance
• Under the basic priority inheritance protocol, if there are m semaphores that can block a thread J, then J can be blocked at most m times; i.e., on each semaphore at most once.

ecs150, fall 2007

Problems
• The Basic Priority Inheritance Protocol has two problems:
• Deadlock - two threads need to access a pair of shared resources simultaneously. If the resources, say A and B, are accessed in opposite orders by each thread, then deadlock may occur.
• Blocking Chain - the blocking duration is bounded (by at most the sum of critical section times), but that may be substantial.

ecs150, fall 2007

waiting queue

M1

t

t

t

t

priority

waiting queue

M2

t

t

t

t

priority

ecs150, fall 2007

Blocking Chain Example

starting time

• Task 1 : L R2 L R3 L R4L ... L Rn L, 2(n-1)
• Task 2 : L R2 R2, 2(n-2)
• Task 3 : L R3 R3, 2(n-3)
• Task 4 : L R4 R4, 2(n-4)
• ...
• Task n-1 : L Rn-1 Rn-1, 2(n-(n-1))
• Task n : L Rn Rn, 2(n-n)

ecs150, fall 2007

L

R2 L

L R2

Rn

R2

Blocking Chain

Blocked!

Blocked!

τ1

Rn L

τ2

τn

L Rn

0

ecs150, fall 2007

L

R2 L

L R2

Rn

R2

Different Timing?

Blocked!

Blocked!

τ1

Rn L

τ2

τn

L Rn

???

0

ecs150, fall 2007

Different Timing??

starting time

• Task 1 : L R2 L R3 L R4L ... L Rn L, 2(n-1)
• Task 2 : L R2 R2, 2(n-n)
• Task 3 : L R3 R3, 2(n-(n-1))
• Task 4 : L R4 R4, 2(n-(n-2))
• ...
• Task n-1 : L Rn-1 Rn-1, 2(n-3)
• Task n : L Rn Rn, 2(n-2)

How many times Task 1 will be blocked?

ecs150, fall 2007

Priority Ceiling Protocols (PCP)
• A higher priority thread can be blocked at most once, in its life time,by one lower priority thread.
• Transitive inheritance is prevented.
• Are they really critical?

ecs150, fall 2007

PCP
• How do we accomplish these goals intuitively?

ecs150, fall 2007

Locking a Mutex
• If the “mutex M” is available and “thread T” needs it , should T lock it?

t

Mutex??

Mutex + Priority Inheritance??

ecs150, fall 2007

Risk for Locking a Mutex
• If the “mutex M” is available and “thread T” needs it , should T lock it?

Checking before Locking it!!

We don’t know whether the high priority thread will occur in the next X seconds!

But, does it matter?

t

t

ecs150, fall 2007

“Checking” What??

ecs150, fall 2007

“Checking” What??
• What is our goal?
• High priority thread will be blocked at most once.
• We will allow blocking ONCE.
• Idea of the check:
• If we are the first Mutex, we lock it.
• If we are not the first, we will not.
• But, how to design/implement this idea?

ecs150, fall 2007

“Checking” What??
• Idea of the check:
• If we are the first Mutex, we lock it.
• If we are not the first, we will not.
• But, why wait?
• We wait for a good reason
• If we lock it and some higher priority threads might be blocked more than ONCE, then we better wait twice…
• But, how do we know that this is the case?
• How do we know whether the high priority thread will show up?

ecs150, fall 2007

PCP
• The protocol uses the notion of a system-wide mutex ceiling priority.
• Each thread has a static default priority assigned.
• Each resource (mutex) has a static ceiling priority defined to be the maximum static priority of any thread that uses it.
• Each thread has a dynamic priority equal to the maximum of its own default priority and any priority it inherits due to blocking a higher priority thread.

ecs150, fall 2007

mutex priority ceiling

potential customers

PC

t

t

t

priority

A preventive action

(could be unnecessary though)

ecs150, fall 2007

PCP
• At run-time, if a thread wants to lock a mutex, its priority must be strictly higher than the ceilings of all mutexes currently locked by other threads (unless it is the thread holding the lock on the mutex with the highest ceiling).
• If this condition is not satisfied, then the thread is blocked.
• When a thread is blocked on a mutex, the thread currently holding the waited mutex inherits the priority of the blocked thread.

ecs150, fall 2007

PC

PC

PC

PC

PC

PC

PC

PC

Priority CeilingShould I get it?

2

???

4

MaxPC

value

2

4

locked

unlocked

ecs150, fall 2007

PC

PC

PC

PC

PC

PC

PC

PC

Mutex/PIPGet it as long as it is available!

1

YES

4

MaxPC

value

2

4

locked

unlocked

ecs150, fall 2007

PC

PC

PC

PC

PC

PC

PC

PC

PCPNot so Fast

1

NO

4

MaxPC

value

2

4

locked

unlocked

ecs150, fall 2007

PC

PC

PC

PC

PC

PC

PC

PC

PCPNot so Fast

2

NO

4

MaxPC

value

3

4

locked

unlocked

ecs150, fall 2007

PC

PC

PC

PC

PC

PC

PC

PC

2

??

4

MaxPC

value

3

4

locked

unlocked

ecs150, fall 2007

PC

PC

PC

PC

PC

PC

PC

PC

PCP mutex blocking

??

MaxPC

value

locked

unlocked

ecs150, fall 2007

Are we sure about the claim of PCP?
• A higher priority thread can be blocked at most once, in its life time,by one lower priority thread.
• Try to find a “Counter Example” to show that PCP’s claim is FALSE!!

ecs150, fall 2007

1

2

3

4

0

2

4

6

8

10

12

14

16

18

Preempted

Executing

Executing with Q locked

Blocked

Executing with V locked

Ceiling-driven Indirectly

Blocked

ecs150, fall 2007

Priority Inversion

1

2

3

4

0

2

4

6

8

10

12

14

16

18

Preempted

Executing

Executing with Q locked

Blocked

Executing with V locked

Ceiling-driven Indirectly

Blocked

ecs150, fall 2007

Priority Inversion Area

1

2

3

4

0

2

4

6

8

10

12

14

16

18

Preempted

Executing

Executing with Q locked

Blocked

Executing with V locked

Ceiling-driven Indirectly

Blocked

ecs150, fall 2007

Basic Priority Inheritance

Process

1

2

1

3

4

1

1

1

0

2

4

6

8

10

12

14

16

18

ecs150, fall 2007

Priority Ceiling

1

1

Process

1

2

3

4

2

2

2

1

1

0

2

4

6

8

10

12

14

16

18

locked

ecs150, fall 2007

Priority Ceiling (1 delay)

1

1

Process

1

2

3

4

2

2

2

2

1

0

2

4

6

8

10

12

14

16

18

locked

ecs150, fall 2007

Can we do better??

ecs150, fall 2007

Priority Ceiling Emulation

1

1

process

d

c

1

1

b

a

1

1

1

1

0

2

4

6

8

10

12

14

16

18

ecs150, fall 2007

Priority Ceiling Emulation
• Each thread has a static (base) default priority assigned (perhaps by the deadline monotonic scheme).
• Each resource has a static ceiling value defined, this is the maximum priority of the threads that use it.
• A thread has a dynamic (active) priority that is the maximum of its own static priority and the ceiling values of any resources it has locked
• As a consequence, a thread will only suffer a block at the very beginning of its execution
• Once the thread starts actually executing, all the resources it needs must be free; if they were not, then some thread would have an equal or higher priority and the thread’s execution would be postponed

ecs150, fall 2007

Summary
• Priority Inversion
• Basic Priority Inheritance
• Priority Ceiling
• Upgraded when a higher priority task (might not be the same as the ceiling value) is blocked due to the Ceiling value
• Priority Ceiling Emulation
• Immediately upgraded to the ceiling value after obtaining the lock

ecs150, fall 2007

s3

s2

s1

Semaphore Requirements
• Threads must lock and unlock semaphores in a “nested” or “pyramid” fashion:
• Let P(S) = L(S) = lock(S) = sem_wait(S).
• Let V(S) = U(S) = unlock(S) = sem_signal(S).
• Example: P(s1);P(s2);P(s3);...;V(s3);V(s2);V(s1);

ecs150, fall 2007

J, Pri(J) = 1

I, Pri(I) = 120

J,80%

I

J

I

I

J

J

J

I

J

J

J

J

J

J

J

I

J

J

J

J

I

J

J

ecs150, fall 2007

Lottery Ticket Sharing
• If task I is blocked by task J, all I’s tickets will be used by J until the block is gone.

ecs150, fall 2007

LT Sharing vs. PI
• LTS: summation of tickets for ALL waiting processes.
• With Lottery Tickets, we can ADD “priority quantities” together.
• PI: the highest priority among ALL waiting processes.
• What is the semantic meaning of “adding” priorities together?

ecs150, fall 2007