-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (156 loc) · 3.51 KB
/
main.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
)
type Data struct {
reference []int
frames []int
}
// FIFO Algorithm
func fifo(reference []int, frames int) int {
fmt.Println("** FIFO")
memory := make([]int, 0, frames)
pageFaults := 0
for _, page := range reference {
found := false
for _, memPage := range memory {
if memPage == page {
found = true
break
}
}
if !found {
if len(memory) < frames {
memory = append(memory, page)
} else {
memory = memory[1:]
memory = append(memory, page)
}
pageFaults++
fmt.Println("\t- ", memory)
}
}
return pageFaults
}
// OPT Algorithm
func opt(reference []int, frames int) int {
fmt.Println("** OPT")
memory := make([]int, 0, frames)
pageFaults := 0
for i, page := range reference {
found := false
for _, memPage := range memory {
if memPage == page {
found = true
break
}
}
if !found {
if len(memory) < frames {
memory = append(memory, page)
} else {
futureUse := make(map[int]int)
for j := i + 1; j < len(reference); j++ {
if _, found := futureUse[reference[j]]; !found && contains(memory, reference[j]) {
futureUse[reference[j]] = j
}
}
// Find the page to replace
if len(futureUse) < len(memory) {
for _, memPage := range memory {
if _, found := futureUse[memPage]; !found {
memory = remove(memory, memPage)
break
}
}
} else {
farthest := -1
pageToRemove := -1
for page, index := range futureUse {
if index > farthest {
farthest = index
pageToRemove = page
}
}
memory = remove(memory, pageToRemove)
}
memory = append(memory, page)
}
pageFaults++
}
}
return pageFaults
}
// LRU Algorithm
func lru(reference []int, frames int) int {
memory := make([]int, 0, frames)
pageFaults := 0
usedRecently := make([]int, 0, frames)
for _, page := range reference {
found := false
for _, memPage := range memory {
if memPage == page {
found = true
break
}
}
if !found {
if len(memory) < frames {
memory = append(memory, page)
} else {
lruPage := usedRecently[0]
memory = remove(memory, lruPage)
usedRecently = usedRecently[1:]
memory = append(memory, page)
}
pageFaults++
} else {
usedRecently = remove(usedRecently, page)
}
usedRecently = append(usedRecently, page)
}
return pageFaults
}
// Helper function to check if an element is in a slice
func contains(slice []int, element int) bool {
for _, v := range slice {
if v == element {
return true
}
}
return false
}
// Helper function to remove an element from a slice
func remove(slice []int, element int) []int {
for i, v := range slice {
if v == element {
return append(slice[:i], slice[i+1:]...)
}
}
return slice
}
func main() {
references := []Data{
{
reference: []int{7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1},
frames: []int{3, 4},
},
{
reference: []int{1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1},
frames: []int{3, 4},
},
{
reference: []int{3, 2, 1, 4, 5, 2, 1, 3, 6, 7, 2, 1, 3, 4, 5, 6, 7, 8, 1, 2},
frames: []int{3, 4},
},
}
for i, ints := range references {
fmt.Println("* Soy el ", i+1, "")
for _, frame := range ints.frames {
fmt.Println("- Con el frame ", frame)
fmt.Printf("\t FIFO Page Faults: %d\n", fifo(ints.reference, frame))
fmt.Printf("\t OPT Page Faults: %d\n", opt(ints.reference, frame))
fmt.Printf("\t LRU Page Faults: %d\n", lru(ints.reference, frame))
}
}
}