Professional Documents
Culture Documents
L2 Chp1 Web
L2 Chp1 Web
Introduction
Lecture 2
Admin
● Prerequisites of COS122 and COS212
will be enforced
● Mutual Exclusion
Processes and Threads
● Process:
● an instance of a program being executed.
● has its own address space, which can not be
accessed from other processes.
● inter-process communication possible through
sockets, pipes, semaphores, etc. (requires
effort)
● Thread:
● a separate line of execution (i.e. a sequence of
instructions) within a process.
● a process can have multiple threads, all of
which share its address space, file handles etc.
Problems of Concurrency
● The concurrency problems arise from the
scenario of concurrent computing, where the
execution of a program is conducted in
multiple processes (or threads)
simultaneously.
● Safety property:
● assert that nothing ‘bad’ will ever happen
during an execution (that is, that the program
will never enter a ‘bad’ state)
● Liveness property:
● assert that something ‘good’ will eventually
happen during execution.
● concerned with making progress in a program
Program Correctness
● Single lane bridge problem
Program Correctness
● Safety property: specify a property to ensure
that cars do not collide (e.g. impose one-way
rule)
Program Correctness
● Liveness property: does every car eventually
get an opportunity to cross the bridge (i.e.
make progress)?
10.2
Mutual Exclusion
Mutual Exclusion (mutex)
● The problem of making sure that one
thread at a time can execute a particular
block of code (critical section)
● No deadlock
● If only one wants in, it gets in
● If both want in, only one gets in
● This is a liveness property – makes sure
that something good happens eventually
A B
Protocol 1: Viewing
● Idea:
● Look at the yard to see if it is empty
● Problem:
● Trees obscure the view
● Alice and Bob might look at (almost) the same
time, and both conclude that the yard is empty.
● Interpretation:
● Looking at the yard and releasing a pet are
distinct atomic steps.
● Explicit communication required for
coordination
Protocol 2: Cellphone Protocol
● Idea:
● Bob calls Alice (or vice-versa) to ask to release
the pet
● Problem:
● Alice may be taking a shower, shopping for pet
food, or her cellphone may be off
● Interpretation:
● Transient communication (like talking) isn’t ideal
● Recipient might not be listening, or there at all
● Communication should be persistent (like sending
mail)
Protocol 3: Can Protocol
● Idea:
● Put a can on Alice’s windowsill, with a string to Bob’s
house (and vice-versa)
● When Bob wants to release the dog, pulls string
knocking over Alice’s can (“sends a message”)
● Bob releases the dog if Alice’s can is down and his
can is up. And vice-versa.
Protocol 3: Can Protocol
● Problem:
● Bob needs Alice to reset her can after the dog has left
the yard. And vice-versa.
● What if Alice goes away on holiday?
● Interpretation:
● Interrupts aren’t ideal for solving mutual exclusion.
Alice and Bob better control their own signals.
Protocol 4: Flag Protocol
A B
Flag Protocol
Alice’s Protocol:
● Raise flag
● Wait until Bob’s flag is down
● Release pet Question:
● Lower flag when pet returns What would happen if
both Alice and Bob
Bob’s Protocol: raise their flags at the
● Raise flag same time?
● Wait until Alice’s flag is down
● Release pet
● Lower flag when pet returns
Flag Protocol Question:
Why isn’t it a good
idea to let both Alice
Alice’s Protocol: and Bob be “polite”?
● Raise flag
● Wait until Bob’s flag is down No, no…
After you!
● Release pet after you!
● Lower flag when pet returns
Bob’s Protocol:
● Raise flag
● Wait until Alice’s flag is down
● Release pet
● Lower flag when pet returns
A deadlock could occur
Flag Protocol
Alice’s Protocol:
● Raise flag
● Wait until Bob’s flag is down
● Release pet
● Lower flag when pet returns
● Deadlock-freedom?
● YES
● If both pets want to use the yard, Bob
defers to Alice
Does the flag protocol work?
● Starvation-freedom?
If a pet wants to enter the yard, will it
eventually succeed?
● NO.
● Whenever Alice and Bob are in conflict, Bob
defers to Alice, thus it is possible that Alice’s
pet uses the yard over and over and Bob’s
pet doesn’t get a turn
Waiting
● If Alice raises her flag and suddenly
becomes ill, Bob’s pet cannot use the
yard until Alice returns
● It can be solved by
● one-bit shared variables that can be read or
written (flags)