Lecture 9 - Suraj @ LUMS
Download
Report
Transcript Lecture 9 - Suraj @ LUMS
Tuesday, June 20, 2006
"The box said that I needed to have
Windows 98 or better... so I
installed Linux."
- LinuxNewbie.org
1
th
6
Edition
Skip 1.4, 1.5, 1.6
Skip 2.6
Skip 3.6, 3.7, 3.8
(3.5 will be done later)
Skip 4.5, 4.6
Skip 6.4, 6.5
2
Producer / Consumer
Critical Section problem
3
The Critical-Section Problem
n processes all competing to use some
shared data
Each process has a code segment,
called critical section, in which the
shared data is accessed.
Problem – ensure that when one
process is executing in its critical
section, no other process is allowed to
execute in its critical section.
4
Race condition: The situation where several
processes access – and manipulate shared data
concurrently. The final value of the shared data
depends upon which process finishes last.
5
Solution to Critical-Section Problem
1. Mutual Exclusion.
2. Progress.
3. Bounded Waiting.
6
Algorithm 1
turn is initialized to 0 (or 1)
7
Algorithm 1
Strict Alternation
Other problems with Algorithm 1? ...
8
Algorithm 2
Shared variables
boolean flag[2];
initially flag [0] = flag [1] = false.
flag [i] = true Pi ready to enter
its critical section
9
Algorithm 2
i, j two processes j=1-i
Process Pi
do {
flag[i] := true;
while (flag[j]) ;
critical section
flag [i] = false;
remainder section
} while (1);
10
Algorithm 2
Process P0
do {
flag[0] := true;
while (flag[1]) ;
critical
section
flag [0] = false;
remainder section
} while (1);
Process P1
do {
flag[1] := true;
while (flag[0]) ;
critical
section
flag [1] = false;
remainder section
} while (1);
11
Algorithm 2
Problems?
12
Algorithm 3
Process Pi
do {
flag [i]:= true;
turn = j;
while (flag [j] and turn = j) ;
critical section
flag [i] = false;
remainder section
} while (1);
13
Algorithm 3
Process P1
Process P0
do {
do {
flag [1]:= true;
flag [0]:= true;
turn = 0;
turn = 1;
while (flag [1] and turn = 1) ; while (flag [0] and turn = 0) ;
critical section
critical section
flag [1] = false;
flag [0] = false;
remainder section
remainder section
} while (1);
} while (1);
14
Algorithm 3
Meets all three requirements;
solves the critical-section problem
for two processes.
15
Bakery Algorithm
Critical section for n processes
Before entering its critical section,
process receives a number. Holder of
the smallest number enters the critical
section.
If processes Pi and Pj receive the same
number, if i < j, then Pi is served first;
else Pj is served first.
The numbering scheme always
generates numbers in increasing order
of enumeration; i.e., 1,2,3,3,3,3,4,5...
16
Bakery Algorithm
Notation < lexicographical order (ticket #,
process id #)
(a,b) < c,d) if a < c or if a = c and b < d
max (a0,…, an-1) is a number, k, such that k ai
for i - 0, …, n – 1
Shared data
boolean choosing[n];
int number[n];
Data structures are initialized to false and 0
respectively
17
Bakery Algorithm
do {
choosing[i] = true;
number[i] = max(number[0], number[1], …,
number [n – 1])+1;
choosing[i] = false;
for (j = 0; j < n; j++) {
while (choosing[j]) ;
while ((number[j] != 0) && (number[j],j) <
(number[i],i) ) ;
}
critical section
number[i] = 0; //exit section
remainder section
18
} while (1);
Synchronization Hardware
One way … disable interrupts
problems?
19
Synchronization Hardware
One way … disable interrupts
What problems can occur if we use the
following implementation of a lock?
Lock::Acquire { disable_interrupts(); }
(critical section)
Lock::Release { enable_interrupts(); }
20
TestAndndSet Instruction
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
21
Mutual Exclusion with Test-and-Set
Shared data:
boolean lock = false;
Process Pi
do {
while (TestAndSet(lock)) ;
critical section
lock = false;
remainder section
}
What if two processes try to execute
TestAndSet?
22
Synchronization Hardware
Any problems with test and set?
23
Can a context switch occur if a process is
executing in its critical section?
24
Synchronization Hardware
Do yourself
Swap
Mutual exclusion with test and set
25
Drawback of all of the synchronization
solutions seen so far?
26
BUSY WAITING
A process is waiting for an event to occur and
it does so by executing instructions
27
Busy Waiting
Priority inversion problem
A lock that uses busy waiting is called a
spinlock
Synchronization tool that allow a process to
block instead of wasting CPU time, when
the process is not allowed to enter its
critical region.
28