-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstInFirstOutPageReplacementAlgo.c
109 lines (100 loc) · 2.14 KB
/
FirstInFirstOutPageReplacementAlgo.c
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
#include<stdio.h>
#include<stdlib.h>
int pageno = 0;
struct frames{
int data;
struct frames *next;
};
struct frames *createNode(int d)
{
struct frames *new = (struct frames *)malloc(sizeof(struct frames));
new->data = d;
new->next = NULL;
return new;
}
void Enqueue(struct frames* *head,int data){
struct frames *ptr = *head;
if(*head==NULL){
*head = createNode(data);
return;
}
while(ptr->next!=NULL){
ptr = ptr->next;
}
ptr->next = createNode(data);
}
struct frames *Dequeue(struct frames* head){
if(head!=NULL){
return head = head->next;
}
else
return head;
}
void printFrames(struct frames *head,int pages[]){
struct frames *ptr = head;
printf("%3d ",pages[pageno]);
while (ptr != NULL)
{
printf(" %d", ptr->data);
ptr = ptr->next;
}
pageno++;
printf("\n");
}
int pageInFrame(struct frames *head,int page){
struct frames *ptr = head;
if (head==NULL)
return 0;
while (ptr != NULL)
{
if (ptr->data==page)
return 1;
ptr = ptr->next;
}
return 0;
}
void main()
{
int n, f, hit = 0, pageFault = 0;
// printf("Enter the number of frames: ");
// scanf("%d", &f);
// printf("Enter the number of pages: ");
// scanf("%d", &n);
// int frames[f], pages[n];
// printf("Enter the page reference string: ");
// for(i = 0; i < n; i++){
// scanf("%d", &pages[i]);
// }
//sample values
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
n = 20;
f = 3;
struct frames *head = NULL;
printf("page frame\n");
for (int i = 0; i < n; i++){
if (pageInFrame(head,pages[i])!=0){
hit++;
printf("%3d <", pages[pageno]);
for (int j = 0; j < f;j++)
printf("--");
pageno++;
printf("\n");
continue;
}
if (i<f){
Enqueue(&head, pages[i]);
printFrames(head, pages);
pageFault++;
}
else{
head=Dequeue(head);
Enqueue(&head, pages[i]);
printFrames(head, pages);
pageFault++;
}
}
printf("Page Fault: %d\n", pageFault);
printf("Page Hit: %d\n", hit);
float hitRatio =(float) hit/(pageFault+hit);
printf("HitRatio: %.3f\n", hitRatio);
}