UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall “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.
UCDavis, ecs150 Fall /23/2007ecs150, fall ::.::. 256 different priorities 64 scheduling classes RR
UCDavis, ecs150 Fall /23/2007ecs150, fall 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall 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”.
UCDavis, ecs150 Fall /23/2007ecs150, fall pthread_mutex_lock pthread_mutex_unlock ::.::. 256 different priorities 64 scheduling classes RR
UCDavis, ecs150 Fall /23/2007ecs150, fall 20078
UCDavis, ecs150 Fall /23/2007ecs150, fall 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)
UCDavis, ecs150 Fall /23/2007ecs150, fall 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)
UCDavis, ecs150 Fall /23/2007ecs150, fall Blocking τ 2 τ 3 t0t+3 t+4 R L L L R R L τ 1 t+6 L L L Blocked!
UCDavis, ecs150 Fall /23/2007ecs150, fall The middle thread τ 2 τ 3 t0t+3 L L L R τ 1 L L L Blocked! t+2
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall 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?
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall 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 , 1990
UCDavis, ecs150 Fall /23/2007ecs150, fall 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".” …
UCDavis, ecs150 Fall /23/2007ecs150, fall 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).”
UCDavis, ecs150 Fall /23/2007ecs150, fall “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
UCDavis, ecs150 Fall /23/2007ecs150, fall 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.”
UCDavis, ecs150 Fall /23/2007ecs150, fall 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].
UCDavis, ecs150 Fall /23/2007ecs150, fall 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall mutex priority inheritance pthread_mutex_lock pthread_mutex_unlock ttt waiting queue t priority
UCDavis, ecs150 Fall /23/2007ecs150, fall pthread_mutex_lock pthread_mutex_unlock M1M1 ttt waiting queue t priority pthread_mutex_lock pthread_mutex_unlock M2M2 ttt waiting queue priority
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall Any Problems with the basic Priority Inheritance Protocol? l ???
UCDavis, ecs150 Fall /23/2007ecs150, fall Problems 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall pthread_mutex_lock pthread_mutex_unlock M1M1 ttt waiting queue t priority pthread_mutex_lock pthread_mutex_unlock M2M2 ttt waiting queue priority t
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall Blocking Chain τ 2 τ n 0 L R n τ 1 RnRn R n L L R 2 R2R2 Blocked! L R 2 L Blocked!
UCDavis, ecs150 Fall /23/2007ecs150, fall Different Timing? τ 2 τ n 0 L R n τ 1 RnRn R n L L R 2 R2R2 Blocked! L R 2 L Blocked! ???
UCDavis, ecs150 Fall /23/2007ecs150, fall 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?
UCDavis, ecs150 Fall /23/2007ecs150, fall 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?
UCDavis, ecs150 Fall /23/2007ecs150, fall PCP l How do we accomplish these goals intuitively?
UCDavis, ecs150 Fall /23/2007ecs150, fall 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??
UCDavis, ecs150 Fall /23/2007ecs150, fall 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?
UCDavis, ecs150 Fall /23/2007ecs150, fall “Checking” What??
UCDavis, ecs150 Fall /23/2007ecs150, fall “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?
UCDavis, ecs150 Fall /23/2007ecs150, fall “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?
UCDavis, ecs150 Fall /23/2007ecs150, fall 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall mutex priority ceiling pthread_mutex_lock pthread_mutex_unlock ttt potential customers PC priority A preventive action (could be unnecessary though)
UCDavis, ecs150 Fall /23/2007ecs150, fall PCP 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall Priority Ceiling Should I get it? PC locked unlocked MaxPC value thread t 2 ??? 4 thread t 4 2 thread t 3 2 4
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall PCP Not so Fast PC locked unlocked MaxPC value thread t 2 NO 4 thread t 4 1 thread t 3 2 4
UCDavis, ecs150 Fall /23/2007ecs150, fall PCP Not so Fast PC locked unlocked MaxPC value thread t 2 NO 4 thread t 4 2 thread t 3 3 4
UCDavis, ecs150 Fall /23/2007ecs150, fall PCP How about??? PC locked unlocked MaxPC value thread t 2 ?? 4 thread t 4 2 thread t thread t 20 thread t 11
UCDavis, ecs150 Fall /23/2007ecs150, fall PCP mutex blocking PC locked unlocked MaxPC value thread t 1 ?? thread t 2 thread t 3
UCDavis, ecs150 Fall /23/2007ecs150, fall 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!!
UCDavis, ecs150 Fall /23/2007ecs150, fall Executing Executing with Q locked Preempted Executing with V locked Blocked Tasks Ceiling-driven Indirectly Blocked
UCDavis, ecs150 Fall /23/2007ecs150, fall Executing Executing with Q locked Preempted Executing with V locked Blocked Priority Inversion Ceiling-driven Indirectly Blocked
UCDavis, ecs150 Fall /23/2007ecs150, fall Executing Executing with Q locked Preempted Executing with V locked Blocked Priority Inversion Area Ceiling-driven Indirectly Blocked
UCDavis, ecs150 Fall /23/2007ecs150, fall Process 1 Basic Priority Inheritance
UCDavis, ecs150 Fall /23/2007ecs150, fall Process 1 Priority Ceiling 11 locked
UCDavis, ecs150 Fall /23/2007ecs150, fall Process 1 Priority Ceiling (1 delay) 11 locked
UCDavis, ecs150 Fall /23/2007ecs150, fall Can we do better??
UCDavis, ecs150 Fall /23/2007ecs150, fall a b c d process 11 Priority Ceiling Emulation 11
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall 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
UCDavis, ecs150 Fall /23/2007ecs150, fall J, Pri(J) = 1I, Pri(I) = 120 J,80%IJI IJJJIJJJJJJJIJJJJIJJ
UCDavis, ecs150 Fall /23/2007ecs150, fall 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.
UCDavis, ecs150 Fall /23/2007ecs150, fall 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?