简体   繁体   中英

Calculate Virtual Memory Page Faults Number

I have trouble with calculating number of page faults. I have a question that is:

Consider the two-dimensional array D defined as follows: int D (128, 128); # each element is one word (I think it means 32 bit)

Assume that the OS only makes one 1024-byte page frame available in the physical (ie main) memory for D. For paging, LRU or FIFO is used as the replacement policy. Assume that the matrix is stored in row-major order. How many page faults would be generated for the following cases?

Summation of all matrix entries row by row, with LRU? Does this change if we make it summation of all matrix entries column by column, with LRU?

Thank you very much

Assuming matrix D starts at a page boundary, then you can compute the total amount of memory it requires. Matrix has 128x128 entries, each 4 bytes long. Therefore each row consumes 128x4 bytes = 512 bytes. As you have 128 rows, the matrix occupies 128x512bytes = 64Kb.

If you access the matrix row-by-row, you'd generate a page fault when accessing the first element, and the OS will page in one 1024-byte page which holds two consecutive rows.

Therefore, after that page fault you would be able to access the next 255 cells (up to completing the two rows). Then when you try to access the first element of the third row you it would generate a new page fault, evicting the page previously loaded and paging in the next two rows.

And we repeat the same all over again until we have accessed all the rows.

So, you have 1 page fault per every 2 rows. As you have 128 rows, you'd have 64 page faults.

It does change when you do the summation column by column. Taking into account that every page contains two rows, as when you read the first column of the first row the OS will page in the first two rows. So the next column accessed (the first column of the second row) will be already on memory and thus will not generate a page fault. But the third column won't be paged in so a new page fault will be generated.

So, for every odd column (1st, 3rd, 5th, and so on) you will generate a page fault. As the matrix has 128 rows, for every column 64 page faults will be generated. And considering the matrix has 128 rows, a total ammount of 64*128 page faults will be issued when accessing the matrix column-wise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM