-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_table.h
96 lines (76 loc) · 1.98 KB
/
frame_table.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef FRAME_TABLE
#define FRAME_TABLE
#define EXISTS '1'
#define EMPTY '0'
// Definitions
struct frame_table_entry
{
char available;
int LRU_count;
int page_number1;
int page_number2;
};
struct frame_table_entry *init_frame_table(int size);
void free_frame_table(struct frame_table_entry *frame_table);
int available_frame(struct frame_table_entry *frame_table, int size);
int LRU_frame(struct frame_table_entry *frame_table, int size);
void update_LRU(struct frame_table_entry *frame_table, int size, int index);
void print_LRU_counts(struct frame_table_entry *frame_table, int size);
// Implementation
struct frame_table_entry *init_frame_table(int size)
{
struct frame_table_entry *frame_table = malloc(sizeof(struct frame_table_entry) * size);
for (int i = 0; i < size; i++)
{
frame_table[i].available = EMPTY;
frame_table[i].LRU_count = 0;
}
return frame_table;
}
void free_frame_table(struct frame_table_entry *frame_table)
{
free(frame_table);
}
int available_frame(struct frame_table_entry *frame_table, int size)
{
for (int i = 0; i < size; i++)
{
if (frame_table[i].available == EMPTY)
{
return i;
}
}
return -1;
}
int LRU_frame(struct frame_table_entry *frame_table, int size)
{
int maxIndex = 0;
for (int i = 0; i < size; i++)
{
if ( frame_table[maxIndex].LRU_count < frame_table[i].LRU_count )
{
maxIndex = i;
}
}
return maxIndex;
}
void update_LRU(struct frame_table_entry *frame_table, int size, int index)
{
frame_table[index].LRU_count = 0;
for (int i = 0; i < size; i++)
{
if ( i != index && frame_table[i].available == EXISTS )
{
frame_table[i].LRU_count++;
}
}
}
void print_LRU_counts(struct frame_table_entry *frame_table, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d -> %d ", i, frame_table[i].LRU_count);
}
printf("\n");
}
#endif