0% found this document useful (0 votes)
27 views4 pages

AUU Lec3

This document provides an overview of the online paging problem in computer systems and algorithms for solving it. The problem involves managing a small fast memory cache and larger slower memory. When a requested page is not in the cache, an algorithm must decide which existing page to remove to make room. The optimal offline algorithm is discussed, as well as limitations of deterministic online algorithms and improved randomized algorithms. Key results include that no deterministic algorithm can have a competitive ratio better than the cache size k, while randomized algorithms can achieve O(log k) competitiveness.

Uploaded by

ishaan20068
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

AUU Lec3

This document provides an overview of the online paging problem in computer systems and algorithms for solving it. The problem involves managing a small fast memory cache and larger slower memory. When a requested page is not in the cache, an algorithm must decide which existing page to remove to make room. The optimal offline algorithm is discussed, as well as limitations of deterministic online algorithms and improved randomized algorithms. Key results include that no deterministic algorithm can have a competitive ratio better than the cache size k, while randomized algorithms can achieve O(log k) competitiveness.

Uploaded by

ishaan20068
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lecture 2: Online Paging

Paging is another classical problem in the domain of online algorithms. The problem appears in im-
plementing virtual memory systems as part of operating systems. We consider all code and data used by a
program to be divided in to pages of uniform size. A two level virtual memory framework consists of - (a)
a fast but small memory (called cache) of size k and (b) a slower but larger memory called of size N . The
paging problem is as follows. At any point of time t, a program might request a particular page pt . In case
the page is in the cache, it is readily accessed. However, in case it is not in cache, we say there is a cache
miss (and the cache is full), pt has to be swapped in with some page that is existent in cache. The main job
of the online algorithm is to decide which page to swap out with the objective of minimizing the number of
cache misses over a sequence of requests.

1 The Offline Optimal


In the offline setting, the entire sequence of page requests is known to the algorithm up front. It turns out
that in this case, Longest-Forward-Distance (LFD) policy is the optimal. It simply means, whenever there is
a cache miss, replace the page which is going to be requested farthest in the future. Note that this is crucially
an offline algorithm and not applicable in an online setting.

Theorem 1. LFD is the optimal page replacement policy for any sequence of requests.

We are not proving this statement as part of these lectures. But the reader is encouraged to do so -
exchange argument comes in handy.

2 Online Deterministic Algorithms


As usual, we start with a lower bound result.

Theorem 2. No deterministic algorithm for paging can be better than k-competitive

Proof. Fix any deterministic algorithm A. We assume that both the optimal algorithm and A start with the
same initial cache configuration. Let the cache size be k and there be k + 1 pages in all. Now we shall
construct a bad sequence for A by playing the role of an adversary. Observe that there is always exactly one
page outside the cache. Now at any round t = 0, 1, 2, · · · , the adversary requests as pt the page which is not
in cache.
It is straightforward to observe that for a sequence σ, A will suffer |σ| cache misses. How many misses
does LFD (opt) suffer ? Suppose at time t, LFD suffers one miss and swaps out a page p′ . We claim that the
next miss for LFD cannot happen before k − 1 requests. Suppose, for contradiction, it does. This means that
p′ has been accessed before the k − 1 other requests that were in the cache when p′ was being swapped out.
But then p′ cannot be the page that is requested farthest in the future - a contradiction to the offline optimal
policy. Hence, the total number of cache misses for LFD is at most |σ|/k.
Thus the competitive ratio is at least k.

1
This is a rather disappointing result. However, there are two silver linings. Firstly, we will see that a
certain class of algorithms indeed achieve the bound of k-competitiveness. Secondly, we will see that with
randomization, we can do much better in expectation.

2.1 1-bit MARKING Algorithm


We define a very generic class of paging algorithm called MARKING . The algorithm maintains a single
0/1-bit of information for every page. We will say the pages are ‘marked’ or ’unmarked’ for the bit value
1 or 0 respectively. Initially, all pages are unmarked (including those intially in the cache). Now suppose a
page pt has been requested. One of the following cases might happen.

1. pt is already in the cache : Then we simply mark the page. Note that it could already be marked in
which case we do nothing.

2. pt is not in cache : Then we swap in pt and mark it while swapping out one of the unmarked pages.
The choice of this page to evict is arbitrary.
In case all pages in the cache are marked, we unmark all of them and then execute the step above.

Before proving the theorem about the MARKING algorithm, we observe that several standard al-
gorithms like Least-Recently-Used (LRU), Last-In-First-Out(LIFO) and CLOCK falls in the category of
MARKING algorithms. The only difference among the above three are in the choice of unmarked page
that they decide to evict.

Theorem 3. The 1-bit MARKING algorithm is k-competitive.

Proof. We would need to introduce the notion of a phase in order to carry out the analysis. Note that this is
just for the purpose of analysis and the algorithm is oblivious to the phases. A phase is defined inductively
as follows. P0 is defined as a vacuous phase that begins and ends before t = 1. For any i = 1, 2, · · · m,
phase i, Pi , is defined to be the longest subsequence of at most k distinct page requests starting at t = t′ + 1
where t′ is the end of Pi−1 . Note that by definition, all the phases except P0 and Pm will have exactly k
distinct page requests. Further, one may also note that every time point where we unmark all pages in the
algorithm coincides with the beginning of a new phase. The phases decompose the entire sequence σ in to
disjoint contiguous sub-sequences.
Now we need two observations to complete the proof. Firstly, inside a phase, the number of misses for
MARKING is at most k since there are k distinct page requests and once a page is inside the cache, it is
never swapped out in the same phase.
Secondly, we claim that LFD suffers at least one cache miss after the end of each phase. The reason for
this is - at the end of a phase, there are k distinct pages in the cache. Further, the next page requested must
be distinct from all of these k pages since otherwise the phase would not have ended. Hence, we have a
sequence of k + 1 distinct page requests and any algorithm using a cache of size k will suffer at least one
miss.
Hence for every phase, the ratio between the number of cache misses of MARKING and LFD is at
most k and adding up over all phases gives us the theorem.

3 Online Randomized Algorithms


We will consider a randomized version of the MARKING algorithm described above. The algorithm re-
mains exactly the same except one change. Here is a full description for the same of completeness.

2
1. pt is already in the cache : Then we simply mark the page. Note that it could already be marked in
which case we do nothing.

2. pt is not in cache : Then we swap in pt and mark it while swapping out an unmarked page uniformly
at random.
In case all pages in the cache are marked, we unmark all of them and then execute the step above.

Note that the only modification is in the choice of unmarked page that is evicted. Whereas in the determin-
istic case, the choice was arbitrary, here every unmarked page has equal probability of getting evicted.

Theorem 4. Randomized MARKING is O(log k)-competitive.

Proof. First, we will prove a somewhat stronger lower bound on opt (that is LFD). For this, we need to
introduce a bit of notation. Consider any phase Pi , 1 ≤ i ≤ m. Now, define ni be the number of requests
which are distinct from all pages that had been requested in Pi−1 or equivalently, pages that are in the cache
at the end of Pi−1 (note that for i = 1, only the second definition is valid since P0 is vacuous). Let qi be the
number of cache misses for opt for all i = 1, 2, · · · m. We are going to prove the following claim
Claim 1.
m
X
opt ≥ ni /2
i=1

Proof. We first prove the following. For all i = 1, 2, · · · m,

qi−1 + qi ≥ ni

The claim follows simply from that fact that considering phases Pi−1 and Pi together, there is a sequence
of k + ni distinct page requests. Hence, any algorithm will need to make at least ni number of misses.

Now, adding up over all i = 1, 2, · · · m


m
X m
X
qi−1 + qi ≥ ni
i=1 i=1
m
X m
X
=⇒ 2 qi − q1 − qm ≥ ni
i=1 i=1
m
X m
X
=⇒ 2opt ≥ n i + q1 + qm ≥ ni
i=1 i=1

Now we need to analyze the expected number of cache misses suffered by randomized MARKING .
Again, let us try to understand what happens inside a phase Pi . Randomized MARKING is definitely going
to suffer ni number of misses since these are fresh page requests which are not in cache at the beginning of
phase Pi . What other misses might it suffer. Well at most k − ni of the pages that were already in cache (let
us call them ‘old pages’) might be evicted (since they have been all unmarked at the beginning of Pi ) but
then requested again inside Pi . The eviction of such pages are random events. Hence, we need to introduce
random variables. Let us rename the old pages as o1 , o2 , · · · ok−ni according to the order of their first access
inside Pi . Now let us introduce random variable Xj as follows.
(
1, if oj is not in cache on first access
Xj =
0, otherwise

3
From the above definition,
Pk it is clear that the total number of cache misses Psuffered by MARKING due
k
to the old pages is X = j=1 Xj . Using linearity of expectation, E[X] = j=1 E[Xj ]. The nice thing is
that Xj is an indicator random variable and hence E[Xj ] = P[Xj = 1]. Let us now bound this probability.
Let do this step by step. Why will O1 be evicted. Well clearly, it will be evicted because one of the
new pages have been requested. Let n̄1 be the number of new pages requested before the first access to o1
is made inside Pi . Each of these n̄1 accesses threw out an old page uniformly at random. An equivalent
viewpoint is the following - we select a subset of size n̄1 from k old pages uniformaly at randon without
replacement. It is a well-known fact (try to prove it !) that the probability of a specific page , in particular
o1 to be among the sampled pages is n̄1 /k ≤ ni /k (since the number of new pages in ni ).
Similarly, consider o2 . Again, let there be n̄2 new page accesses before o2 is accessed for the first time.
There is one additional subtlety here. It might also happen that o2 has been evicted because of an access
to o1 which was evicted earlier due to access request for a new page, say p′ . However, while counting, this
can be charged directly to the access to p′ which was actually responsible for eviction of o1 . So, what is the
probability now ? Note that now the number of unmarked pages is not k any more but k − 1, since o1 has
been marked. Thus, the probability is n̄2 /(k − 1) ≤ ni /(k − 1),
One can generalize the above argument to claim that P[Xj = 1] = ni /(k − j) for all j = 1, 2, · · · k − 1.
Thus
k−n
Xi ni
E[X] = = O(log k)ni
k−j
j=1

Thus, adding up over all phases Pi , i = 1, 2, · · · m, we get the expected number of cache misses for
MARKING is at most
m m
!
X X
(ni + ni c log k) ≤ c log k ni = O(log k)opt
i=1 i=1

where the last inequality follows from the Claim 1 proved above.

You might also like