# Ernest Cachia University of Malta “Designing Concurrency” Slide No. 1 of part 2 Problem with single flag method  A small but definite time interval exists.

## Presentation on theme: "Ernest Cachia University of Malta “Designing Concurrency” Slide No. 1 of part 2 Problem with single flag method  A small but definite time interval exists."— Presentation transcript:

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 1 of part 2 Problem with single flag method  A small but definite time interval exists between flag status reading and flag status update. 000 Task 1 Task 2 1 Resource ? ? ? 11 22 t1_readt2_readt2_set

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 2 of part 2 Solving the single flag problem  Make the time interval between flag status reading and update as minimal as possible  Better still - make these separate actions part of the same instruction  Forbid task switching while tasks are in critical area  Keep critical areas as minimal as possible

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 3 of part 2 Semaphores  Different action to simple flags  Widely used common resource access control mechanism  Take the form of “binary” or “counting” semaphores  Associates itself with to access operations P and V (from the Dutch “passeren” meaning to pass and “vrygeven” meaning to release) Task 1 Task 2 Task n  semaphore Shared resource

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 4 of part 2 Actions of a Semaphore  In general same as those of a simple flag - but with one very important difference: If another task is waiting to use a given shared resource, the task currently using that resource, once done, will leave the semaphore’s status unchanged and simply passes on “authority” to the waiting task, which then proceeds to use the shared resource.

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 5 of part 2 Example of Semaphore Actions Timeline milestones A: sr set to STOP by 1 B: sr set to GO by 1 C: sr set to STOP by 2 D: 1 waiting for resource E: 1 re-activated, no change to sr F: sr set to GO by 1 Semaphore “sr” Shared resource 1 Task 1 Task 2 Shared resource 2 1 C 2 C ABCD C 1 EF Task 1 Task 2 “sr”    time C

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 6 of part 2 The Binary Semaphore (1) Procedure WaitForAccess (S: semaphore); BEGIN DisableInterrupts; IF S = NotBusy THEN S := Busy; ELSE SuspendTask; END; EnableInterrupts; END; Procedure SigResourceFree (S: semaphore); BEGIN DisableInterrupts; IF TaskWaiting THEN WakeUpTask; ELSE S := NotBusy; END; EnableInterrupts; END;

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 7 of part 2 The Binary Semaphore (2) PROGRAM TOP; BEGIN {Some program code} WaitForAccess(sr); {Critical code part using any combination of r1 and r2} SigResourceFree(sr); {Some more program code} END. PROGRAM TOP; BEGIN {Some program code} AccessSharedResource(sr, r2); {Some more program code} END.

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 8 of part 2 The Counting Semaphore (1) Used to control access to groups of identical resources Semaphore “csr” Group “g1” Group “g2” Shared resource 2 Task 1 Task 2 Shared resource 1 Shared resource 3 Shared resource 1 Shared resource 2 Task n 

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 9 of part 2 The Counting Semaphore (2)  Consider the following system: Now analyse a possible timeline for the above system - (next slide) Semaphore “prs” “printers” Task 1 Task 2 Task 3 Task 4 Shared printer 1 Shared printer 2 Shared printer 3

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 10 of part 2 The Counting Semaphore (3) “prs” counts how many resources remainavailable within the “printers” group. 0121001 C C 3233122 CC 1 Task 1 Task 2 “prs”   time Task 3 Task 4 C C C 0123    C C CC

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 11 of part 2 The Counting Semaphore (4) Procedure WaitForPrinter; BEGIN DisableInterrupts; IF Printers > 0 THEN Decrement(Printers); ELSE SuspendTask; END; EnableInterrupts; END; Procedure SigPrinterFree; BEGIN DisableInterrupts; IF TaskWaiting THEN WakeUpTask; ELSE Increment(Printers); END; EnableInterrupts; END;

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 12 of part 2 Task Co-operation  Must exist in some form or other to co-ordinate the (correct) procedure, effort, and effect of independent tasks.  Can include, or exclude, data transfer  Requires individual attention when developing concurrent systems.  Is substantially dependent on the nature and requirements of the system in question.  Has direct impact on system efficiency and complexity

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 13 of part 2 Task co-operation modes  Only synchronisation Strict control dependency between the activities of the co- operating tasks but no transfer of data between them.  Only data transfer Co-operating task activities are completely independent of each other except for the passage of data between them in some pre-defined way.  Both synchronisation and data transfer Co-operating tasks are linked via control dependencies and through the passage of data between them.

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 14 of part 2 Task co-operation via synchronisation only  Definition (adapted to concurrency): “A means by which to provide task co-ordination”  Type of synchronisation: u Ordered (specifies the order in which task operations are to be carried out) u Timed (specifies the time at which task operations are to be carried out)  Makes use of “event flags” and “signals”

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 15 of part 2 Synchronisation Only Example (Events) Above tasks can be viewed as independent. They must synchronise (sign-in) their actions at some pre-defined point, but not have their actions suspended by any of the other tasks. Compute coords Check for coords Validate cmnds Process cmnds Task 1 Task 2 Task 3 Task 4 Coord available Cmd available Event operations: SET CLEAR READ

Ernest Cachia University of Malta “Designing Concurrency” Slide No. 16 of part 2 Synchronisation Only Example (Signals) Compute coords Check for coords Validate cmnds Process cmnds Task 1 Task 2 Task 3 Task 4 Coord available Cmd available Key: A - t3 signals, t4 not started B - t4 accepts and runs C - t3 signals, t4 busy D - t4 accepts and runs E - t4 waits for t3 signal F - t4 accepts and runs G - t4 waits for t3 signal H - t4 accepts and runs I - t3 signals, t4 busy J - t4 accepts and runs Task 3 Task 4   time ABCDEFGHIJ

Download ppt "Ernest Cachia University of Malta “Designing Concurrency” Slide No. 1 of part 2 Problem with single flag method  A small but definite time interval exists."

Similar presentations