You are on page 1of 34

1

08-Virtual-memory-Ex-1
Họ và Tên: Trần Thị Hoa Mai _ MSV: 22028141

1. Bài 9.3: Considering the page table shown in Figure 9.30 for a system
with 12-bit virtual and physical addresses and with 256-byte pages. The
list of free page frames is D, E, F (that is, D is at the head of the list, E
is second, and F is last).

Convert the following virtual addresses to their equivalent physical


addresses in hexadecimal. All numbers are given in hexadecimal. (A
dash for a page frame indicates that the page is not in memory.)

 9EF

 111

 700

 0FF
2
Địa chỉ ảo có 12 bit, kích thước 1 page là 256 byte => số bit offset =

3
log2(256) = 8, vậy số bit biểu diễn số page = 12 – 8 = 4 => Chính số
đầu tiên trong hệ hexadecimal của địa chỉ ảo chỉ số page. Chuyển
page_number -> frame_number tương ứng, 2 số còn lại trong hệ
hexadecimal biểu diễn offset giữ nguyên

 9EF -> 0EF

 111 -> 211

 700 ->D00 (vì frame D đang trống)

 0FF ->EFF (vì frame D đã dùng cho page trên, còn frame E, F trống)

2. Bài 9.8: Consider the following page reference string:

1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6.

How many page faults would occur for the following replacement
algorithms, assuming one, two, three, four, five, six, and seven frames?
Remember that all frames are initially empty, so your first unique pages
will cost one fault each.

 LRU replacement

 FIFO replacement

 Optimal replacement

a) 1

frame

a-1) LRU

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

=>20 page faults

a-2) FIFO

4
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

5
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

=>20 page faults

a-3) Optimal replacement

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

=>20page faults

b) 2

frames

b-1) LRU

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 3 3 2 2 5 5 2 2 2 7 7 3 3 1 3 3

_ 2 2 4 4 1 1 6 6 1 3 3 6 6 2 2 2 6

=>18 page

faults

b-2) FIFO

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 3 3 2 2 5 5 2 2 2 7 7 3 3 1 1 6

_ 2 2 4 4 1 1 6 6 1 3 3 6 6 2 2 3 3

=>18 page faults

6
b-3) Optimal replacement

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 3 4 1 5 6 1 3 3 3 3 1 3 3

_ 2 2 2 2 2 2 2 2 7 6 2 2 2 6

=>15 page faults

c) 3

frames

c-1) LRU

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 4 4 4 6 6 6 3 3 3 3 3 3

_ 2 2 2 1 1 1 2 2 2 2 6 6 1 6

_ _ 3 3 3 5 5 5 1 1 7 7 2 2 2

=>15 page

faults

c-2) FIFO

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 4 4 4 6 6 6 3 3 3 2 2 2 6

_ 2 2 2 1 1 1 2 2 2 7 7 7 1 1 1

_ _ 3 3 3 5 5 5 1 1 1 6 6 6 3 3

7
=>16 page

faults

c-3) Optimal

8
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 3 3 3 3 6

_ 2 2 2 2 2 2 7 2 2 2

_ _ 3 4 5 6 6 6 6 1 1

=>11 page faults

d) 4

frames

d-1) LRU

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 1 1 6 6

_ 2 2 2 2 2 2 2 2 2

_ _ 3 3 5 5 3 3 3 3

_ _ _ 4 4 6 6 7 7 1

=>10 page

faults

d-2) FIFO

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 5 5 5 5 3 3 3 3 1 1

_ 2 2 2 2 6 6 6 6 7 7 7 7 3

_ _ 3 3 3 3 2 2 2 2 6 6 6 6

_ _ _ 4 4 4 4 1 1 1 1 2 2 2
9
 14 page

faults

d-3) Optimal

10
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 7 1

_ 2 2 2 2 2 2 2

_ _ 3 3 3 3 3 3

_ _ _ 4 5 6 6 6

 8 page faults

e) 5

frames e-1)

LRU

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 1 1

_ 2 2 2 2 2 2 2

_ _ 3 3 3 6 6 6

_ _ _ 4 4 4 3 3

_ _ _ _ 5 5 5 7

=>8 page faults

e-2) FIFO

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 6 6 6 6 6

_ 2 2 2 2 2 7 7 7 7

_ _ 3 3 3 3 3 2 2 2

11
_ _ _ 4 4 4 4 4 1 1

_ _ _ _ 5 5 5 5 5 3

12
=>10 page faults

e-3) Optimal

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 1

_ 2 2 2 2 2 2

_ _ 3 3 3 3 3

_ _ _ 4 4 6 6

_ _ _ _ 5 5 7

 7 page faults

f) 6

frames f-1)

LRU

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 1

_ 2 2 2 2 2 2

_ _ 3 3 3 3 3

_ _ _ 4 4 4 7

_ _ _ _ 5 5 5

_ _ _ _ _ 6 6

 7 page faults

f-2) FIFO

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6
13
1 1 1 1 1 1 7 7 7 7

_ 2 2 2 2 2 2 1 1 1

_ _ 3 3 3 3 3 3 2 2

_ _ _ 4 4 4 4 4 4 3

_ _ _ _ 5 5 5 5 5 5

_ _ _ _ _ 6 6 6 6 6

 10 page faults

f-3) Optimal

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 1

_ 2 2 2 2 2 2

_ _ 3 3 3 3 3

_ _ _ 4 4 4 4

_ _ _ _ 5 5 5

_ _ _ _ _ 6 6

 7 page faults

g) 7 frames

Vì có 7 frame mà dãy có 7 số khác nhau => cả 3 thuật đều có 7 page


faults

g-1) LRU

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 1

14
_ 2 2 2 2 2 2

_ _ 3 3 3 3 3

_ _ _ 4 4 4 4

_ _ _ _ 5 5 5

_ _ _ _ _ 6 6

_ _ _ _ _ _ 7

 7 page faults

g-2) FIFO

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 1

_ 2 2 2 2 2 2

_ _ 3 3 3 3 3

_ _ _ 4 4 4 4

_ _ _ _ 5 5 5

_ _ _ _ _ 6 6

_ _ _ _ _ _ 7

 7 page faults

g-3) Optimal

1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

1 1 1 1 1 1 1

_ 2 2 2 2 2 2

_ _ 3 3 3 3 3

_ _ _ 4 4 4 4
15
_ _ _ _ 5 5 5

_ _ _ _ _ 6 6

_ _ _ _ _ _ 7

Number of frames LRU FIFO Optimal

1 20 20 20

2 18 18 15

3 15 16 11

4 10 14 8

5 8 10 7

6 7 10 7

7 7 7 7

3. Bài 9.19: Assume that we have a demand-paged memory. The page


table is held in registers. It takes 8 milliseconds to service a page fault if
an empty frame is available or if the replaced page is not modified and
20 milliseconds if the replaced page is modified. Memory-access time is
100 nanoseconds. Assume that the page to be replaced is modified 70
percent of the time. What is the maximum acceptable page-fault rate for
an effective access time of no more than 200 nanoseconds?

P is maximum acceptable page-fault rate

(1 – P) x 100 ns + 0.3P x 8000 ns + 0.7P x 20000 ns = 200 ns

 P = 0.000006

16
4. Bài 9.21: Consider the following page reference string

7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 7, 1, 0, 5, 4, 6, 2, 3, 0 , 1.

Assuming demand paging with three frames, how many page faults
would occur for the following replacement algorithms?

• LRU replacement

• FIFO replacement

• Optimal replacement

1) LRU replacement

7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1

7 7 7 1 1 3 3 3 7 7 7 5 5 5 2 2 2 1

_ 2 2 2 2 2 4 4 4 1 1 1 4 4 4 3 3 3

_ _ 3 3 5 5 5 6 6 6 0 0 0 6 6 6 0 0

 18 page faults

2) FIFO replacement

7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1

7 7 7 1 1 1 6 6 6 0 0 0 6 6 6 0 0

_ 2 2 2 5 5 5 7 7 7 5 5 5 2 2 2 1

_ _ 3 3 3 4 4 4 1 1 1 4 4 4 3 3 3

 17 page faults

3) Optimal replacement

17
7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1

7 7 7 1 1 1 1 1 1 1 1 1 1

_ 2 2 2 5 5 5 5 5 4 6 2 3

_ _ 3 3 3 4 6 7 0 0 0 0 0

 13 page faults

5. Bài 9.22: The page table shown in Figure 9.32 is for a system with 16-bit
virtual and physical addresses and with 4,096-byte pages. The
reference bit is set to 1 when the page has been referenced.
Periodically, a thread zeroes out all values of the reference bit. A dash
for a page frame indicates the page is not in memory. The page-
replacement algorithm is localized LRU, and all numbers are provided in
decimal.

a. Convert the following virtual addresses (in hexadecimal) to the

equivalent physical addresses. You may provide answers in either


hexadecimal or decimal. Also set the reference bit for the appropriate entry

18
in the page table.

19
• 0xE12C

• 0x3A9D

• 0xA9D9

• 0x7001

• 0xACA1

b. Using the above addresses as a guide, provide an example of

a logical address (in hexadecimal) that results in a page fault.

c. From what set of page frames will the LRU page-replacement

algorithm choose in resolving a page fault?

a. Địa chỉ ảo có 12 bit, địa chỉ vật lý có 12 bit, kích cỡ 1 page 4096 byte

 Số bit offset = log2(4096) = 12 bit

 Số bit biểu diễn số page = 16 – 12 = 4 bit

 Số đầu tiên trong địa chỉ hệ hexadecimal chỉ số page

 0xE12C -> 0x312C (E -> page 14)

 0x3A9D -> 0xAA9D

 0xA9D9 -> 0x59D9

 0x7001 -> 0xF001

 0xACA1 -> 0x5CA1

b. Page 4, 8, 12, 13 chưa được load vào RAM, các địa chỉ ảo dạng 0x4***,
0x8***, 0xC***, 0xD*** sẽ gây ra page fault, VD: 0x412C

c. Các frame 9, 1, 14, 13, 8, 0, 4 có reference bit = 0

6. Bài 9.31: Consider a demand-paging system with a paging disk that


has an average access and transfer time of 20 milliseconds. Addresses

20
are

21
translated through a page table in main memory, with an access time of 1
microsecond per memory access. Thus, each memory reference through
the page table takes two accesses. To improve this time, we have added
an associative memory that reduces access time to one memory
reference if the page-table entry is in the associative memory. Assume
that 80 percent of the accesses are in the associative memory and that,
of those remaining, 10 percent (or 2 percent of the total) cause page
faults. What is the effective memory access time?

EAT = 0.8 x 1 microsecond + 0.18 x 2 x 1 microsecond + 0.02 x


(20000 microsecond + 2 microsecond) = 401.2 microsecond

7. Bài 9.35: In a 1,024-KB segment, memory is allocated using the


buddy system. Using Figure 9.26 as a guide, draw a tree illustrating
how the following memory requests are allocated:

• Request 6-KB

• Request 250 bytes

• Request 900 bytes

• Request 1,500 bytes

• Request 7-KB

Next, modify the tree for the following releases of memory. Perform

coalescing whenever possible:

• Release 250 bytes

• Release 900 bytes

• Release 1,500 bytes

22
Request 6KB cho 8KB segment.
Request 250B cho 256-byte segment.
Request 900B cho 1KB segment.
Request 1500B cho 2KB segment
Request 7KB cho 8KB segment.

Các cỡ segment trống: 256-bytes, 512 bytes, 4KB, 8KB, 32KB, 64KB,
128KB, 256KB, and 512KB

Sau khi giải phóng bộ nhớ, các segments được sử dụng gồm 8KB segment
chứa 7KB dữ liệu và một 8KB segment chứa 6KB dữ liệu.

Các cỡ segment trống: 16KB, 32KB, 64KB, 128KB, 256KB, and 512KB.

23
8. Bài 9.39
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>

#define MAX_LINE 1024

int array[MAX_LINE / 2];


int page_faults = 0;

//Initializes an int array with 9999


//Parameters - working ize frame
//Returns - Nothing
void initialize_check(int working_set_size)
{
page_faults = 0;
int j = 0;
for (j = 0; j < working_set_size; j++)
{
array[j] = 9999;
}
}

//Checks if data is in the array for FIFO,Optimal,LRU


//Parameters: data, working size frame
//Returns: 1 if data is present
int check_present(int check, int working_set_size)
{
int present = 0;
int k = 0;
while (k < working_set_size)
{
if (array[k] != check)
{
}

24
else
{
present = 1;
break;
}
k++;
}
return present;
}

//Checks if page is in the array for MFU


//Parameters: array, working size frame, data, counter
//Returns: true if found otherwise false
bool page_found(int pages[], int working_set_size, int page_search, int *counter)
{
int j = 0;
for (j = 0; j < working_set_size; j++)
{
if (pages[j] == page_search)
{
*counter = j;
return true;
}
}
*counter = -1;
return false;
}

//Checks if the page is blank for MFU


//Parameters: array, working size frame
int page_blank(int pages[], int working_set_size)
{
int j = 0;
for (j = 0; j < working_set_size; j++)
{
if (pages[j] == -1)
{

25
return 1;
}
}
return 0;
}

//Implements First In First Out page fault algorithm


//Paramters: array of page requests, working size frame, length of the array
//Returns: Page faults in FIFO
int FIFO_pagefault(int pages[], int working_set_size, int length)
{
//Initialize the array
initialize_check(working_set_size);
int i, j = 0;
for (i = 0; i < length; i++)
{
//Finds and executes only if there is a page fault
if (check_present(pages[i], working_set_size) == 0)
{
for (j = 0; j < working_set_size - 1; j++)
{
array[j] = array[j + 1];
}
array[j] = pages[i];
page_faults = page_faults + 1;
}
}
return page_faults;
}

//Implements Optimal page fault algorithm


//Parameters: array of page requests, working size frame, length of the array
//Returns: Page Faults in Optimal
int optimal_pagefault(int pages[], int working_set_size, int length)
{
int i, j, k = 0;
int close[MAX_LINE / 2];

26
//Initialize the array
initialize_check(working_set_size);
while (k < length)
{
//Finds and executes only if there is a page fault
if (check_present(pages[k], working_set_size) == 0)
{
for (i = 0; i < working_set_size; i++)
{
int find = 0;
int page = array[i];
j = k;
while (j < length)
{
if (page == pages[j])
{
find = 1;
close[i] = j;
break;
}
else
{
find = 0;
}
j++;
}
if (!find)
{
close[i] = 9999;
}
}
int maximum = -9999;
int repeated;
i = 0;
while (i < working_set_size)
{
if (maximum < close[i])

27
{
repeated = i;
maximum = close[i];
}
i++;
}
array[repeated] = pages[k];
page_faults = page_faults + 1;
}
k++;
}
return page_faults;
}

//Implements Least Recently Found page fault algorithm


//Parameters: array of page requests, working size frame, length of the array
//Returns: Page Faults in LRU
int LRU_pagefault(int pages[], int working_set_size, int length)
{
int i, j, k = 0;
int close[MAX_LINE / 2];
//Initialize the array
initialize_check(working_set_size);
while (k < length)
{
//Finds and executes only if there is a page fault
if (check_present(pages[k], working_set_size) == 0)
{
for (i = 0; i < working_set_size; i++)
{
int find = 0;
int page = array[i];
j = k - 1;
while (j >= 0)
{
if (page == pages[j])
{

28
find = 1;
close[i] = j;
break;
}
else
{
find = 0;
}
j--;
}
if (!find)
{
close[i] = -9999;
}
}
int least = 9999;
int repeated;
i = 0;
while (i < working_set_size)
{
if (close[i] < least)
{
repeated = i;
least = close[i];
}
i++;
}
array[repeated] = pages[k];
page_faults = page_faults + 1;
}
k++;
}
return page_faults;
}

//Implements Most Frequently Used page fault algorithm


//Parameters: char array of page requests, working size frame

29
//Returns: Page Faults in MFU
int MFU_pagefault(int working_set_size, char copy_forMFU[])
{
page_faults = 0;
int pages[working_set_size];
int array_copy[MAX_LINE];
int pages_copy[working_set_size];
int page_counter = 0;
int prev_counter = 0;
int final_counter = 0;
int counter = 0;

//Initialize the array


int i = 0;
for (i = 0; i < MAX_LINE; i++)
{
array_copy[i] = -1;
}
for (i = 0; i < working_set_size; i++)
{
pages_copy[i] = 0;
pages[i] = -1;
}

int length = 0;
char *token = strtok(copy_forMFU, " ");

//Convert char array to integer array


while (token != NULL)
{
array_copy[length] = atoi(token);
token = strtok(NULL, " ");
length++;
}

int check_pages = page_blank(pages, working_set_size);

30
//Fill up the working set
//Determine which page was used recently
while (check_pages == 1)
{
if (!page_found(pages, working_set_size, array_copy[page_counter], &counter))
{
pages[prev_counter] = array_copy[page_counter];
pages_copy[prev_counter]++;
page_faults++;
prev_counter++;
}
else if (page_found(pages, working_set_size, array_copy[page_counter], &counter))
{
pages_copy[counter]++;
}
page_counter++;
check_pages = page_blank(pages, working_set_size);
}

//Do nothing if we find the value in the working set


for (final_counter = page_counter; final_counter < length; final_counter++)
{
if (page_found(pages, working_set_size, array_copy[final_counter], &counter))
{
pages_copy[counter]++;
continue;
}

//Remove least recently used from the array set, if there is no page
int max_occur = pages_copy[0];
int k = 0;
counter = k;
for (k = 1; k < working_set_size; k++)
{
if (max_occur < pages_copy[k])
{
max_occur = pages_copy[k];

31
counter = k;
}
}
max_occur = counter;
pages[max_occur] = array_copy[final_counter];
pages_copy[max_occur] = 1;
page_faults++;
}
return page_faults;
}

int main(int argc, char *argv[])


{
int working_set_size = 0;
char *line = NULL;
size_t line_length = MAX_LINE;
char *filename;

FILE *file;

if (argc < 2)
{
printf("Error: You must provide a checkfile as an argument.\n");
printf("Example: ./fp checkfile.txt\n");
exit(EXIT_FAILURE);
}

filename = (char *)malloc(strlen(argv[1]) + 1);


line = (char *)malloc(MAX_LINE);

memset(filename, 0, strlen(argv[1] + 1));


strncpy(filename, argv[1], strlen(argv[1]));

printf("\nOpening file %s ...\n", filename);


file = fopen(filename, "r");

if (file)

32
{
int pages[MAX_LINE];
int length = 0;
char copy_forMFU[MAX_LINE];
char *line2 = (char *)malloc(MAX_LINE);

//Read in from the file


while (fgets(line, line_length, file))
{
char *token;
char *token2;

strcpy(line2, line);
token2 = strtok(line2, " ");
token2 = strtok(NULL, "\r\n");
strcpy(copy_forMFU, token2);

token = strtok(line, " ");


working_set_size = atoi(token);

printf("\nWorking set size: %d\n\n", working_set_size);

//Changes the character array into the integer array


while (token != NULL)
{
token = strtok(NULL, " ");
if (token != NULL)
{
pages[length] = atoi(token);
length++;
}
}
//Prints the coressponding page faults in correspnding algorithm
printf("Page faults of FIFO: %5d\n", FIFO_pagefault(pages, working_set_size,
length));
printf("Page faults of LRU: %6d\n", LRU_pagefault(pages, working_set_size,
length));
printf("Page faults of MFU: %6d\n", MFU_pagefault(working_set_size,
33
copy_forMFU));
printf("Page faults of Optimal: %2d\n", optimal_pagefault(pages, working_set_size,
length));
length = 0;
printf("\n");
}
free(line);
fclose(file);
}
else
{
perror("Couldnt open file :( ....\n");
}
return 0;
}

34

You might also like