Struct Cours9 e
Struct Cours9 e
Problem
Cache Memory
254
The dining philosophers problem: definition
255
• However, the table is set in a very peculiar way: between every pair of
adjacent plates, there is only one fork.
• A philosopher being clumsy, he needs two forks to eat: the one on his
right and the one on his left.
256
The dining philosophers problem: illustration
f4
P4
f0
P3
f3
P0
P2
P1 f1
f2
257
The dining philosophers problem:
a first solution
258
/* Definitions and global initializations */
#define N = ? /* number of philosophers */
semaphore fork[N]; /* semaphores modeling
the forks */
int j; for (j=0, j < N, j++) fork[j]=1;
philospher(i)
int i;
{ while(true)
{ think();
wait(fork[i]); wait(fork[(i+1)%N]);
eat();
signal(fork[i]); signal(fork[(i+1)%N]);
}
}
259
The dining philosophers problem:
a first solution - the deadlock
• The problem is that each philosopher must acquire two resources and
does this
1. in two steps,
In this solution, the order in which the philosopher N-1 picks up his forks is
modified.
261
The dining philosophers problem:
a second solution - deadlock ?
262
Furthermore, assuming the the semaphores are fair, not process can be
blocked forever.
• For a fork never to be freed, the process holding it must be waiting for
a higher-order fork.
• Since the chain of blocking waits takes us up in the fork order, it must
stop when the highest-order fork is reached. Indeed, this one will
necessarily be the last one acquired by the process using it and will
thus eventually be freed.
263
The dining philosophers problem:
a third solution
In this solution, resource acquisition is done in one step. For this we will
use a monitor through which all the fork management will be done.
• This monitor uses a table f[], where the number of forks available for
each philosopher (0, 1 or 2) is held.
264
The dining philosophers problem:
The fork monitor - constructors
public ForkMonitor(int N)
{ nb = N; urcount = 0;
f = new int[nb];
for (int i=0; i<nb ; i++) f[i] = 2;
urgent = new SemaphoreFIFO(0);
mutex = new SemaphoreFIFO(1);
oktoeat = new Semaphore[nb];
for (int i=0; i<nb ; i++)
{ oktoeat[i] = new Semaphore(0);
waiting[i] = false;
}
}
265
The dining philosophers problem:
the fork monitor - picking up forks
266
The dining philosophers problem:
the fork monitor - releasing forks
philosophe(int i)
{ while(true)
{ think();
F.takeFork(i);
eat();
F.releaseFork(i);
}
269
Cache memory: principle
• One can then hope that most accesses will be to the cache and will
thus be fast. When the required data is not in the cache, it must be
transferred from DRAM, which is slow and will force the processor to
wait.
270
Cache memory: its associative character
• Cache memory must provide the same function and thus associate
data, not to its own addresses, but to those of the main memory.
• Cache memory must thus contain pairs (address, data) and make it
possible to very quickly find the data part of a pair given its address
part.
271
Cache memory: the overall schema
address address
CPU CACHE DRAM
Mem[address] Mem[address]
Fast Slow
When the required data is not in the cache, the processor is blocked until
the data is transferred from the DRAM to the cache.
272
Why are cache memories effective?
• Indeed, at times that are close to each other, one often accesses the
same addresses. This is called temporal locality.
273
Totally associative cache
...
Data hit
274
Totally associative cache: characteristics
• The cache includes a comparator for each stored word, which limits its
capacity.
275
Block cache
hit
276
Direct mapped cache
• There is only one possible cache address for each memory address. A
replacement policy is thus not needed.
277
A block direct mapped cache
address
25 bits 3 2
Data
hit
278
An intermediate solution: set associative cache
279
A set associative cache
address
27 bits 3
= = = =
Data
hit
280
Cache and write operations
• Most memory operations are reads. What must be done for writes? Is
it necessary to immediately modify the memory? There are several
options.
281
Cache memory versus virtual memory
• In the context of virtual memory, the cache can use either physical or
virtual addresses.
282