Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

Lecture 3

VIEWS: 0 PAGES: 8

									            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




                                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




                                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

                Admit new
                people one
                by one                                                     Turnstile 2
                Nv :=Nv+1
                                                                           Admit new people
                                                                           one by one
                                                                           Nv :=Nv+1




    Exit
                              Number of visitors (Nv)
    Lock gate after
    letting out Nv
    people




                                        Concurrent & Distributed Systems
                      Ornamental Gardens: 3 Process Annotations

•   Nv is set to zero at the beginning of the day
                                               Turnstile 2
     Turnstile 1
                                               Repeat
     Repeat
                                                 ----
      ----
                                                 ----
      ----
                                                 ----
      ----
                                                 b1 Y := Nv
      a1 X := Nv
                                                 b2 Y := Y+1
      a2 X := X+1
                                                 b3 Nv := Y
      a3 Nv := X
                                                 ----
      ----
                                                 ----
      ----
                                               Until no more admissions
     Until no more admissions


•   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 a1 - a3 ,
         b1 - b3. (Check the effect of this by running CCP4 a few times)


                                  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


     Turnstile 1
                                                         Turnstile 2
     Repeat
                                                         Repeat
      ---- da di da
                                                          ----
      ----
                                                          ---- da di da
      $U-
                                                          $U_
      a1 X := Nv
                                                          b1 Y := Nv
                                Linked pair
      a2 X := X+1
                                of Critical               b2 Y := X+1
      a3 Nv := X                Sections
                                                          b3 Nv := Y
      $U+
                                                          $U+
      ----
                                                          ----
     Until no more admissions
                                                         Until no more admissions

•   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




                                              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.
     Turnstile 1
                                                         Turnstile 2
     Repeat
                                                         Repeat
      ----
                                                          ----
      While Key=2 remain busy waiting
                                                         While Key=1 remain busy waiting
      a1 X := Nv
                                                          b1 Y := Nv
                                Linked pair
      a2 X := X+1
                                of Critical               b2 Y := X+1
      a3 Nv := X                Sections
                                                          b3 Nv := Y
      Key := 2
                                                          Key := 1
      ----
                                                          ----
     Until no more admissions
                                                         Until no more admissions


•   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


                                              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

    Turnstile 1                                          Turnstile 2
    Repeat                                               Repeat
     ----                                                  ----
    a1 While (P2 = inCritical) remain busy waiting        b1 While (P1 = inCritical) remain busy waiting
    a2 P1 := inCritical                                   b2 P2 := inCritical
      X := Nv                                            Y := Nv
                                  Linked pair
      X := X+1                    of Critical            Y := X+1
                                  Sections
     Nv := X                                             Nv := Y
     P1 := outOfCritical                                  P2 := outOfCritical
     ----                                                  ----
    Until no more admissions                             Until no more admissions


•   Good! Asymmetry between processes doesn’t matter
•   Bad
     – Unsafe scenarios exist, eg a1 , b1, a2, b2 – run CCP6 a few times to see this
     – What about busy waiting?
     – What about > 2 processes?




                                                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

    Turnstile 1                                             Turnstile 2
    Repeat                                                  Repeat
     ----                                                     ----
    a1 P1 := inCritical                                     b1 P2 := inCritical
    a2 While (P2 = inCritical) remain busy waiting          b2 While (P1 = inCritical) remain busy waiting
      X := Nv                                               Y := Nv
                                 Linked pair
      X := X+1                   of Critical                Y := X+1
                                 Sections
     Nv := X                                                Nv := Y
     P1 := outOfCritical                                     P2 := outOfCritical
     ----                                                    ----
    Until no more admissions                                Until no more admissions




•   Good! Asymmetry between processes still doesn’t matter, and no unsafe scenarios
•   Bad
      – ‘Unlive’ scenarios exist, eg a1 , b1, a2, b2 – 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?

                                               Concurrent & Distributed Systems

								
To top