Motvaton Engebettete Systeme Echtzetverhalten und Betrebssysteme 7. Ressourcen 1 2 Terms A resource s any software structure that can be used by a process to advance ts executon, e.g. data structure, a set of varables, a man memory area, a fle or a set of regsters of a perpheral devce. A resource dedcated to a partcular process s sad to be prvate, whereas a resource that can be used by more tasks s called a shared resource. A shared resource protected aganst concurrent accesses s called an exclusve resource. A pece of code executed under mutual excluson (to guarantee consstency of data structures n exclusve resources n case of competng tasks) s called a crtcal secton. Operatng systems typcally provde a general synchronzaton tool called a semaphore for buldng crtcal sectons. A semaphore can be accessed only through kernel prmtves called wat and sgnal. Terms A task watng for an exclusve resource s sad to be blocked on that resource. Otherwse, t proceeds by enterng the crtcal secton and holds the resource. When a task leaves a crtcal secton, the assocated resource becomes free. Watng state caused by resource constrants: actvaton ready sgnal dspatchng preempton wat run wat termnaton 3 4
Terms Each exclusve resource R must be protected by a dfferent semaphore S and each crtcal secton operatng on a resource must begn wth a wat(s ) prmtve and end wth a sgnal(s ) prmtve. All tasks blocked on the same resource are kept n a queue assocated wth the semaphore. When a runnng task executes a wat on a locked semaphore, t enters a watng state, untl another tasks executes a sgnal prmtve that unlocks the semaphore. When a task leaves the watng state, t goes nto the ready state. From all tasks n the ready state, the scheduler selects one for dspatchng accordng to ts schedulng polcy. Blockng on an exclusve resource Software structure 5 6 Prorty nverson Prorty nverson Unavodable blockng J 1 blocked by J 3 prorty nverson can last arbtrarly long [But97, S.184] 7 8
Solutons Dsallow preempton durng the executon of all crtcal sectons. Smple, but creates unnecessary blockng as unrelated tasks may be blocked. Solutons Dsallow preempton n crtcal sectons (+) smple (-) unrelated tasks can be blocked Construct schedule whch s guaranteed to be free of conflcts (+) smple concept (no semaphores etc.) (-) complex to construct (search procedure) (-) must be changed n case of dynamc tasks (-) no on-lne schedule possble Resource Access Protocols 9 10 Resource access protocols Basc dea: Modfy the prorty of those tasks that cause blockng. When a task J blocks one or more hgher prorty tasks, t temporarly assumes a hgher prorty. Methods: Prorty Inhertance Protocol (PIP), for statc prortes Prorty Celng Protocol (PCP), for statc prortes Stack Resource Polcy (SRP), for statc and dynamc prortes others Prorty Inhertance Protocol (PIP) Assumptons: n perodc tasks whch cooperate through m shared resources; fxed prortes, deadlnes equal perods, all crtcal sectons on a resource begn wth a wat(s ) and end wth a sgnal(s ) operaton. Basc dea: When a task J blocks one or more hgher prorty tasks, t temporarly assumes (nherts) the hghest prorty of the blocked tasks. Terms: We dstngush a fxed nomnal prorty P and an actve prorty p larger or equal to P. Jobs J 1, J n are ordered wth respect to nomnal prorty where J 1 has hghest prorty. Jobs do not suspend themselves. The crtcal sectons used by any task are properly nested; that s, one secton s ether entrely contaned n another one or they are unrelated. 11 12
Prorty Inhertance Protocol (PIP) Algorthm: Jobs are scheduled based on ther actve prortes. Jobs wth the same prorty are executed n a FCFS dscplne. When a job J tres to enter a crtcal secton and the resource s blocked by a lower prorty job, the job J s blocked. Otherwse t enters the crtcal secton. When a job J s blocked, t transmts ts actve prorty to the job J k that holds the semaphore. J k resumes and executes the rest of ts crtcal secton wth a prorty p k =p (t nherts the prorty of the hghest prorty of the jobs blocked by t). When J k exts a crtcal secton, t unlocks the semaphore and the hghest prorty job blocked on that semaphore s awakened. If no other jobs are blocked by J k, then p k s set to P k, otherwse t s set to the hghest prorty of the jobs blocked by J k. Prorty nhertance s transtve,.e. f 1 s blocked by 2 and 2 s blocked by 3, then 3 nherts the prorty of 1 va 2. 13 Example: Drect Blockng: hgher-prorty job tres to acqure a resource held by a lower-prorty job Push-through Blockng: medum-prorty job s blocked by a lowerprorty job that has nherted a hgher prorty form a job t drectly. blocks 14 Example wth nested crtcal sectons: prorty does not change Example of transtve prorty nhertance: J1 blocked by J2, J2 blocked by J3. J3 nherts prorty from J1 va J2. [But97, S. 189] [But97, S. 190] 15 16
Schedulablty test: A set of n perodc tasks usng PIP can be scheduled by the rate-monotonc algorthm f, 1 n, k = 1 C T k k B + T 1/ ( 2 1) Problem: Chaned Blockng Maxmum blockng tme B: Under PIP, a job J can be blocked for at most the duraton of mn(n,m) crtcal sectons, where n s the number of lowerprorty jobs that could block J and m s the number of dstnct semaphores that can be used to block J. There are algorthms to compute the maxmum blockng tmes B. [But97, S. 200] 17 18 Prorty nhertance protocol Problem: Deadlock [But97, S. 200] 19