Program Structure

Q1. Explain Program Structure.

Ans. Demand paging is designed to be transparent to the user program. In many cases, the user is completely unaware of the paged nature of memory. In other cases, however, system performance can be improved if the user (or compiler) has an awareness of the underlying demand paging.

 

Let’s look at a contrived but informative example. Assume that pages are 128 words in size. Consider a C program whose function is to initialize to 0 each element of a 128-by-128 array. The following code is typical:

 

inti , j ;

int [128][128] data;

for (j = 0; j < 128; j++)

for (i = 0; i< 128; i++)

data[i] [j] = 0;

 

Notice that the array is stored row major; that is, the array is stored data[0] [0], data[0] [1], …, data[0] [127], data[l] [0], data[l] [1], …,data [127] [127]. For pages of 128 words, each row takes one page. Thus, the preceding code zeroes one word in each page, then another word in each page, and so on. If the operating system allocates fewer than 128 frames to the entire program, then its execution will result in 128 x 128 = 16,384 page faults.

 

In. contrast, changing the code to

 

inti, j ;

int[128][128] data;

for (i = 0; i< 128; i++)

for (j = 0 ; j < 128; j++)

data[i] [j] = 0;

 

zeroes all the words on one page before starting the next page, reducing the number of page faults to 128.

 

Careful selection of data structures and programming structures can increase locality and hence lower the page-fault rate and the number of pages in the working set.

 

For example, a stack has good locality, since access is always made to the top.

Leave a Reply

Your email address will not be published. Required fields are marked *