Professional Documents
Culture Documents
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).
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
0FF ->EFF (vì frame D đã dùng cho page trên, còn frame E, F trống)
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
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
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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 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
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
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.
18
in the page table.
19
• 0xE12C
• 0x3A9D
• 0xA9D9
• 0x7001
• 0xACA1
a. Địa chỉ ảo có 12 bit, địa chỉ vật lý có 12 bit, kích cỡ 1 page 4096 byte
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
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?
• Request 6-KB
• Request 7-KB
Next, modify the tree for the following releases of memory. Perform
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>
24
else
{
present = 1;
break;
}
k++;
}
return present;
}
25
return 1;
}
}
return 0;
}
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;
}
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;
}
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;
int length = 0;
char *token = strtok(copy_forMFU, " ");
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);
}
//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;
}
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);
}
if (file)
32
{
int pages[MAX_LINE];
int length = 0;
char copy_forMFU[MAX_LINE];
char *line2 = (char *)malloc(MAX_LINE);
strcpy(line2, line);
token2 = strtok(line2, " ");
token2 = strtok(NULL, "\r\n");
strcpy(copy_forMFU, token2);
34