-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog9.c
154 lines (136 loc) · 2.65 KB
/
prog9.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
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
#include<stdio.h>
#include<stdlib.h>
void FIFO()
{
char s[200];
char F[200];
int l,f,i,j=0,k,flag=0,cnt=0;
printf("\nEnter the number of frames : ");
scanf("%d",&f);
printf("\nEnter the length of the string: ");
scanf("%d",&l);
printf("\nEnter the string: ");
scanf("%s", s);
for(i=0;i<f;i++)
F[i]=' ';
printf("\n\tPAGE\t\tFRAMES\t\t\tFAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
if(F[k]==s[i])
flag=1;
if(flag==0)
{
printf("\n\t%c\t",s[i]);
F[j]=s[i];
j++;
for(k=0;k<f;k++)
printf("\t%c",F[k]);
printf("\tPage-fault%d",cnt);
cnt++;
}
else
{
flag=0;
printf("\n\t%c\t",s[i]);
for(k=0;k<f;k++)
printf("\t%c",F[k]);
printf("\tNo page-fault");
}
if(j==f)
j=0;
}
}
int findLRU(int time[], int n)
{
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i)
{
if(time[i] < minimum)
{
minimum = time[i];
pos = i;
}
}
return pos;
}
int lru()
{
int no_of_frames, no_of_pages, frames[10], counter = 0;
int time[10], flag1, flag2, i, j, pos, faults = 0, page;
char s[200];
printf("\nEnter number of frames: ");
scanf("%d", &no_of_frames);
printf("\nEnter number of pages: ");
scanf("%d", &no_of_pages);
printf("\nEnter reference string: ");
scanf("%s", s);
for(i = 0; i < no_of_frames; ++i)
frames[i] = -1;
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
page = s[i] - '0';
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == page)
{
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
counter++;
faults++;
frames[j] = page;
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = page;
time[pos] = counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
printf("%d\t", frames[j]);
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
int main()
{
int ch,YN=1,i,l,f;
char F[10],s[25];
do
{
printf("\nOptions : ");
printf("\n\n1:FIFO\n2:LRU \n3:EXIT");
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: FIFO();
break;
case 2: lru();
break;
default: exit(0);
}
printf("\n\nPress 1 to continue.. 0 to exit ");
scanf("%d",&YN);
}while(YN==1);
return(0);
}