660 likes | 809 Views
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. Priority Scheduling.
E N D
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 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
pthread_mutex_lock pthread_mutex_unlock 1 RR 0 0 : : . 256 different priorities 64 scheduling classes 1 0 1 ecs150, fall 2007
Real-Time Threads • 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. • Access to the data is restricted using semaphore x: • 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 The middle thread 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 Medium priority: communications task 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
pthread_mutex_lock pthread_mutex_unlock mutex priority inheritance waiting queue t t t t priority ecs150, fall 2007
pthread_mutex_lock waiting queue M1 t t t priority pthread_mutex_unlock t pthread_mutex_lock waiting queue M2 t t t priority pthread_mutex_unlock ecs150, fall 2007
Transitive Priority pthread_mutex_lock pthread_mutex_lock waiting queue waiting queue M2 M1 t t t t t t t priority priority pthread_mutex_unlock pthread_mutex_unlock 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
Any Problems with the basic Priority Inheritance Protocol? • ??? 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
pthread_mutex_lock waiting queue M1 t t t t priority pthread_mutex_unlock pthread_mutex_lock waiting queue M2 t t t t priority pthread_mutex_unlock 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. • Deadlocks are prevented/avoided (?!). • 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?? pthread_mutex_lock pthread_mutex_unlock 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 pthread_mutex_lock t pthread_mutex_unlock 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
pthread_mutex_lock pthread_mutex_unlock 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? thread t3 2 thread t2 ??? 4 thread t4 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! thread t3 1 thread t2 YES 4 thread t4 MaxPC value 2 4 locked unlocked ecs150, fall 2007
PC PC PC PC PC PC PC PC PCPNot so Fast thread t3 1 thread t2 NO 4 thread t4 MaxPC value 2 4 locked unlocked ecs150, fall 2007
PC PC PC PC PC PC PC PC PCPNot so Fast thread t3 2 thread t2 NO 4 thread t4 MaxPC value 3 4 locked unlocked ecs150, fall 2007
PC PC PC PC PC PC PC PC PCPHow about??? thread t2 2 thread t2 ?? 4 thread t4 MaxPC value 3 thread t11 4 thread t20 locked unlocked ecs150, fall 2007