-
Notifications
You must be signed in to change notification settings - Fork 1
/
9lab.c
145 lines (141 loc) · 3.56 KB
/
9lab.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
#include <stdio.h>
#include <stdlib.h>
void FIFO(char[], char[], int, int);
void lru(char[], char[], int, int);
void opt(char[], char[], int, int);
int main()
{
int ch, YN = 1, i, length_of_string, frame_length;
char FRAMES[10], s[25];
printf("\n\n\tEnter the no ofempty frames: ");
scanf("%d", &frame_length);
printf("\n\n\tEnter the length of the string: ");
scanf("%d", &length_of_string);
printf("\n\n\tEnter the string: ");
scanf("%s", s);
for (i = 0; i < frame_length; i++)
{
FRAMES[i] = "#";
}
for (;;)
{
printf("\n\n\t*********** MENU ***********");
printf("\n\n\t1:FIFO\n\n\t2:LRU\n\n\t4:EXIT");
printf("\n\n\tEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
FIFO(s, FRAMES, length_of_string, frame_length);
break;
case 2:
lru(s, FRAMES, length_of_string, frame_length);
break;
case 4:
exit(0);
}
printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO PRESS 0 :");
scanf("%d", &YN);
}
return (0);
}
// FIFO
// i is main var, j in second var,k is counter var
void FIFO(char input[], char FRAMES[], int length_of_string, int frame_length)
{
int i, k = 0, j, flag = 0, cnt = 1;
printf("\n\tPAGE\t FRAMES \t FAULTS");
for (i = 0; i < length_of_string; i++)
{
for (j = 0; j < frame_length; j++)
{
// check if the page is in the frames
if (FRAMES[j] == input[i])
{
flag = 1;
break;
}
}
printf("\n\t%c\t", input[i]);
if (flag == 0)
{
// add the page to the frames
FRAMES[k] = input[i];
k++;
for (j = 0; j < frame_length; j++)
{
printf(" %c", FRAMES[j]);
}
printf("\tPage-fault %d", cnt);
cnt++;
}
else
{
flag = 0;
for (j = 0; j < frame_length; j++)
{
printf(" %c", FRAMES[j]);
}
printf("\tNo page-fault");
}
// in case the frame is full, the first element is removed and the new element is added to the end of the frame,by reseting k to 0
if (k == frame_length)
k = 0;
}
}
// LRU #unchanged
void lru(char s[], char F[], int l, int f)
{
int i, j = 0, k, m, flag = 0, cnt = 0, top = 0;
printf("\n\tPAGE\t FRAMES\t FAULTS");
for (i = 0; i < l; i++)
{
for (k = 0; k < f; k++)
{
if (F[k] == s[i])
{
flag = 1;
break;
}
}
printf("\n\t%c\t", s[i]);
if (j != f && flag != 1)
{
F[top] = s[i];
j++;
if (j != f)
top++;
}
else
{
if (flag != 1)
{
for (k = 0; k < top; k++)
{
F[k] = F[k + 1];
}
F[top] = s[i];
}
if (flag == 1)
{
for (m = k; m < top; m++)
{
F[m] = F[m + 1];
}
F[top] = s[i];
}
}
for (k = 0; k < f; k++)
{
printf(" %c", F[k]);
}
if (flag == 0)
{
printf("\tPage-fault%d", cnt);
cnt++;
}
else
printf("\tNo page fault");
flag = 0;
}
}