views:

83

answers:

1

Homework:

Consider the two-dimensional array A:

      int A[][] = new int[200][200];

where A[0][0] is at location 500 in a paged memory system with pages of size 500 (a little unrealistic -- not 512). A small process that manipulates the matrix resides in page 0 (locations 0 to 499). Thus, every instruction fetch will be from an instruction currently stored in page 0.

Assuming there are only five page frames, including page 0, how many page faults are generated by the following array-initialization loops, using LRU replacement and assuming that page frame 0 contains the process and the other four are initially empty?

A)for (int j = 0; j < 200; j++) for (int i = 0; i < 200; i++) A[i][j] = 0;

B)for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) A[i][j] = 0;

Question:

Where do I begin to figure this out? I have went through my text but haven't found much of it to be useful. I threw some numbers around and I found:

40,000 = total number of array items

80 (40k/500) = total number of pages

A) 20,000 (80*250) because every other loop causes a page fault?

B) 80 (One for each page, 40,000/500 = 80)?

Am I on the right track? Any advice? Hints?

+1  A: 

Think about the order that the initialization happens in loop A vs loop B, and how a multi-dimentional array is laid out in memory. One of them will access memory addresses in-order, the other will jump around. After 200 assignments, you'll be looking at A[199][0] in one case, and A[0][199] in the other. How many pages have you touched?

Bob
http://en.wikipedia.org/wiki/Row-major_order I am using this as my reference point for how memory is laid out and row-major order. This is not a bad idea, right?
Google
A) Jumping around, after every 2.5 inner iterations it page faults??? B) Only page faults once per page.
Google
On that thought, A) will fault 80 times every inner loop, 200 times so that's 80*200, 16,000 total page faults for A. I think that's it?! Should I keep looking into this? I hope I got it!
Google
@Google Yep, that's exactly what you need to be looking at. Calculate the offset for each case at different points. Pay special attention to what happens after 1, 199, 200, and 201 "steps" in each case.
Bob
@Google What I usually do in cases like this . . . write a short program to model the behavior of another program. Do the same loops, but calculate the "memory address" in each case, and increment a "page fault" whenever you cross a page boundary. That's why we write programs; to make solving problems easier :)
Bob
Thanks a lot! If you have any more advice please let me know~ you have been very helpful.
Google