Os Lab 13 M
Os Lab 13 M
Karachi Campus
03 Execute both programs for the same set of reference strings. What
difference did you observe? Comment.
Submitted On:
Date: __ __
1
[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]
Segmentation code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_REGIONS 10
typedef struct {
int start;
int size;
} Region;
int main() {
int memoryCapacity, numRegions;
Region regions[MAX_REGIONS];
translateLogicalAddress(regions, numRegions);
return 0;
}
OUTPUT:
Paging code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_FRAMES 10
#define MAX_PAGE_REFS 20
int found = 0;
if (!found) {
int replace_index = 0;
for (int j = 0; j < capacity; j++) {
if (frames[j] == -1) {
replace_index = j;
break;
}
}
if (replace_index == capacity) {
frames[replace_index] = pageRef[i];
pageFaults++;
}
int main() {
int pageRef[MAX_PAGE_REFS];
int n, capacity;
return 0;
}
OUTPUT:
TASK NO. 02: Implement the FIFO and LRU policies described above in C language.
SOLUTION:
LRU:
#include <stdio.h>
#include <stdlib.h>
#define MAX_FRAMES 10
#define MAX_REFERENCES 20
minimum = time[i];
pos = i;
return pos;
frames[i] = -1;
time[i] = 0;
flag1 = flag2 = 0;
if (frames[j] == pages[i]) {
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
if (flag1 == 0) {
if (frames[j] == -1) {
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
if (flag2 == 0) {
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
printf("\n");
printf("%d\t", frames[j]);
int main() {
int n, frameCount, i;
scanf("%d", &n);
scanf("%d", &pages[i]);
scanf("%d", &frameCount);
return 0;
OUTPUT: LRU:
FIFO:
#include <stdio.h>
#include <stdlib.h>
#define MAX_FRAMES 10
#define MAX_REFERENCES 20
int nextFrame = 0;
frames[i] = -1;
flag = 0;
if (frames[j] == pages[i]) {
flag = 1;
break;
if (flag == 0) {
frames[nextFrame] = pages[i];
faults++;
printf("\n");
printf("%d\t", frames[k]);
Output:
TASK NO. 03: Execute both programs for the same set of reference strings. What
difference did you observe? Comment.
SOLUTION:
LRU Algorithm: The LRU algorithm generally results in fewer page faults because it takes into
account the recent usage of pages. It replaces the page that hasn't been used for the longest
period.
FCFS Algorithm: The FCFS algorithm may result in more page faults compared to LRU because it
replaces pages in the order they were loaded, regardless of how frequently or recently they
have been accessed..
Conclusion:
LRU generally results in fewer page faults compared to FIFO because it better adapts to the
actual usage patterns of the pages. FIFO may replace a page that will be needed soon, while
LRU retains pages that are used more frequently or recently, leading to improved performance
in typical scenarios.