EDA222/DIT160 – Real-Time Systems, Chalmers/GU, 2008/2009 Lecture #7 Updated 2009-02-03 Real Real- -Time Systems Time Systems Monitors Monitors Monitors: (Burns & Wellings, Chapter 8.6) Monitors: (Burns & Wellings, Chapter 8.6) • A monitor is a construct offered by some programming • Monitors • Semaphores Specification languages, e.g., Modula-1, Concurrent Pascal, Mesa. • Implementation of • A monitor encapsulates data structures that are shared mutual exclusion among multiple tasks and provides procedures to be called when a task needs to access the data structures. Implementation • Execution of monitor procedures are done under mutual exclusion. Verification • Synchronization of tasks is done with a mechanism called condition variables. Monitors Monitors Monitors Monitors Monitors vs. protected objects: Monitors vs. protected objects: Operations on condition variables: Operations on condition variables: • Monitors are similar to protected objects in Ada 95. Both wait(cond_var) : the calling task is blocked and is inserted into are passive objects that can guarantee mutual exclusion a FIFO queue corresponding to cond_var . during calls to procedures manipulating shared data. send(cond_var) : wake up first task in the queue corresponding to cond_var . No effect if the queue is empty. • The difference between monitors and protected objects are in the way they handle synchronization: Properties: Properties: – Protected objects use entries with barriers (auto wake-up) 1. After a call to wait the monitor is released (e.g., other tasks may – Monitors use condition variables (manual wake-up) execute the monitor procedures). • Java offers a monitor-like construct: 2. A call to send must be the last statement in a monitor procedure. 3. Queuing tasks that are awoken by a call to send has priority over – Java’s synchronized methods correspond to monitor procedures tasks waiting to enter the monitor. – However, Java has no mechanism that corresponds to condition variables; a thread that gets woken up must check manually whether the resource is available. 1
EDA222/DIT160 – Real-Time Systems, Chalmers/GU, 2008/2009 Lecture #7 Updated 2009-02-03 Example: simple resource manager Example: simple resource manager Example: circular buffer Example: circular buffer monitor body Simple_Resource is -- NOT Ada 95 Problem: Write a monitor Circular_Buffer that handles a circular Problem: Resource_Max : constant := 8; buffer with room for 8 data records of type Data . R : Integer range 0..Resource_Max := Resource_Max; CR : condition_variable; – The monitor should have two entries, Put and Get . procedure Acquire is begin if R = 0 then Wait(CR); end if ; – Producer tasks should be able to insert data records in the buffer R := R - 1; via entry Put . If the buffer is full, a task that calls Put should be end Acquire; blocked. procedure Release is begin – Consumer tasks should be able to remove data records from the R := R + 1; buffer via entry Get . If the buffer is empty, a task that calls Get Send(CR); end Release; should be blocked. end Simple_Resource; We solve this on the whiteboard! We solve this on the whiteboard! Semaphores Semaphores Semaphores Semaphores is an integer variable with value domain ≥ ≥ 0 Semaphores: (Burns & Wellings, Chapter 8.4) Semaphores: A semaphore s A semaphore s is an integer variable with value domain (Burns & Wellings, Chapter 8.4) 0 • A semaphore is a passive synchronization primitive that is Atomic operations on semaphores: Atomic operations on semaphores: used for protecting shared and exclusive resources. Init(s,n) : assign s an initial value n • Synchronization is done using two operations, wait and Wait(s) : signal . These operations are atomic (indivisible) and if s > 0 then s := s - 1; are themselves critical regions with mutual exclusion. else • Semaphores are used in real-time kernels och operating ”block calling task”; systems to e.g. implement rendezvous, protected objects Signal(s) : if ”any task that has called Wait(s) is blocked” or monitors. then • Semaphores were proposed by (Dutchman) E. W. Dijkstra. ”allow one such task to execute”; else It is therefore common to see the notation P and V for the s := s + 1; operations wait and signal, respectively. 2
EDA222/DIT160 – Real-Time Systems, Chalmers/GU, 2008/2009 Lecture #7 Updated 2009-02-03 Example: semaphores in Ada 95 Example: semaphores in Ada 95 Using semaphores Using semaphores Simple resource manager with critical regions Simple resource manager with critical regions Problem: Write a package Semaphores that implements semaphores Problem: in Ada 95. with Semaphores; use Semaphores; Resource_Control : Semaphore(1); – The package should define a protected object Semaphore . task A; task B; – The object should receive an initial value when it is created. task body A is begin loop – The object should have two entries, Wait and Signal , that work in Resource_Control.Wait; accordance with the definition of semaphores. ... -- Critical region with statements using the resource Resource_Control.Signal; end loop ; end A; task body B is begin loop Resource_Control.Wait; ... -- Critical region with statements using the resource Resource_Control.Signal; We solve this on the whiteboard! We solve this on the whiteboard! end loop ; end B; Mutual exclusion Disabling interrupts Mutual exclusion Disabling interrupts Methods for implementing mutual exclusion: Methods for implementing mutual exclusion: In single- -processor systems, the mutual exclusion is guaranteed processor systems, the mutual exclusion is guaranteed In single by disabling the processor’ by disabling the processor ’s interrupt service mechanism s interrupt service mechanism • By disabling the processor’s interrupt service mechanism (” ”interrupt masking interrupt masking” ”) while the critical region is executed. ) while the critical region is executed. ( – Only works for single-processor systems This way, unwanted task switches in the critical region (caused This way, unwanted task switches in the critical region (caused • With atomic processor instructions by e.g. timer interrupts) are avoided. However, all other all other tasks tasks by e.g. timer interrupts) are avoided. However, For example: the test-and-set instruction are unable to execute during this time. are unable to execute during this time. – Variables can be tested and updated in one operation Therefore, critical regions should only contain such instructions s Therefore, critical regions should only contain such instruction – Necessary for systems with two or more processors that really require mutual exclusion (e.g., code that handles that really require mutual exclusion (e.g., code that handles • With software the operations the operations wait wait and and signal signal for semaphores). for semaphores). – Dekker’s algorithm, Peterson’s algorithm – Requires no dedicated hardware support This method does not work for multi- This method does not work for multi -processor systems! processor systems! 3
Recommend
More recommend