Download presentation
Presentation is loading. Please wait.
1
Concurrent & Distributed Systems Lecture 3: Processes interacting by sharing resources Asynchronous processes can interact by sharing a common resource. In some cases this can create Critical Sections in the processes. Ways of making these Mutually Exclusive have to be found. This lecture uses a simple example of how this can happen, allowing us to do some conceptual problem solving as well as some practical work. It is essentially the same problem as the Travel Agent example, but easier to work with. The lecture introduces you to the idea of abstractions The lecture introduces you to the idea of Process Annotations
2
Concurrent & Distributed Systems The Ornamental Gardens abstraction: 1 This is a standard abstraction representing multiple concurrent processes which share a resource in an unsafe way, unless parts of each process (the Critical Sections) are made Mutually Exclusive (ME) –It is used a lot since it is easy to model in software (CCP4 in our lab) –It is a very good ‘testbed’ for ways of providing the ME (by conventional algorithms or by other mechanisms) The idea is simple: there is a walled garden with a number of entry turnstiles (at least 2), where people can enter the garden. The turnstiles record the number of people who have entered (one global total shared by all the turnstile processes). There is one exit and at the end of the day, the warden uses the global total to check that everybody has left for the day. It would be unsafe to leave anybody in the garden because the lions wake up at night! In the lab, CCP4 shows the basic abstraction and what can go wrong, CCP5,6,7 all show various attempts to cure the problem by making the Critical Sections Mutually Exclusive
3
Concurrent & Distributed Systems Ornamental Gardens: 2 There can be > 2 entrance turnstiles, but only one exit Nv is a globally shared variable, among the entrance processes Turnstile 1 Turnstile 2 Exit Number of visitors (Nv) Lock gate after letting out Nv people Admit new people one by one Nv :=Nv+1 Admit new people one by one Nv :=Nv+1
4
Concurrent & Distributed Systems Ornamental Gardens: 3 Process Annotations Nv is set to zero at the beginning of the day When simulated on a computer, the increment of Nv is done via two local variables X and Y (this is deliberate, so that interleaving of process steps can occur and cause a safety problem) The processes involve people, and so are asynchronous –So there are many detailed scenarios as to the actual ordering of - Check the effect of this by running CCP4 a few times) Turnstile 1 Repeat ---- X := Nv X := X+1 Nv := X ---- Until no more admissions Turnstile 2 Repeat ---- Y := Nv Y := Y+1 Nv := Y ---- Until no more admissions
5
Concurrent & Distributed Systems ME by brute force Possibility 1: Turn off the scheduler before and after each critical section –NB This can be done in MultiA.inc by placing $U- and $U+ in the code Problems –Not many Operating Systems allow the programmer to do that! (unlike MultiA.inc, where we have control) –There could be lots of other processes not involved in this pair of Critical Sections and they will also be denied access to the CPU Turnstile 1 Repeat ---- da di da ---- $U- X := Nv X := X+1 Nv := X $U+ ---- Until no more admissions Turnstile 2 Repeat ---- ---- da di da $U_ Y := Nv Y := X+1 Nv := Y $U+ ---- Until no more admissions Linked pair of Critical Sections
6
Concurrent & Distributed Systems Mutual Exclusion by using a Single Key (CCP5) Only allow processes access to the CS if the process has the global key –This is the old fashioned way single track railways used to work –The key can be given to either process, on initialization, eg key :=1. Good!Neither process can access its CS if the other one already is, with the key Bad –What happens if the two processes are asymmetrical in any way In duration In speed –What about more than two processes –What about busy waiting Turnstile 1 Repeat ---- While Key=2 remain busy waiting X := Nv X := X+1 Nv := X Key := 2 ---- Until no more admissions Turnstile 2 Repeat ---- While Key=1 remain busy waiting Y := Nv Y := X+1 Nv := Y Key := 1 ---- Until no more admissions Linked pair of Critical Sections
7
Concurrent & Distributed Systems Mutual Exclusion by using Own Keys :1 (CCP6) Give each process a flag that the other process can see, and check, before going into its Critical Section Sensibly, assume both processes don’t start in their critical sections. P1, P2 := outOfCritical Good!Asymmetry between processes doesn’t matter Bad –Unsafe scenarios exist, eg , – run CCP6 a few times to see this –What about busy waiting? –What about > 2 processes? Turnstile 1 Repeat ---- While (P2 = inCritical) remain busy waiting P1 := inCritical X := Nv X := X+1 Nv := X P1 := outOfCritical ---- Until no more admissions Turnstile 2 Repeat ---- While (P1 = inCritical) remain busy waiting P2 := inCritical Y := Nv Y := X+1 Nv := Y P2 := outOfCritical ---- Until no more admissions Linked pair of Critical Sections
8
Concurrent & Distributed Systems Mutual Exclusion by using Own Keys :2 (CCP6a) Avoid the unsafe interleaving scenario of CCP6 by setting the flag early, before the test Again intitialise the flags as: P1, P2 := outOfCritical Good!Asymmetry between processes still doesn’t matter, and no unsafe scenarios Bad –‘Unlive’ scenarios exist, eg , – run CCP6a a few times to see this –What about busy waiting? –What about > 2 processes? Is there a problem with ‘test and set’ with these flags? Turnstile 1 Repeat ---- P1 := inCritical While (P2 = inCritical) remain busy waiting X := Nv X := X+1 Nv := X P1 := outOfCritical ---- Until no more admissions Turnstile 2 Repeat ---- P2 := inCritical While (P1 = inCritical) remain busy waiting Y := Nv Y := X+1 Nv := Y P2 := outOfCritical ---- Until no more admissions Linked pair of Critical Sections
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.