Download presentation
Presentation is loading. Please wait.
1
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20071 Operating System 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/ sfelixwu@gmail.com
2
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20072 Priority Scheduling l A priority number (integer) is associated with each process l The CPU is allocated to the process with the highest priority (smallest integer highest priority). –Preemptive –Non-preemptive l SJF is a priority scheduling scheme where priority is the predicted next CPU burst time. l FCFS is a priority scheduling scheme where priority is the arrival time. l Lottery scheduling is a probabilistic priority scheduling scheme where the priority is the ticket number.
3
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20073 “Fixed” Priority l What is it? –The process sticks with the origin assigned priority. l A good or bad idea? –Have we learned the lesson from HW#2? l What other possible policy? –Dynamic policy. l Problem Starvation – low priority processes may never execute. l Solution Aging – as time progresses increase the priority of the process.
4
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20074 1 0 0 1 0 1 ::.::. 256 different priorities 64 scheduling classes RR
5
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20075 Real-Time Scheduling l 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 l Soft real-time computing – requires that critical processes receive priority over less fortunate ones.
6
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20076 Unexpected Effects between two OS control mechanisms l Real-time priority scheduling –Responsiveness: if a higher priority thread appears, serve it asap. l 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”.
7
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20077 pthread_mutex_lock pthread_mutex_unlock 1 0 0 1 0 1 ::.::. 256 different priorities 64 scheduling classes RR
8
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20078
9
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 20079 Real-Time Threads l Thread τ 1 L L L R x L l Thread τ 2 L L... L l Thread τ 3 L L L R x L... L l L: local CPU burst l R: resource required (Mutual Exclusion)
10
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200710 Example l Suppose that threads τ 1 and τ 3 share some data. l Access to the data is restricted using semaphore x: –each task executes the following code: l do local work (L) l sem_wait(s) (P(x)) –access shared resource (R) l sem_signal(s) (V(x)) l do more local work (L)
11
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200711Blocking τ 2 τ 3 t0t+3 t+4 R L L L R R L τ 1 t+6 L L L Blocked!
12
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200712 The middle thread τ 2 τ 3 t0t+3 L L L R τ 1 L L L Blocked! t+2
13
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200713 Unbounded Priority Inversion τ 2 τ 3 t0t+3 t+253 RL L L R R L τ 1 t+254 L L L... L Blocked! t+2
14
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200714 Unbounded Priority Inversion τ 2-1 τ 3 t0t+3 t+2530 RL L L R R L τ 1 t+2 t+2540 L L L L Blocked! τ 2-2 τ 2-n L L
15
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200715 The problem.. l Do we have Priority Inversion in the FreeBSD kernel with XYZ scheduling policy? l As long as we have priority and mutual exclusion at the same time, we will have some form of priority inversion. l How to resolve it? trade-off?
16
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200716 Priority Inheritance τ 2 τ 3 t0t+3t+4 L... L L L L R R L τ 1 t+2t+6 L L L R Blocked! dynamic 3 = 1 L... L
17
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200717 Priority Inheritance Protocols l 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
18
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200718 The MARS Pathfinder Problem l “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".” …
19
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200719 The MARS Pathfinder Problem l “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.” l “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 with mutual exclusion locks (mutexes).”
20
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200720 “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.” High priority: retrieval of data from shared memory Medium priority: communications task Low priority: thread collecting meteorological data
21
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200721 l “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.”
22
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200722 l 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].
23
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200723 Basic Priority Inheritance l For each resource (semaphore), a list of blocked threads must be stored in a priority queue. l 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. l Priority inheritance is transitive; that is, if thread τ i blocks τ j and τ j blocks τ k, then τ i can inherit the priority of τ k.
24
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200724 mutex priority inheritance pthread_mutex_lock pthread_mutex_unlock ttt waiting queue t priority
25
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200725 pthread_mutex_lock pthread_mutex_unlock M1M1 ttt waiting queue t priority pthread_mutex_lock pthread_mutex_unlock M2M2 ttt waiting queue priority
26
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200726 Transitive Priority pthread_mutex_lock pthread_mutex_unlock M1M1 ttt waiting queue t priority pthread_mutex_lock pthread_mutex_unlock M2M2 tt waiting queue t priority
27
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200727 Types of Blocking l 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. l 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.
28
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200728 Properties of Priority Inheritance l 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.
29
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200729 Any Problems with the basic Priority Inheritance Protocol? l ???
30
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200730Problems l 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.
31
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200731 pthread_mutex_lock pthread_mutex_unlock M1M1 ttt waiting queue t priority pthread_mutex_lock pthread_mutex_unlock M2M2 ttt waiting queue priority t
32
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200732 Blocking Chain Example l Task 1 : L R 2 L R 3 L R 4 L... L R n L,2(n-1) l Task 2 : L R 2 R 2, 2(n-2) l Task 3 : L R 3 R 3, 2(n-3) l Task 4 : L R 4 R 4, 2(n-4) l... l Task n-1 : L R n-1 R n-1, 2(n-(n-1)) l Task n : L R n R n, 2(n-n) starting time
33
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200733 Blocking Chain τ 2 τ n 0 L R n τ 1 RnRn R n L L R 2 R2R2 Blocked! L R 2 L Blocked!
34
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200734 Different Timing? τ 2 τ n 0 L R n τ 1 RnRn R n L L R 2 R2R2 Blocked! L R 2 L Blocked! ???
35
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200735 Different Timing?? l Task 1 : L R 2 L R 3 L R 4 L... L R n L,2(n-1) l Task 2 : L R 2 R 2, 2(n-n) l Task 3 : L R 3 R 3, 2(n-(n-1)) l Task 4 : L R 4 R 4, 2(n-(n-2)) l... l Task n-1 : L R n-1 R n-1, 2(n-3) l Task n : L R n R n, 2(n-2) starting time How many times Task 1 will be blocked?
36
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200736 Priority Ceiling Protocols (PCP) l A higher priority thread can be blocked at most once, in its life time, by one lower priority thread. l Deadlocks are prevented/avoided (?!). l Transitive inheritance is prevented. l Are they really critical?
37
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200737 PCP l How do we accomplish these goals intuitively?
38
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200738 Locking a Mutex l If the “mutex M” is available and “thread T” needs it, should T lock it? pthread_mutex_lock pthread_mutex_unlock t Mutex?? Mutex + Priority Inheritance??
39
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200739 Risk for Locking a Mutex l If the “mutex M” is available and “thread T” needs it, should T lock it? pthread_mutex_lock pthread_mutex_unlock t t Checking before Locking it!! We don’t know whether the high priority thread will occur in the next X seconds! But, does it matter?
40
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200740 “Checking” What??
41
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200741 “Checking” What?? l What is our goal? –High priority thread will be blocked at most once. –We will allow blocking ONCE. l Idea of the check: –If we are the first Mutex, we lock it. –If we are not the first, we will not. l But, how to design/implement this idea?
42
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200742 “Checking” What?? l 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? l 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?
43
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200743 PCP l The protocol uses the notion of a system-wide mutex ceiling priority. l Each thread has a static default priority assigned. l Each resource (mutex) has a static ceiling priority defined to be the maximum static priority of any thread that uses it. l 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.
44
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200744 mutex priority ceiling pthread_mutex_lock pthread_mutex_unlock ttt potential customers PC priority A preventive action (could be unnecessary though)
45
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200745PCP l 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). l If this condition is not satisfied, then the thread is blocked. l When a thread is blocked on a mutex, the thread currently holding the waited mutex inherits the priority of the blocked thread.
46
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200746 Priority Ceiling Should I get it? PC locked unlocked MaxPC value thread t 2 ??? 4 thread t 4 2 thread t 3 2 4
47
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200747 Mutex/PIP Get it as long as it is available! PC locked unlocked MaxPC value thread t 2 YES 4 thread t 4 1 thread t 3 2 4
48
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200748 PCP Not so Fast PC locked unlocked MaxPC value thread t 2 NO 4 thread t 4 1 thread t 3 2 4
49
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200749 PCP Not so Fast PC locked unlocked MaxPC value thread t 2 NO 4 thread t 4 2 thread t 3 3 4
50
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200750 PCP How about??? PC locked unlocked MaxPC value thread t 2 ?? 4 thread t 4 2 thread t 2 3 4 thread t 20 thread t 11
51
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200751 PCP mutex blocking PC locked unlocked MaxPC value thread t 1 ?? thread t 2 thread t 3
52
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200752 Are we sure about the claim of PCP? l A higher priority thread can be blocked at most once, in its life time, by one lower priority thread. l Deadlocks are prevented/avoided. l Try to find a “Counter Example” to show that PCP’s claim is FALSE!!
53
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200753 4 3 2 1 024681012141618 Executing Executing with Q locked Preempted Executing with V locked Blocked Tasks Ceiling-driven Indirectly Blocked
54
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200754 4 3 2 1 024681012141618 Executing Executing with Q locked Preempted Executing with V locked Blocked Priority Inversion Ceiling-driven Indirectly Blocked
55
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200755 4 3 2 1 024681012141618 Executing Executing with Q locked Preempted Executing with V locked Blocked Priority Inversion Area Ceiling-driven Indirectly Blocked
56
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200756 111 4 3 2 1 024681012141618 Process 1 Basic Priority Inheritance
57
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200757 2212 4 3 2 1 024681012141618 Process 1 Priority Ceiling 11 locked
58
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200758 2222 4 3 2 1 024681012141618 Process 1 Priority Ceiling (1 delay) 11 locked
59
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200759 Can we do better??
60
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200760 1111 a b c d 024681012141618 process 11 Priority Ceiling Emulation 11
61
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200761 Priority Ceiling Emulation l Each thread has a static (base) default priority assigned (perhaps by the deadline monotonic scheme). l Each resource has a static ceiling value defined, this is the maximum priority of the threads that use it. l 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 l As a consequence, a thread will only suffer a block at the very beginning of its execution l 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
62
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200762 Summary l Priority Inversion l Basic Priority Inheritance l Priority Ceiling –Upgraded when a higher priority task (might not be the same as the ceiling value) is blocked due to the Ceiling value l Priority Ceiling Emulation –Immediately upgraded to the ceiling value after obtaining the lock
63
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200763 Semaphore Requirements l 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(s 1 );P(s 2 );P(s 3 );...;V(s 3 );V(s 2 );V(s 1 ); s 1 s 2 s 3
64
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200764 J, Pri(J) = 1I, Pri(I) = 120 J,80%IJI IJJJIJJJJJJJIJJJJIJJ
65
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200765 Lottery Ticket Sharing l If task I is blocked by task J, all I’s tickets will be used by J until the block is gone.
66
UCDavis, ecs150 Fall 2007 10/23/2007ecs150, fall 200766 LT Sharing vs. PI l LTS: summation of tickets for ALL waiting processes. –With Lottery Tickets, we can ADD “priority quantities” together. l PI: the highest priority among ALL waiting processes. –What is the semantic meaning of “adding” priorities together?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.