-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
294 lines (242 loc) · 5.67 KB
/
main.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include<stdlib.h>
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
int pageArray[9] = {0}, pageOfBuffer[3] = {0}, fault[9]={0};
float pagePosition[9] = {-5.5,-5.5,-5.5,-5.5,-5.5,-5.5,-5.5,-5.5,-5.5};
int hit = 0, step = -1, destination = 0, startAnimation = 0;
char res[]="No. of Page Faults are : ";
//Colours
float backgroundColor[][3]={{1,0,0}, {0,1,0}, {0,0,1}}, tileColor[][3]={{1,1,0}, {1,0.7,0.7}, {0,1,1}};
int bcPointer = 0, tcPointer = 0;
void init()
{
glColor3f(0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,800,0,600);
glMatrixMode(GL_MODELVIEW);
}
void tile(float x, float y, char n)
{
float size = 20;
//Colour of frame buffer
// glColor3f(1,0,1);
glBegin(GL_POLYGON);
glVertex2f(x-size, y-size);
glVertex2f(x+size, y-size);
glVertex2f(x+size, y+size);
glVertex2f(x-size, y+size);
glEnd();
glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(x-size, y-size);
glVertex2f(x+size, y-size);
glVertex2f(x+size, y+size);
glVertex2f(x-size, y+size);
glEnd();
glRasterPos2f(x-size/2, y);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, n);
}
//for fault count
void draw_flag()
{
glColor3fv(backgroundColor[bcPointer]);
glBegin(GL_POLYGON);
glVertex2f(-10,-10);
glVertex2f(10,-10);
glVertex2f(10,10);
glVertex2f(-10,10);
glEnd();
}
void setInput()
{
glColor3fv(backgroundColor[bcPointer]);
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(700,0);
glVertex2f(700,100);
glVertex2f(0,100);
glEnd();
glPushMatrix();
glTranslatef(-10, 40, 0);
int i;
for(i=0; i<9; i++)
{
glColor3fv(tileColor[tcPointer]);
glTranslatef(70, 0, 0);
glPushMatrix();
if(pagePosition[i] > -4.5)
{
//Moving right
glTranslatef(70*step-70*i,0,0);
}
//Input positions
glTranslatef(0, -250-(45*pagePosition[i]), 0);
if((int)pagePosition[i]==destination && pagePosition[i]>=0)
glColor3f(1,0.3,0.3);
else
glColor3fv(tileColor[tcPointer]);
//glColor3f(0.1,0.5,0.1);
tile(10,10,pageArray[i]+'0');
glPopMatrix();
if(fault[i])
{
glPushMatrix();
glTranslatef(0, -385, 0);
draw_flag();
glPopMatrix();
}
}
glPopMatrix();
}
//Writing text on the screen
void drawText(char *string, float x,float y,float z)
{
char *c;
glRasterPos3f(x, y,z);
for (c=string; *c != '\0'; c++)
{
if(*c=='\n')
glRasterPos3f(x, y-0.05,z);
else
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *c);
}
}
void setFrame()
{
glPushMatrix();
tile(0,0,pageOfBuffer[0]==0?' ':pageOfBuffer[0]+'0');
glTranslatef(0, -45, 0);
tile(0,0,pageOfBuffer[1]==0?' ':pageOfBuffer[1]+'0');
glTranslatef(0, -45, 0);
tile(0,0,pageOfBuffer[2]==0?' ':pageOfBuffer[2]+'0');
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
//Frame Buffer position
glTranslatef(120+(70*step),195,0);
setFrame();
glPopMatrix();
glColor3f(1,0,0);
glPushMatrix();
//Input Red box position
glTranslatef(50, 400, 0);
setInput();
glPopMatrix();
glEnd();
if(step==8)
{
glColor3f(0,0,0);
res[24]=(9-hit)+'0';
drawText(res, 50, 20, 0);
}
drawText("FIFO Page Replacement Algorithm", 250, 550, 0);
glFlush();
glutSwapBuffers();
}
void idle()
{
if(!startAnimation)
return;
printf("Idle called\n");
printf("%d, %f\n",destination, pagePosition[step]);
if(destination>pagePosition[step])
pagePosition[step]+=0.10;
if(destination<pagePosition[step])//It has reached its location...So stop animation
{
if(fault[step])
pageOfBuffer[destination]=pageArray[step];
startAnimation = 0;
destination = -10;
}
display();
}
void mouse(int btn,int state,int x, int y)
{
printf("Mouse function called\n");
int n,i,j;
if(startAnimation==1){
printf("Animating");
return;
}
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
if(step<9)
step++;
else
printf("%d\n", hit);
for(i=0;i<3;i++)
{
if(pageArray[step]==pageOfBuffer[i])
{
hit++;
fault[step] = 0;
startAnimation = 1;
destination = -5;
glutPostRedisplay();
return;
}
}
destination = ((step-hit)%3);
printf("%d\n", destination);
startAnimation = 1;
fault[step] = 1;
glutPostRedisplay();
}
}
void setBGColor(int action)
{
bcPointer = action;
glutPostRedisplay();
}
void setTileColor(int action)
{
tcPointer = action;
glutPostRedisplay();
}
void menu(int action)
{
if(action==0)
exit(0);
}
void addMenu()
{
int submenu1, submenu2;
submenu1 = glutCreateMenu(setBGColor);
glutAddMenuEntry("Red",0);
glutAddMenuEntry("Green",1);
glutAddMenuEntry("Blue",2);
submenu2 = glutCreateMenu(setTileColor);
glutAddMenuEntry("Yellow",0);
glutAddMenuEntry("Light Red",1);
glutAddMenuEntry("Light Blue",2);
glutCreateMenu(menu);
glutAddSubMenu("Background Colour",submenu1);
glutAddSubMenu("Tile Colour",submenu2);
glutAddMenuEntry("Quit",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
int j;
printf("Enter the page array of size 9\n");
for(j=0; j<9; j++)
scanf("%d", &pageArray[j]);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutCreateWindow("FIFO PAGE REPLACEMENT");
init();
addMenu();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutIdleFunc(idle);
glClearColor(1,1,1,1);
glutMainLoop();
return 0;
}