-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpract0202.c
More file actions
74 lines (74 loc) · 1.85 KB
/
Copy pathpract0202.c
File metadata and controls
74 lines (74 loc) · 1.85 KB
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
#include <stdio.h>
struct node
{
int pno, reftime;
} frames[20];
int n;
int page_found(int pno)
{
int fno;
for (fno = 0; fno < n; fno++)
if (frames[fno].pno == pno)
return fno;
return (-1);
}
int get_free_frame()
{
int fno;
for (fno = 0; fno <= n; fno++)
if (frames[fno].pno == -1)
return (fno);
return (-1);
}
int get_lru_frame()
{
int lrufno = 0, fno;
for (fno = 1; fno < n; fno++)
if (frames[fno].reftime < frames[lrufno].reftime)
lrufno = fno;
return (lrufno);
}
int main()
{
int p_request[] = {5, 8, 10, 14, 10, 9, 5, 1, 10, 9, 12, 10};
int currtime = 0;
int page_falts = 0, i, j, fno;
printf("\n How many frames:");
scanf("%d", &n);
// initialize frames
for (i = 0; i < n; i++)
{
frames[i].pno = -1;
frames[i].reftime = -1;
}
printf("\n pageNo pageFrames pageFault");
printf("\n---------------------------------\n");
// currtime=0;//initialize time
for (i = 0; i < 15; i++)
{
j = page_found(p_request[i]);
if (j == -1) // page fault occurs
{
j = get_free_frame();
if (j == -1) // no free frame - do replacement
j = get_lru_frame();
page_falts++;
frames[j].pno = p_request[i];
printf("%4d:", p_request[i]);
for (fno = 0; fno < n; fno++)
printf("%4d", frames[fno].pno);
printf(":YES\n\n");
}
else
{
printf("%4d: ", p_request[i]);
for (fno = 0; fno < n; fno++)
printf("%4d", frames[fno].pno);
printf(" :NO\n\n");
}
frames[j].reftime = currtime;
currtime++;
}
printf("\n-------------------------------------");
printf("\n Number of page_Falts=%d", page_falts);
}