G53SRP: Resource Sharing Issues

19
1 G53SRP: Resource Sharing Issues Chris Greenhalgh Chris Greenhalgh School of Computer School of Computer Science Science

description

G53SRP: Resource Sharing Issues. Chris Greenhalgh School of Computer Science. 1. Contents. Resource sharing Priority inversion Priority inheritance Priority ceiling emulation RTSJ support Wait-free interactions Summary Book: Wellings ch. 14 (esp. 14.1 & 14.2). 2. Resource sharing. - PowerPoint PPT Presentation

Transcript of G53SRP: Resource Sharing Issues

Page 1: G53SRP: Resource Sharing Issues

11

G53SRP: Resource Sharing Issues

Chris GreenhalghChris Greenhalgh

School of Computer ScienceSchool of Computer Science

Page 2: G53SRP: Resource Sharing Issues

22

Contents• Resource sharingResource sharing• Priority inversionPriority inversion• Priority inheritancePriority inheritance• Priority ceiling emulationPriority ceiling emulation• RTSJ supportRTSJ support• Wait-free interactionsWait-free interactions• SummarySummary

• Book: Wellings ch. 14 (esp. 14.1 & 14.2)Book: Wellings ch. 14 (esp. 14.1 & 14.2)

Page 3: G53SRP: Resource Sharing Issues

3

Resource sharingResource sharing• Locking => blocking

– lower-priority process locking a resource shared with a higher-priority process delays it

– Additional source of interference – delays worst case response time of higher-priority process

• By longest time for which lower priority process holds lock for each acquisition in worst case

Page 4: G53SRP: Resource Sharing Issues

4

Example of Blocking

Process

a

b

0 2 4 6 8 10 12 14 16 18

Executing

Executing with Q locked

Preempted

Blocked

Process b has higher priority

Process b attempts to lock shared resource Q

Process a releases lock on Q and is preempted

Page 5: G53SRP: Resource Sharing Issues

5

Priority inversionPriority inversion• But its worse than that:

– P3 (lowest priority) holds lock– P1 (highest priority) is blocked waiting for lock– P2 (medium priority) executes

• Pre-empts P3 – higher priority

• so P1 is further delayed by P2 (no shared resource) as well as P1

Page 6: G53SRP: Resource Sharing Issues

6

Example of Priority InversionProcess

a

b

c

0 2 4 6 8 10 12 14 16 18

Executing

Executing with Q locked

Preempted

Blocked

Process c attempts to lock shared resource Q

Process a releases lock on Q

Process c delayed byb which pre-empts a

Page 7: G53SRP: Resource Sharing Issues

7

Priority inheritancePriority inheritance• Dynamically adjusts priority of a process

holding a lock:(a) Simple priority inheritance

=> to maximum priority of all processes currently waiting for lock

(b) Priority ceiling emulation

=> to maximum priority of all processes that ever acquire lock

Page 8: G53SRP: Resource Sharing Issues

8

Example of Simple Priority Inheritance

Process

a

b

c

0 2 4 6 8 10 12 14 16 18

Executing

Executing with Q locked

Preempted

Blocked

Process c attempts to lock shared resource Q

Process a releaseslock on Q

Process a inheritspriority of c

Page 9: G53SRP: Resource Sharing Issues

9

Example of Priority Ceiling Emulation

Process

a

b

c

0 2 4 6 8 10 12 14 16 18

Executing

Executing with Q locked

Preempted

Blocked

Process a has elevatedpriority while locking Q Process a has normal

priority when not locking Q

Page 10: G53SRP: Resource Sharing Issues

10

Priority inheritance notes• Simple priority inheritance

– Prevents priority inversion – limits blocking delay– No additional information required

• Priority ceiling emulation– Easier to implement– Requires input from programmer or program analysis

to decide in advance what priority to associate with each lock/resource

– May reduce worst case blocking compared to simple priority inheritance, but may increase delays in common case (always elevates priority)

Page 11: G53SRP: Resource Sharing Issues

11

Priority inheritance in RTSJ• Simple priority inheritance required by default• Priority ceiling emulation may also be available

– In the specification, but optional to implement

– NOT implemented in the lab JVM

• Specifiable per monitor (i.e. Java object)– With process-wide default

• Implemented by MonitorControl and subclasses…

Page 12: G53SRP: Resource Sharing Issues

12

Priority Inheritance classes

<<abstract>>javax.realtime.MonitorControl

javax.realtime.PriorityCeilingEmulation

javax.realtime.PriorityInheritance

extends

Page 13: G53SRP: Resource Sharing Issues

13

MonitorControl classpackage javax.realtime;public abstract class MonitorControl { // default public static MonitorControl getMonitorControl(); public static MonitorControl setMonitorControl( MonitorControl mc); // one monitor public static MonitorControl getMonitorControl( Object monitor); public static MonitorControl setMonitorControl( Object monitor, MonitorControl mc);}

Page 14: G53SRP: Resource Sharing Issues

14

PriorityInheritance classpackage javax.realtime;public class PriorityInheritance extends MonitorControl { // singleton public static PriorityInheritance instance();}

Page 15: G53SRP: Resource Sharing Issues

15

PriorityCeilingEmulation classpackage javax.realtime;public class PriorityCeilingEmulation extends MonitorControl { // instance per priority public PriorityCeilingEmulation instance(int ceiling);

public int getCeiling(); // max public static int getMaxCeiling();}

N.B. throws UnsupportedOperationExceptionif not supported by JVM

Page 16: G53SRP: Resource Sharing Issues

16

Examples of use … // actually default anyway… MonitorControl.setMonitorControl( PriorityInheritance.instance()); … MonitorControl.setMonitorControl(sharedObj, PriorityCeilingEmulation.instance(20)); … synchronized(sharedObj) { // will run with priority 20… … } …

N.B. it is an error to lock an object with a lower ceiling priority than the thread

Page 17: G53SRP: Resource Sharing Issues

17

Wait-free interactionsWait-free interactions• An alternative to locks and blocking…• Interactions from a (e.g. high priority) thread fail

rather than block• So no risk of delaying the (e.g. high priority) thread

• E.g. javax.realtime.WaitFreeReadQueue & WaitFreeWriteQueue– Will block write/read (respectively) if queue full/empty

(respectively)

– Will not block read/write (respectively) but return null/false (respectively) to indicate failure

Page 18: G53SRP: Resource Sharing Issues

18

Summary (1)Summary (1)• Resource sharing with locks (mutual exclusion) Resource sharing with locks (mutual exclusion)

may introduce additional delays due to blockingmay introduce additional delays due to blocking• Priority inversion can resultPriority inversion can result

– Intermediate priority processes pre-empt lower priority Intermediate priority processes pre-empt lower priority processes holding locks needed by higher priority processes holding locks needed by higher priority processesprocesses

• Priority inheritance prevents this => limits Priority inheritance prevents this => limits blocking timeblocking time– Simple priority inheritance increases lock-holder’s Simple priority inheritance increases lock-holder’s

priority when higher priority processes are waiting for priority when higher priority processes are waiting for locklock

– Priority ceiling emulation always increases lock-Priority ceiling emulation always increases lock-holder’s priorityholder’s priority

Page 19: G53SRP: Resource Sharing Issues

19

Summary (2)Summary (2)• RTSJ requires priority inheritance by RTSJ requires priority inheritance by

defaultdefault– Others (e.g. priority ceiling emulation) can be Others (e.g. priority ceiling emulation) can be

specified as default or per-monitorspecified as default or per-monitor• These are optional in implementation(s)These are optional in implementation(s)

– See See MonitorControlMonitorControl

• Wait-free interaction can be used to avoid Wait-free interaction can be used to avoid some forms of conflict & blockingsome forms of conflict & blocking– E.g. E.g. WaitFreeReadWaitFreeRead//WriteQueueWriteQueue