Mutual Exclusion - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Mutual Exclusion

Description:

Results of one process. may depend on. information obtained. from others. Timing of process may ... Cooperation by sharing. Processes indirectly aware of each ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 18
Provided by: paulr7
Category:

less

Transcript and Presenter's Notes

Title: Mutual Exclusion


1
Mutual Exclusion Synchronization
  • Chapter 5a

2
Concurrency
  • Mutual Exclusion and Synchronization
  • Principles of Concurrency
  • Mutual Exclusion
  • Software Solutions
  • Hardware Solutions
  • Semaphores
  • Producer/Consumer
  • Message Passing
  • Monitors

3
Cooperating Processes
Concurrency
  • Operating systems allow for the creation and
    concurrent execution of multiple processes
  • concurrency can ease program complexity
  • concurrency can increase efficiency
  • How can the processes work together?
  • Flags
  • Files
  • Messages
  • Shared memory

4
Cooperating Processes
Concurrency
  • Concurrent processes (or threads) often need to
    share data (maintained either in shared memory or
    files) and resources
  • If there is no controlled access to shared data,
    some processes will obtain an inconsistent view
    of this data
  • The actions performed by concurrent processes may
    depend on the order in which their execution is
    interleaved
  • With no synchronization, results are typically
    not deterministic nor reproducible.

5
Operating Systems must
Concurrency
  • The OS must keep track of active processes.
  • The OS must allocate and deallocate resources.
  • Processor time
  • Memory
  • Files
  • I/O devices
  • The OS must protect the data and physical
    resources.
  • The results of a process must be independent of
    the speed of execution relative to the speed of
    other concurrent processes.

6
Examples
Concurrency Coherence
void echo() static char chin, chout chin
getchar() chout chin putchar(chout)
static int a, b void P1() void P2()
a a 1 b 2 b b b 1 a
2 a
7
Process Interaction
Concurrency
Potential Control Problems
Process Interaction
Relationship
Degree of Awareness
8
Resource Competition
Concurrency
  • Mutual Exclusion
  • Critical resource a single non-sharable
    resource.
  • Critical section portion of the program that
    accesses a critical resource.
  • Deadlock
  • Each process owns a resource that the other is
    waiting for.
  • Two processes are waiting for communication from
    the other.
  • Starvation
  • A process is denied access to a resource, even
    though there is no deadlock situation.

9
The Critical-Section Solution
Mutual Exclusion
  • Mutual Exclusion
  • At any time, at most one process can be in its
    critical section (CS).
  • Progress
  • Only processes that are not executing in their CS
    can participate in the decision of who will enter
    next in the CS.
  • This selection cannot be postponed indefinitely.
  • Bounded Waiting
  • After a process has made a request to enter its
    CS, there is a bound on the number of times that
    the other processes are allowed to enter their CS
    - otherwise the process will suffer from
    starvation.
  • And, of course, no deadlock!
  • No assumptions can be made about speed
  • Solutions execute some entry code and some exit
    code surrounding critical section

10
Rules of Mutual Exclusion
Mutual Exclusion
  • Mutual exclusion must be enforceable.
  • A process that halts in its non-critical section
    must not interfere with other processes.
  • No deadlock or starvation.
  • If a resource is available, entry must be
    permitted without delay.
  • No assumptions are to be made about the relative
    process speeds or number of processors.
  • A process can remain inside its critical section
    for only a finite time.

11
Turns
Mutual Exclusion Software
repeat while (turn ! me) do no-op ltcritical
sectiongt turn you other stuff until false
12
Bakery Algorithm
Mutual Exclusion Software
repeat choosingi true ni
max(n0, n1, nn-1) 1) choosingi
false for j 0 to n-1 do while choosingj
do no-op while ((nj, j) lt (ni, i) and
(nj ! 0)) do no-op endfor // do
critical section numberi 0 // do
remainder section until false
Wait while someone else is choosing
When there is a tie, use their id number
Could have overflow if we dont reset all numbers
periodically!
More on Bakery Algorithm later
13
Software Solutions
Mutual Exclusion Software
  • Turns
  • Bakery algorithm works
  • Proof is left as an exercise for the reader -)
  • Proof by exhaustive cases
  • Bakery algorithm is a pain, complex, difficult to
    follow
  • Other software solutions
  • Dekkers algorithm
  • Petersons algorithm
  • Can hardware help?
  • An atomic instruction support for exclusion

14
Hardware Solutions
Mutual Exclusion
  • Interrupt disabling
  • not all interrupts need to be disabled
  • might not work in multiprocessor environment
  • Special machine instructions
  • test and set instruction (TSET)
  • exchange instruction (XCHG)
  • advantages
  • works on any number of processors sharing memory
  • simple and supports multiple critical sections
  • disadvantages
  • busy waiting is employed
  • starvation and deadlock are possible

15
Test-and-Set
Mutual Exclusion
bool testset(int i) disable interrupts
if (i 0) i 1 enable
interrupts return false else
enable interrupts return true
  • Shared variable i (initialized to 0)
  • Only the first Pi who sets i enters CS

Process Pi repeat repeat until !testset(b)
CS b0 Stuff until false
16
Mutual Exclusion w/Test-and-Set
Mutual Exclusion
while(true) waitingi true key true
while(waitingi key) if (key
TestAndSet(lock)) ...context switch...
waitingi false // do critical section
// find the next waiting task (round robin) j
(i 1) n while((j ! i) !waitingj)
j (j 1) n if (j i) lock false
else waitingj false // do remainder
17
Mutual Exclusion w/XCHG
Mutual Exclusion
  • Shared variable b is initialized to 0
  • Each Pi has a local variable k
  • The only Pi that can enter CS is the one who
    finds b0
  • This Pi excludes all the other Pj by setting b to
    1

Process Pi repeat k1 repeat xchg(k,b)
until k0 CS b0 RS until false
Write a Comment
User Comments (0)
About PowerShow.com