Upload
fatimah-al-juhani
View
222
Download
0
Embed Size (px)
Citation preview
8/2/2019 Chapter 6 102 II
1/42
SPRING 2011 - 1021
8/2/2019 Chapter 6 102 II
2/42
SPRING 2011 - 1022
8/2/2019 Chapter 6 102 II
3/42
SPRING 2011 - 102
3
What is Critical Section
A piece of code in cooperating
threads/processes in which thethreads/process may update some
shared data (variable, file,
database).
What is Critical
Section Problem
If multiple threads/processes try
to execute their CS
simultaneously, we need to
execute them one by one
completely.
8/2/2019 Chapter 6 102 II
4/42
do
{
Critical Section
remainder section
}
Entry section
Exit Section
SPRING 2011 - 102
4
Solution
8/2/2019 Chapter 6 102 II
5/42
SPRING 2011 - 102
5
MutualExclusion
ProgressBoundedWaiting
Good
Solution
8/2/2019 Chapter 6 102 II
6/42
SPRING 2011 - 102
6
MutualExclusion
If process Pi is executing in its
critical section, then no other
processes can be executing intheir critical sections
Pi
8/2/2019 Chapter 6 102 II
7/42
SPRING 2011 - 102
7
Progress
If no process is executing in
its critical section and there
exist some processes that
wish to enter their critical
section, then the selection ofthe processes that will enter
the critical section next
cannot be postponed
indefinitely
8/2/2019 Chapter 6 102 II
8/42
SPRING 2011 - 102
8
BoundedWaiting
A bound must exist on the
number of times that other
processes are allowed to
enter their critical sectionsafter a process has made a
request to enter its critical
section and before that
request is granted
8/2/2019 Chapter 6 102 II
9/42
Assume that each process executes at a
nonzero speed, No assumption
concerning relative speed of the N
processes
Consideration
SPRING 2011 - 102
9
8/2/2019 Chapter 6 102 II
10/42
1. Petersons solution (software solution)
2. Disable interrupts (hardware solution)
3. Switch variables (assume atomic read and write)
4. Locks (hardware solution with TSL or TAS)
5. Semaphores (software solution)
6. Critical Regions and Monitors (HLL solution)
SPRING 2011 - 102
10
8/2/2019 Chapter 6 102 II
11/42SPRING 2011 - 10211
8/2/2019 Chapter 6 102 II
12/42
Peterson solution is restricted to two
processes that alternate execution between
their critical section and remainder sections.
SPRING 2011 - 102
12
8/2/2019 Chapter 6 102 II
13/42
The two processes share two variables:
int turn;
Boolean interested/Flag[2]
Flag [i] = true implies that process Pi is ready!
Flag [j] = true implies that process Pj is ready!
The variable turn indicates
whose turn it is to enter the
critical section.
The interested array is used to
indicate if a process is ready to
enter the critical section
STEPS
Before entering CS, each process set its flag to true and make turn equal to
other process ID.
It then enters the while loop and checks whether the other process wants
to enter its CS and is it his turn if yes spin SPRING 2011 - 102
13
8/2/2019 Chapter 6 102 II
14/42
Not interested in
running
do {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
} while (TRUE);
I am
interested
to run
But give turn
to other
process
If its a turn of
other process and
other process is
also ready to runLOOP
SPRING 2011 - 102
14
Pi
8/2/2019 Chapter 6 102 II
15/42
do {
flag[0] = TRUE;
turn = 1;
while ( flag[1] && turn == 1);
CRITICAL SECTION
flag[0] = FALSE;
REMAINDER SECTION
} while (TRUE);
do {
flag[1] = TRUE;
turn = 0;
while ( flag[0] && turn == 0);
CRITICAL SECTION
flag[1] = FALSE;
REMAINDER SECTION
} while (TRUE);
Process 0 Process 1SPRING 2011 - 102
15
8/2/2019 Chapter 6 102 II
16/42
SPRING 2011 - 102
16
Pi enters in its critical section at flag[j]=false orturn =i. Turn can have only one value at a giventime
MutualExclusion
8/2/2019 Chapter 6 102 II
17/42
SPRING 2011 - 102
17
Pi can only be prevented to enter in critical sectionif stuck in while loop with condition flag[j]=trueand turn=j
Progress
8/2/2019 Chapter 6 102 II
18/42
SPRING 2011 - 102
18
Exiting process sets it flag to falsecomesback and quickly set it to true again. Butsets turn to the number of other process
Satisfied or notBoundedWaiting
8/2/2019 Chapter 6 102 II
19/42
SPRING 2011 - 102
19
Exiting process sets it flag to falsecomesback and quickly set it to true again. Butsets turn to the number of other process
BoundedWaiting
8/2/2019 Chapter 6 102 II
20/42
SPRING 2011 - 10220
8/2/2019 Chapter 6 102 II
21/42
H/W solution does not mean that we are using
some IC to handle the CS problem.
It basically mean that we are using instructions
specified in the Instruction Set Architecture of a
processor to handle CSP.
SPRING 2011 - 102
21
8/2/2019 Chapter 6 102 II
22/42
SPRING 2011 - 102
22
8/2/2019 Chapter 6 102 II
23/42
In a uni-processor environment, we can handle CSP, if weforbid/mask interrupts , while some shared variable is being
modified.
Shortcoming
If a user level program is given the ability to disableinterrupts, then it can disable the timer interrupt. Thus
context switching will not take place, thus allowing it to use
the CPU w/o letting other processes to execute.
Uni-Processor environment
SPRING 2011 - 102
23
8/2/2019 Chapter 6 102 II
24/42
In a multi-processor environment, it is not feasible to disable interrupts,
because
It will only prevent processes from executing on the CPU in which
interrupts are disabled. Processes can execute on other CPUs and
therefore does not guarantee mutually exclusive access to program
state.
Disabling interrupts on all CPUs gives a great performance loss
Disabling interrupts works, but safe to use only inside OS/kernel.
Multi-Processor environment
SPRING 2011 - 102
24
8/2/2019 Chapter 6 102 II
25/42
SPRING 2011 - 10225
8/2/2019 Chapter 6 102 II
26/42
Modern machines provide special atomic hardware instructions
Test & Set
test memory word and set value
Swap Instructions
swap contents of two memory words
Atomic instruction = = non - interrupt able instruction
8/2/2019 Chapter 6 102 II
27/42
SPRING 2011 - 10227
8/2/2019 Chapter 6 102 II
28/42
One of the hardware approach to provide certain
atomicoperations.
These operations are guaranteed to operate as a
single instruction, without interruption.
One such operation is the "Test and Set", which
simultaneously sets a boolean lock variable and
returns its previous value.
SPRING 2011 - 102
28
8/2/2019 Chapter 6 102 II
29/42
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
Save the value of
lock
Make lock
value True
Return old value of
Lock
8/2/2019 Chapter 6 102 II
30/42
do {
while ( TestAndSet (&lock ))
; /* do nothing
critical section
lock = FALSE;
remainder section
} while ( TRUE);
Shared boolean variable lock
initialized to false.
boolean TestAndSet (boolean *target)
8/2/2019 Chapter 6 102 II
31/42
SPRING 2011 - 102
31
do {
while ( TestAndSet (&lock ))
; /* do nothing
critical section
lock = FALSE;
remainder section
} while ( TRUE);
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
lock FalseTrue
False
8/2/2019 Chapter 6 102 II
32/42
SPRING 2011 - 102
32 Satisfied.How?MutualExclusion
Satisfied.HowProgress
If there are multiple processes trying to get into their critical
sections, there is no guarantee of what order they will
enter, and any one process could have the bad luck to wait
forever until they got their turn in the critical section
BoundedWaiting
Is the TSL-based solution good?
8/2/2019 Chapter 6 102 II
33/42
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
Another common hardware-supplied instruction that is useful for
building synchronization primitives is compare-and-swap. Much like
test-and-set, it is atomic in hardware
8/2/2019 Chapter 6 102 II
34/42
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
Shared Boolean variable lock initialized to FALSE;
Each process has a local Boolean variable key.
void Swap (boolean *a, boolean *b)
8/2/2019 Chapter 6 102 II
35/42
SPRING 2011 - 102
35
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
p ( , ){
boolean temp = *a;*a = *b;*b = temp:
}lock False
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
Key FalseKey False
void Swap (boolean *a, boolean *b)
8/2/2019 Chapter 6 102 II
36/42
SPRING 2011 - 102
36
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
p ( , ){
boolean temp = *a;*a = *b;*b = temp:
}lock FalseTrue
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
Key FalseKey FalseTRUE
TRUE
void Swap (boolean *a, boolean *b)
8/2/2019 Chapter 6 102 II
37/42
SPRING 2011 - 102
37
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
p ( , ){
boolean temp = *a;*a = *b;*b = temp:
}lock FalseTrue
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
Key FalseKey FalseTRUEFalse TRUE
TRUE
True
void Swap (boolean *a, boolean *b)
8/2/2019 Chapter 6 102 II
38/42
SPRING 2011 - 102
38
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
p ( , ){
boolean temp = *a;*a = *b;*b = temp:
}lock FalseTrue
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
critical section
lock = FALSE;
remainder section
} while ( TRUE);
Key FalseKey FalseTRUEFalse TRUE
TRUE
True
8/2/2019 Chapter 6 102 II
39/42
SPRING 2011 - 102
39 Satisfied.How?MutualExclusion
Satisfied.HowProgress
If there are multiple processes trying to get into their critical
sections, there is no guarantee of what order they will
enter, and any one process could have the bad luck to wait
forever until they got their turn in the critical section
BoundedWaiting
Is the SWAP- based solution good?
8/2/2019 Chapter 6 102 II
40/42
Achieve Bounded wait using TestAndSet
Achieve Bounded wait using SWAP
SPRING 2011 - 102
40
Do{
8/2/2019 Chapter 6 102 II
41/42
SPRING 2011 - 102
41
Do{
waiting[i] = true;
key = true;
while (waiting[i] && key)
key = TestAndSet(lock);waiting[i] = false;
Critical Section
j = (i+1) % n;
while ( (j != i) && !waiting[j] )
j = (j+1) % n;
if (j == i)
lock = false;else
waiting[j] = false;
Remainder Section
}while(1)
Local variable key;
Global variables lock andwaiting set to false
1
2
3
45
6
7
8
9
10
11
12
13
14
Do{
8/2/2019 Chapter 6 102 II
42/42
42
{
waiting[i] = true;
key = true;
while (waiting[i] && key)
swap(lock, key);waiting[i] = false;
Critical Section
j = (i+1) % n;
while ( (j != i) && !waiting[j] )
j = (j+1) % n;
if (j == i)
lock = false;else
waiting[j] = false;
Remainder Section
Local variable key;
Global variables lock andwaiting set to false
1
2
3
45
6
7
8
9
10
11
12
13
14