0% found this document useful (0 votes)
11 views

Os Lab 13 M

Uploaded by

haleemayasin294
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Os Lab 13 M

Uploaded by

haleemayasin294
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Bahria University

Karachi Campus

LAB EXPERIMENT NO.


13
LIST OF TASKS
TASK NO OBJECTIVE

01 Implement paging and segmentation code in C.

02 Implement the FIFO and LRU policies described above in C language.

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]

TASK NO. 01: Implement paging and segmentation code in C.


SOLUTION:

Segmentation code:

#include <stdio.h>
#include <stdlib.h>

#define MAX_REGIONS 10

typedef struct {
int start;
int size;
} Region;

void translateLogicalAddress(Region regions[], int numRegions) {


int regionIndex, offset;
printf("Enter region index and offset: ");
scanf("%d %d", &regionIndex, &offset);

if (regionIndex >= numRegions || offset >= regions[regionIndex].size) {


printf("Invalid input.\n");
return;
}

int physicalAddress = regions[regionIndex].start + offset;


printf("Physical Address: %d\n", physicalAddress);
}

int main() {
int memoryCapacity, numRegions;
Region regions[MAX_REGIONS];

printf("Enter memory capacity and number of regions: ");


scanf("%d %d", &memoryCapacity, &numRegions);

for (int i = 0; i < numRegions; i++) {


printf("Enter starting address and size for region %d: ", i);
scanf("%d %d", &regions[i].start, &regions[i].size);

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

if (regions[i].start >= memoryCapacity || regions[i].start + regions[i].size > memoryCapacity)


{
printf("Invalid memory capacity or address.\n");
exit(0);
}
}

translateLogicalAddress(regions, numRegions);

return 0;
}

OUTPUT:

Paging code:

#include <stdio.h>
#include <stdlib.h>

#define MAX_FRAMES 10
#define MAX_PAGE_REFS 20

void fifo(int pageRef[], int n, int capacity) {


int frames[capacity];
int pageFaults = 0;

for (int i = 0; i < capacity; i++) {


frames[i] = -1;
}

for (int i = 0; i < n; i++) {

int found = 0;

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

for (int j = 0; j < capacity; j++) {


if (frames[j] == pageRef[i]) {
found = 1;
break;
}
}

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) {

static int count = 0;


replace_index = count;
count = (count + 1) % capacity;
}

frames[replace_index] = pageRef[i];
pageFaults++;
}

printf("Frames after %dth reference: ", i + 1);


for (int j = 0; j < capacity; j++) {
printf("%d ", frames[j]);
}
printf("\n");
}

printf("Total Page Faults = %d\n", pageFaults);


}

int main() {
int pageRef[MAX_PAGE_REFS];
int n, capacity;

printf("Enter the number of page references: ");


scanf("%d", &n);

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

printf("Enter the page reference string: ");


for (int i = 0; i < n; i++) {
scanf("%d", &pageRef[i]);
}

printf("Enter the number of frames (memory capacity): ");


scanf("%d", &capacity);

if (capacity > MAX_FRAMES) {


fprintf(stderr, "Error: Maximum frame capacity is %d\n", MAX_FRAMES);
exit(1);
}

printf("Using FIFO Page Replacement Algorithm\n");


fifo(pageRef, n, capacity);

return 0;
}

OUTPUT:

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

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

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;

void lru(int pages[], int n, int frames[], int frameCount) {

int counter = 0, time[MAX_FRAMES];

int flag1, flag2, i, j, pos, faults = 0;

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

for (i = 0; i < frameCount; ++i) {

frames[i] = -1;

time[i] = 0;

for (i = 0; i < n; ++i) {

flag1 = flag2 = 0;

for (j = 0; j < frameCount; ++j) {

if (frames[j] == pages[i]) {

counter++;

time[j] = counter;

flag1 = flag2 = 1;

break;

if (flag1 == 0) {

for (j = 0; j < frameCount; ++j) {

if (frames[j] == -1) {

counter++;

faults++;

frames[j] = pages[i];

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

time[j] = counter;

flag2 = 1;

break;

if (flag2 == 0) {

pos = findLRU(time, frameCount);

counter++;

faults++;

frames[pos] = pages[i];

time[pos] = counter;

printf("\n");

for (j = 0; j < frameCount; ++j) {

printf("%d\t", frames[j]);

printf("\nTotal Page Faults (LRU): %d\n", faults);

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

int main() {

int pages[MAX_REFERENCES], frames[MAX_FRAMES];

int n, frameCount, i;

printf("Enter the number of page references: ");

scanf("%d", &n);

printf("Enter the page reference string: ");

for (i = 0; i < n; ++i) {

scanf("%d", &pages[i]);

printf("Enter the number of frames: ");

scanf("%d", &frameCount);

lru(pages, n, frames, frameCount);

return 0;

OUTPUT: LRU:

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

FIFO:

#include <stdio.h>

#include <stdlib.h>

#define MAX_FRAMES 10

#define MAX_REFERENCES 20

void fcfs(int pages[], int n, int frames[], int frameCount) {

int flag, i, j, k, faults = 0;

int nextFrame = 0;

for (i = 0; i < frameCount; ++i) {

frames[i] = -1;

for (i = 0; i < n; ++i) {

flag = 0;

for (j = 0; j < frameCount; ++j) {

if (frames[j] == pages[i]) {

flag = 1;

break;

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

if (flag == 0) {

frames[nextFrame] = pages[i];

nextFrame = (nextFrame + 1) % frameCount;

faults++;

printf("\n");

for (k = 0; k < frameCount; ++k) {

printf("%d\t", frames[k]);

printf("\nTotal Page Faults (FCFS): %d\n", faults);

Output:

MINHAJ UR RIYAN 02-131222-080


[LAB 13] [Implementing page replacement policies]
[OPERATING SYSTEM]

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.

MINHAJ UR RIYAN 02-131222-080

You might also like