You are on page 1of 13

Dining Philosopher Problem

Dining Philosopher Problem


Shared: int state[5], semaphore s[5], semaphore mutex;
Init: mutex = 1; s[i] = 0 for all i=0 .. 4

take_fork(i) {
Philosopher i P(mutex);
state[i] = hungry;
test(i) {
do { test(i);
if(state[i] == hungry
/* think */ V(mutex);
&& state[(i+1)%N] != eating
take_fork(i); P(s[i]);
&& state[(i-1+N)%N != eating)
/* eat */ }
{
put_fork(i); state[i] = eating;
/* think */ put_fork(i) {
V(s[i]);
} while(true); P(mutex);
}
state[i] = thinking;
test(LEFT);
test(RIGHT)%N);
V(mutex);
}
Memory Management
Page and Frame Replacement Algorithms

 Page Replacement algorithm


 FIFO, Optimal and LRU
 Want lowest page-fault rate on both first access and re-access
 Frame Allocation algorithm determines
 How many frames to give each process
Allocation of Frames
 How to calculate number of frames allocated to each process ?
 Same fraction of memory?
 Different fractions of memory?
 Each process needs minimum number of pages
 Want to make sure that all processes that are loaded into memory can
make forward progress
 Example: IBM 370 – 6 pages to handle SS MOVE instruction:
 instruction is 6 bytes, might span 2 pages
 2 pages to handle from
 2 pages to handle to
Fixed Allocation
 Equal allocation
 Every process gets same amount of memory
 Example:
 100 frames
 5 processes  each process gets 20 frames

 Proportional allocation
 Allocate according to the size of process
 Computation proceeds as follows:
si = size of process pi and
S = si
m = total number of frames
s si
ai = allocation for pi = m
S
Priority Allocation

 Proportional scheme using process priorities rather than size


 If process Pi generates a page fault,
 select for replacement one of its frames
 select for replacement a frame from a process with lower priority
number
Thrashing
Thrashing
 If a process does not have “enough” pages, the page-fault rate is very high
 Page fault to get page
 Replace existing frame
 But quickly need replaced frame back
 This leads to:
 Low CPU utilization
 Operating system thinking that it needs to increase the degree of
multiprogramming
 Another process added to the system

 Thrashing  a process is busy swapping pages in and out


Thrashing
 Swapping out pages of a process just before that pages are needed
 The processor spends most of its time swapping pages rather than
executing user instructions
 Why does thrashing occur?
Σ size of locality > total memory size
Working Set Model
 As a program executes it transitions through a sequence of “working sets”
consisting of varying sized subsets of the address space
Page Number

Time
Working-Set Model

   working-set window  fixed number of page references


 Example: 10,000 instructions
 WSi (working set of Process Pi) = total set of pages referenced in the most
recent  (varies in time)
 if  too small will not encompass entire locality
 if  too large will encompass several localities
 if  =   will encompass entire program
 D = |WSi|  total demand frames
 if D > m  Thrashing
 Policy: if D > m, then suspend/swap out processes
 This can improve overall system behavior by a lot!
Global vs. Local Allocation
 Global replacement – process selects a replacement frame from the set of
all frames; one process can take a frame from another
 But then process execution time can vary greatly
 But greater throughput so more common

 Local replacement – each process selects from only its own set of
allocated frames
 More consistent per-process performance
 But possibly underutilized memory

You might also like