UCDavis ecs 251 Fall 2007 Operating System Models

  • Slides: 54
Download presentation
UCDavis, ecs 251 Fall 2007 : Operating System Models ecs 251 Fall 2007 #3:

UCDavis, ecs 251 Fall 2007 : Operating System Models ecs 251 Fall 2007 #3: Priority Inversion Dr. S. Felix Wu Computer Science Department University of California, Davis http: //www. cs. ucdavis. edu/~wu/ sfelixwu@gmail. com 10/23/2007 ecs 251, fall 2007 1

UCDavis, ecs 251 Fall 2007 Unexpected Effects between two OS control mechanisms l Real-time

UCDavis, ecs 251 Fall 2007 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”. 10/23/2007 ecs 251, fall 2007 2

UCDavis, ecs 251 Fall 2007 1 RR 0 0 lock unlock : : .

UCDavis, ecs 251 Fall 2007 1 RR 0 0 lock unlock : : . 256 different priorities 64 scheduling classes 1 0 1 10/23/2007 ecs 251, fall 2007 3

UCDavis, ecs 251 Fall 2007 Real-Time Threads Thread τ1 l Thread τ2 l Thread

UCDavis, ecs 251 Fall 2007 Real-Time Threads Thread τ1 l Thread τ2 l Thread τ3 l L L L Rx L L L. . . L L Rx L. . . L L: local CPU burst l R: resource required (Mutual Exclusion) l 10/23/2007 ecs 251, fall 2007 4

UCDavis, ecs 251 Fall 2007 Example l l Suppose that threads τ1 and τ3

UCDavis, ecs 251 Fall 2007 Example l l Suppose that threads τ1 and τ3 share some data. Access to the data is restricted using semaphore x: – each task executes the following code: l l do local work (L) sem_wait(s) (P(x)) – access shared resource (R) l l 10/23/2007 sem_signal(s) (V(x)) do more local work (L) ecs 251, fall 2007 5

UCDavis, ecs 251 Fall 2007 Blocking Blocked! τ1 L L L R L τ2

UCDavis, ecs 251 Fall 2007 Blocking Blocked! τ1 L L L R L τ2 τ3 LLLR 0 10/23/2007 LLL R t t+3 ecs 251, fall 2007 t+4 t+6 6

UCDavis, ecs 251 Fall 2007 The middle thread Blocked! τ1 L L L τ2

UCDavis, ecs 251 Fall 2007 The middle thread Blocked! τ1 L L L τ2 τ3 LLLR 0 10/23/2007 t t+2 t+3 ecs 251, fall 2007 7

UCDavis, ecs 251 Fall 2007 Unbounded Priority Inversion Blocked! τ1 L L L τ2

UCDavis, ecs 251 Fall 2007 Unbounded Priority Inversion Blocked! τ1 L L L τ2 τ3 L L. . . L LLLR 0 10/23/2007 R R t t+2 t+3 ecs 251, fall 2007 t+253 t+254 8

UCDavis, ecs 251 Fall 2007 Unbounded Priority Inversion Blocked! τ1 L L L τ2

UCDavis, ecs 251 Fall 2007 Unbounded Priority Inversion Blocked! τ1 L L L τ2 -1 τ2 -2 R L L τ2 -n τ3 L L L R 0 10/23/2007 L L R t t+2 t+3 ecs 251, fall 2007 t+2530 t+2540 9

UCDavis, ecs 251 Fall 2007 The problem. . l As long as we have

UCDavis, ecs 251 Fall 2007 The problem. . 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? 10/23/2007 ecs 251, fall 2007 10

UCDavis, ecs 251 Fall 2007 Priority Inheritance Blocked! τ1 L L L R L

UCDavis, ecs 251 Fall 2007 Priority Inheritance Blocked! τ1 L L L R L L. . . L τ2 dynamic 3 = 1 τ3 R LLLR 0 10/23/2007 t t+2 t+3 t+4 ecs 251, fall 2007 L. . . L t+6 11

UCDavis, ecs 251 Fall 2007 Priority Inheritance Protocols l L. Sha, R. Rajkumar, J.

UCDavis, ecs 251 Fall 2007 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 10/23/2007 ecs 251, fall 2007 12

UCDavis, ecs 251 Fall 2007 High priority: retrieval of data from shared memory Medium

UCDavis, ecs 251 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. ” 10/23/2007 ecs 251, fall 2007 13

UCDavis, ecs 251 Fall 2007 Basic Priority Inheritance l l l For each resource

UCDavis, ecs 251 Fall 2007 Basic Priority Inheritance l l l 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. 10/23/2007 ecs 251, fall 2007 14

UCDavis, ecs 251 Fall 2007 Mutex Priority Inheritance pthread_mutex_lock waiting queue t t pthread_mutex_unlock

UCDavis, ecs 251 Fall 2007 Mutex Priority Inheritance pthread_mutex_lock waiting queue t t pthread_mutex_unlock 10/23/2007 ecs 251, fall 2007 priority 15

UCDavis, ecs 251 Fall 2007 pthread_mutex_lock waiting queue M 1 t t t priority

UCDavis, ecs 251 Fall 2007 pthread_mutex_lock waiting queue M 1 t t t priority pthread_mutex_unlock t pthread_mutex_lock waiting queue M 2 pthread_mutex_unlock 10/23/2007 ecs 251, fall 2007 t t t priority 16

UCDavis, ecs 251 Fall 2007 Transitive Priority pthread_mutex_lock waiting queue t M 2 t

UCDavis, ecs 251 Fall 2007 Transitive Priority pthread_mutex_lock waiting queue t M 2 t t t waiting queue M 1 priority pthread_mutex_unlock 10/23/2007 t t t priority pthread_mutex_unlock ecs 251, fall 2007 17

UCDavis, ecs 251 Fall 2007 l Problems The Basic Priority Inheritance Protocol has two

UCDavis, ecs 251 Fall 2007 l 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. 10/23/2007 ecs 251, fall 2007 18

UCDavis, ecs 251 Fall 2007 Blocking Chain Example starting time l l l l

UCDavis, ecs 251 Fall 2007 Blocking Chain Example starting time l l l l Task. . . Task 1 2 3 4 : : L L R 2 R 3 R 4 L R 3 L R 4 L. . . L Rn L, R 2, R 3, R 4, n-1 : L Rn-1, n : L Rn Rn, 10/23/2007 2(n-1) 2(n-2) 2(n-3) 2(n-4) 2(n-(n-1)) 2(n-n) ecs 251, fall 2007 19

UCDavis, ecs 251 Fall 2007 Blocking Chain Blocked! τ1 L τ2 L R 2

UCDavis, ecs 251 Fall 2007 Blocking Chain Blocked! τ1 L τ2 L R 2 τn Blocked! R 2 L Rn L R 2 Rn L Rn 0 10/23/2007 ecs 251, fall 2007 20

UCDavis, ecs 251 Fall 2007 Priority Ceiling Protocols (PCP) A higher priority thread can

UCDavis, ecs 251 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. l Deadlocks are prevented/avoided (? !). l Transitive inheritance is prevented. l l Are they really critical? 10/23/2007 ecs 251, fall 2007 21

UCDavis, ecs 251 Fall 2007 PCP l How do we accomplish these goals intuitively?

UCDavis, ecs 251 Fall 2007 PCP l How do we accomplish these goals intuitively? 10/23/2007 ecs 251, fall 2007 22

UCDavis, ecs 251 Fall 2007 l Locking a Mutex If the “mutex M” is

UCDavis, ecs 251 Fall 2007 l Locking a Mutex If the “mutex M” is available and “thread T” needs it , should T lock it? t pthread_mutex_lock Mutex? ? Mutex + Priority Inheritance? ? pthread_mutex_unlock 10/23/2007 ecs 251, fall 2007 23

UCDavis, ecs 251 Fall 2007 l Risk for Locking a Mutex If the “mutex

UCDavis, ecs 251 Fall 2007 l Risk for Locking a Mutex If the “mutex M” is available and “thread T” needs it , should T lock it? Checking before Locking it!! t pthread_mutex_lock t We don’t know whether the high priority thread will occur in the next X seconds! But, does it matter? pthread_mutex_unlock 10/23/2007 ecs 251, fall 2007 24

UCDavis, ecs 251 Fall 2007 “Checking” What? ? 10/23/2007 ecs 251, fall 2007 25

UCDavis, ecs 251 Fall 2007 “Checking” What? ? 10/23/2007 ecs 251, fall 2007 25

UCDavis, ecs 251 Fall 2007 Mutex Priority Ceiling pthread_mutex_lock potential customers PC t t

UCDavis, ecs 251 Fall 2007 Mutex Priority Ceiling pthread_mutex_lock potential customers PC t t t pthread_mutex_unlock Max priority A preventive action (could be unnecessary though) 10/23/2007 ecs 251, fall 2007 26

UCDavis, ecs 251 Fall 2007 Priority Ceiling Should I get it? PC PC PC

UCDavis, ecs 251 Fall 2007 Priority Ceiling Should I get it? PC PC PC thread t 2 ? ? ? Max. PC value PC 1 thread t 9 PC 4 thread t 4 PC 2 PC 4 locked unlocked 10/23/2007 PC ecs 251, fall 2007 27

UCDavis, ecs 251 Fall 2007 Mutex/PIP Get it as long as it is available!

UCDavis, ecs 251 Fall 2007 Mutex/PIP Get it as long as it is available! PC PC PC thread t 2 YES Max. PC value PC 1 thread t 9 PC 4 thread t 4 PC 7 PC 4 locked unlocked 10/23/2007 PC ecs 251, fall 2007 28

UCDavis, ecs 251 Fall 2007 PCP Not so Fast PC PC PC thread t

UCDavis, ecs 251 Fall 2007 PCP Not so Fast PC PC PC thread t 2 NO Max. PC value PC 1 thread t 9 PC 4 thread t 4 PC 7 PC 4 locked unlocked 10/23/2007 PC ecs 251, fall 2007 29

UCDavis, ecs 251 Fall 2007 PCP Not so Fast PC PC PC thread t

UCDavis, ecs 251 Fall 2007 PCP Not so Fast PC PC PC thread t 2 NO Max. PC value PC 2 thread t 9 PC 4 thread t 4 PC 7 PC 4 locked unlocked 10/23/2007 PC ecs 251, fall 2007 30

UCDavis, ecs 251 Fall 2007 PCP How about? ? ? PC PC PC thread

UCDavis, ecs 251 Fall 2007 PCP How about? ? ? PC PC PC thread t 2 ? ? Max. PC value PC 2 thread t 2 PC 4 thread t 4 PC 7 PC 4 locked unlocked 10/23/2007 PC ecs 251, fall 2007 31

UCDavis, ecs 251 Fall 2007 Are we sure about the claim of PCP? A

UCDavis, ecs 251 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. l Deadlocks are prevented/avoided. l l Try to find a “Counter Example” to show that PCP’s claim is FALSE!! 10/23/2007 ecs 251, fall 2007 32

UCDavis, ecs 251 Fall 2007 Critical Section Requirements (similar to 2 PL) l Threads

UCDavis, ecs 251 Fall 2007 Critical Section Requirements (similar to 2 PL) l Threads must lock and unlock in a “nested” or “pyramid” fashion: – Let L(S) = lock(S). – Let U(S) = unlock(S). – Example: L(s 1); L(s 2); L(s 3); . . . ; U(s 3); U(s 2); U(s 1); s 3 s 2 s 1 10/23/2007 ecs 251, fall 2007 33

UCDavis, ecs 251 Fall 2007 Tasks 1 2 3 4 0 2 10/23/2007 4

UCDavis, ecs 251 Fall 2007 Tasks 1 2 3 4 0 2 10/23/2007 4 Executing 6 8 10 12 14 Preempted 16 18 Executing with Q locked Blocked Executing with V locked Ceiling-driven Indirectly Blocked ecs 251, fall 2007 34

UCDavis, ecs 251 Fall 2007 Priority Inversion (12, 6, 8, 17) 1 2 3

UCDavis, ecs 251 Fall 2007 Priority Inversion (12, 6, 8, 17) 1 2 3 4 0 2 10/23/2007 4 Executing 6 8 10 12 14 Preempted 16 18 Executing with Q locked Blocked Executing with V locked Ceiling-driven Indirectly Blocked ecs 251, fall 2007 35

UCDavis, ecs 251 Fall 2007 Priority Inversion Area (12, 6, 8, 17) 1 2

UCDavis, ecs 251 Fall 2007 Priority Inversion Area (12, 6, 8, 17) 1 2 3 4 0 2 10/23/2007 4 Executing 6 8 10 12 14 Preempted 16 18 Executing with Q locked Blocked Executing with V locked Ceiling-driven Indirectly Blocked ecs 251, fall 2007 36

UCDavis, ecs 251 Fall 2007 Basic Priority Inheritance (9, 12, 14, 17) Process 1

UCDavis, ecs 251 Fall 2007 Basic Priority Inheritance (9, 12, 14, 17) Process 1 2 1 3 1 4 0 2 10/23/2007 4 6 1 1 8 10 ecs 251, fall 2007 12 14 16 18 37

UCDavis, ecs 251 Fall 2007 1 1 Priority Ceiling (7, 12, 14, 17) Process

UCDavis, ecs 251 Fall 2007 1 1 Priority Ceiling (7, 12, 14, 17) Process 1 2 3 2 4 0 2 2 4 2 1 6 1 8 10 12 14 16 18 locked 10/23/2007 ecs 251, fall 2007 38

UCDavis, ecs 251 Fall 2007 Can we do better? ? 10/23/2007 ecs 251, fall

UCDavis, ecs 251 Fall 2007 Can we do better? ? 10/23/2007 ecs 251, fall 2007 39

UCDavis, ecs 251 Fall 2007 1 1 Priority Ceiling Emulation (6, 12, 14, 17)

UCDavis, ecs 251 Fall 2007 1 1 Priority Ceiling Emulation (6, 12, 14, 17) process d c 1 1 b 1 a 0 2 10/23/2007 1 1 4 1 6 8 10 ecs 251, fall 2007 12 14 16 18 40

UCDavis, ecs 251 Fall 2007 “Kernel” Mutex User processes OS Kernel “No preemption in

UCDavis, ecs 251 Fall 2007 “Kernel” Mutex User processes OS Kernel “No preemption in the Kernel mode” 10/23/2007 ecs 251, fall 2007 41

UCDavis, ecs 251 Fall 2007 PCP/PCE Mutex Obtain the PC value as my own

UCDavis, ecs 251 Fall 2007 PCP/PCE Mutex Obtain the PC value as my own priority pthread_mutex_lock Less context switching No Mutex queue!? PC pthread_mutex_unlock 10/23/2007 ecs 251, fall 2007 42

UCDavis, ecs 251 Fall 2007 l l l Priority Ceiling Emulation Each thread has

UCDavis, ecs 251 Fall 2007 l l l 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 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 10/23/2007 ecs 251, fall 2007 43

UCDavis, ecs 251 Fall 2007 Property #1 l A job J can be blocked

UCDavis, ecs 251 Fall 2007 Property #1 l A job J can be blocked by a lower priority job Jlow, only if the priority of J is no higher than the highest ceiling of all locked mutexes by lower priority jobs before J is initiated. 10/23/2007 ecs 251, fall 2007 44

UCDavis, ecs 251 Fall 2007 Property #2 l Jj in a mutex Mj is

UCDavis, ecs 251 Fall 2007 Property #2 l Jj in a mutex Mj is preempted by Ji in another mutex Mi. Then, Jj can not inherit a priority higher than or equal to Ji until Ji completes. 10/23/2007 ecs 251, fall 2007 45

UCDavis, ecs 251 Fall 2007 How? l Jsuperhigh is blocked by Jj (so Jj

UCDavis, ecs 251 Fall 2007 How? l Jsuperhigh is blocked by Jj (so Jj will inherit) l But, that mutex must not be locked yet. l Jj will never reach there before Ji finishes. 10/23/2007 ecs 251, fall 2007 46

UCDavis, ecs 251 Fall 2007 Transitive Blocking 10/23/2007 ecs 251, fall 2007 47

UCDavis, ecs 251 Fall 2007 Transitive Blocking 10/23/2007 ecs 251, fall 2007 47

UCDavis, ecs 251 Fall 2007 Transitive Blocking PCP prevents Transitive Blocking l J 1,

UCDavis, ecs 251 Fall 2007 Transitive Blocking PCP prevents Transitive Blocking l J 1, J 2, J 3 l J 3 blocks J 2, and J 2 blocks J 1 l J 3 will inherit priority of J 1, contradiction! l 10/23/2007 ecs 251, fall 2007 48

UCDavis, ecs 251 Fall 2007 Deadlock Free of PCP 10/23/2007 ecs 251, fall 2007

UCDavis, ecs 251 Fall 2007 Deadlock Free of PCP 10/23/2007 ecs 251, fall 2007 49

UCDavis, ecs 251 Fall 2007 Deadlock Free of PCP Circular waiting (circle must be

UCDavis, ecs 251 Fall 2007 Deadlock Free of PCP Circular waiting (circle must be two!) l Both of them must need both mutexes! l 10/23/2007 ecs 251, fall 2007 50

UCDavis, ecs 251 Fall 2007 Property #3 l Ji can be blocked by Jlow

UCDavis, ecs 251 Fall 2007 Property #3 l Ji can be blocked by Jlow for at most one duration of mutex. 10/23/2007 ecs 251, fall 2007 51

UCDavis, ecs 251 Fall 2007 One duration block l Ji can be blocked once

UCDavis, ecs 251 Fall 2007 One duration block l Ji can be blocked once for all Jlow’s. 10/23/2007 ecs 251, fall 2007 52

UCDavis, ecs 251 Fall 2007 Priority Ceiling Protocols (PCP) A higher priority thread can

UCDavis, ecs 251 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. l Deadlocks are prevented/avoided (? !). l Transitive inheritance is prevented. l l Are they really critical? 10/23/2007 ecs 251, fall 2007 53

UCDavis, ecs 251 Fall 2007 Summary l l l Priority Inversion Basic Priority Inheritance

UCDavis, ecs 251 Fall 2007 Summary l l l 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 l Priority Ceiling Emulation – Immediately upgraded to the ceiling value after obtaining the lock 10/23/2007 ecs 251, fall 2007 54