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
| #include <stdio.h> #include <stdlib.h> #include<iostream> using namespace std; int n,j=0; struct page { int num; int visit=65535; struct page* next = NULL; }; typedef struct page Page; struct PageQueue { Page* head; Page* tail; }; void initQueue(struct PageQueue* rq) { rq->head = NULL; rq->tail = NULL; } void EnQueue(struct PageQueue* rq, Page* p) { if (!rq->head) { rq->head = p; rq->tail = p; } else { rq->tail->next = p; rq->tail = p; p->next = NULL; } } void Dequeue(struct PageQueue* rq, Page** p) { if (rq->head != NULL) { *p = rq->head; rq->head = rq->head->next; if (rq->head == NULL) { rq->tail = NULL; } (*p)->next = NULL; } } int Find(int n1, struct PageQueue rq) { int i = 0; Page* p = rq.head; while(p){ if (p->num == n1) return 1; p = p->next; } return 0; } int Getleast(struct PageQueue* rq, int a[]) { int maxv = 0, num=0; Page* q = rq->head; while (q) { int k = 0; while (a[k] != 0) { if (q->num == a[k]) { q->visit = k; break; } k++; }if (maxv < q->visit) { maxv = q->visit; num = q->num; } q = q->next; } return num; } int Getleastr(struct PageQueue* rq) { int num = 0,minv=65535; Page* q = rq->head; while (q) { int k = 0; if (minv > q->visit) { minv = q->visit; num = q->num; } q = q->next; } return num; } void Delete(struct PageQueue* rq, int num) { Page* p1 = rq->head; Page* p2; if (p1->num ==num) Dequeue(rq, &p2); if (p1->num != num) { while (p1->next->num != num) { p1 = p1->next; } if (p1->next == rq->tail)rq->tail = p1; if (p1->next)p1->next = p1->next->next; } } void Print(struct PageQueue rq) { Page* p = rq.head; while (p) { cout << p->num << '\t'; p = p->next; } } void FIFO(struct PageQueue* rq, int a[]) { int i = 0,count=0; while (a[i] != 0) { Page* p = new page; if (!Find(a[i], *rq)) { cout << "在" << a[i] << "处产生缺页中断"; if(count>=n)Dequeue(rq, &p); p->num = a[i]; p->next = NULL; EnQueue(rq, p); Print(*rq); cout << endl; count++; } i++; } cout << "缺页次数" << count << "缺页率" << count * 1.0 / j << endl; } void OPT(struct PageQueue* rq, int a[]){ int i = 0, count = 0,num1=0; while (a[i] != 0) { Page* p = new page; if (!Find(a[i], *rq)) { cout << "在" << a[i] << "处产生缺页中断"; if (count >= n) { num1 = Getleast(rq, a); Delete(rq, num1); } p->num = a[i]; p->next = NULL; EnQueue(rq, p); Print(*rq); cout << endl; count++; } i++; } cout << "缺页次数" << count << "缺页率" << count * 1.0 / j << endl; } void ChangeV(struct PageQueue* rq, int a, int i) { Page* q = rq->head; while (q->num != a) q = q->next; q->visit = i; } void LRU(struct PageQueue* rq, int a[]) { int i = 0, count = 0,num; while (a[i] != 0) { Page* p = new page; if (!Find(a[i], *rq)) { cout << "在" << a[i] << "处产生缺页中断"; if (count >= n) { num = Getleastr(rq); Delete(rq, num); } p->num = a[i]; p->next = NULL; p->visit = i; EnQueue(rq, p); Print(*rq); cout << endl; count++; } else { ChangeV(rq, a[i], i); } i++; } cout << "缺页次数" << count << "缺页率" << count * 1.0 / j << endl; } int main() { int a[30] = { 0 },t,i=0; cout << "主存块数" << endl; cin >> n; cout << "页面号" << endl; while (cin.get() != '\n'||j==0) { cin >> a[j]; j++; } PageQueue pg; initQueue(&pg); LRU(&pg, a); }
|