0% found this document useful (0 votes)
7 views7 pages

Exp 5a

The document outlines various page replacement algorithms including FIFO, LRU, and Optimal, along with disk scheduling algorithms like FCFS, SSTF, SCAN, and C-SCAN. Each algorithm is implemented in C, detailing the initialization of frames, page fault handling, and head movement calculations. The code snippets demonstrate how to manage page faults and head movements for disk requests effectively.

Uploaded by

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

Exp 5a

The document outlines various page replacement algorithms including FIFO, LRU, and Optimal, along with disk scheduling algorithms like FCFS, SSTF, SCAN, and C-SCAN. Each algorithm is implemented in C, detailing the initialization of frames, page fault handling, and head movement calculations. The code snippets demonstrate how to manage page faults and head movements for disk requests effectively.

Uploaded by

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

exp 5a // Replace the page in FIFO manner

frames[index] = page;
#include <stdio.h> index = (index + 1) % numFrames; //
Circular increment for the index
#define MAX_FRAMES 10
#define MAX_PAGES 20

// Function to implement FIFO page // Print the current state of frames


replacement printf("Page fault for page %d ->
void fifoPageReplacement(int pages[], int Frames: ", page);
numPages, int numFrames) { for (int j = 0; j < numFrames; j++) {
int frames[numFrames]; // Array to hold if (frames[j] == -1) {
the frames printf("_ ");
int pageFaults = 0; // Counter for page } else {
faults printf("%d ", frames[j]);
int index = 0; // Index to track }
which frame to replace }
printf("\n");
// Initialize frames with -1 (indicating }
empty frames) }
for (int i = 0; i < numFrames; i++) {
frames[i] = -1; printf("Total page faults: %d\n", pageFaults);
} }

printf("Pages: "); int main() {


for (int i = 0; i < numPages; i++) { int pages[MAX_PAGES], numPages,
printf("%d ", pages[i]); numFrames;
}
printf("\n"); // Input the number of pages
printf("Enter the number of pages: ");
// Process the pages one by one scanf("%d", &numPages);// Input the
for (int i = 0; i < numPages; i++) { page references
int page = pages[i]; printf("Enter the pages: ");
int found = 0; for (int i = 0; i < numPages; i++) {
scanf("%d", &pages[i]);
// Check if the page is already in the frames }
for (int j = 0; j < numFrames; j++) {
if (frames[j] == page) { // Input the number of frames
found = 1; printf("Enter the number of frames: ");
break; scanf("%d", &numFrames);
}
} // Call FIFO page replacement function
fifoPageReplacement(pages, numPages,
// If the page is not found in the frames, it's numFrames);
a page fault
if (!found) {
pageFaults++;
return 0;
}
5b // If the page is not found in the
frames, it's a page fault
#include <stdio.h> if (!found) {
pageFaults++;
#define MAX_FRAMES 10
#define MAX_PAGES 20 // Find the least recently used page (the one
with the smallest last used time)
// Function to implement LRU page int lruIndex = 0;
replacement for (int j = 1; j < numFrames; j++) {
void lruPageReplacement(int pages[], int if (lastUsed[j] < lastUsed[lruIndex])
numPages, int numFrames) { {
int frames[numFrames]; // Array to hold lruIndex = j;
the frames }
int lastUsed[numFrames]; // Array to track }
the last used time for pages
int pageFaults = 0; // Counter for page // Replace the LRU page
faults frames[lruIndex] = page;
lastUsed[lruIndex] = i; // Update the
// Initialize frames with -1 (indicating empty last used time for the replaced page
frames)
for (int i = 0; i < numFrames; i++) { // Print the current state of frames
frames[i] = -1; printf("Page fault for page %d ->
lastUsed[i] = -1; // Initialize last used Frames: ", page);
time for (int j = 0; j < numFrames; j++) {
} if (frames[j] == -1) {
printf("_ ");
printf("Pages: "); } else {
for (int i = 0; i < numPages; i++) { printf("%d ", frames[j]);
printf("%d ", pages[i]); }
} }
printf("\n"); printf("\n");
}
// Process the pages one by one } printf("Total page faults: %d\n",
for (int i = 0; i < numPages; i++) { pageFaults);
int page = pages[i]; }
int found = 0;
int main() {
// Check if the page is already in the frames int pages[MAX_PAGES], numPages,
for (int j = 0; j < numFrames; j++) { numFrames;
if (frames[j] == page) {
found = 1; // Input the number of pages
lastUsed[j] = i; // Update the last printf("Enter the number of pages: ");
used time scanf("%d", &numPages);
break;
}
}
// Input the page references
printf("Enter the pages: ");
for (int i = 0; i < numPages; i++) {
scanf("%d", &pages[i]); // Check if the page is already in the
} frames
for (int j = 0; j < numFrames; j++) {
// Input the number of frames if (frames[j] == page) {
printf("Enter the number of frames: "); found = 1;
scanf("%d", &numFrames); break;
}
// Call LRU page replacement function }
lruPageReplacement(pages, numPages,
numFrames); // If the page is not found in the frames, it's
a page fault
return 0; if (!found) {
} pageFaults++;

5c // If there is an empty frame, insert the


page in that frame
#include <stdio.h> int emptyFrame = -1;
#include <limits.h> for (int j = 0; j < numFrames; j++) {
if (frames[j] == -1) {
#define MAX_FRAMES 10 emptyFrame = j;
#define MAX_PAGES 20 break;
}
// Function to implement Optimal page }
replacement
void optimalPageReplacement(int pages[], // If there is no empty frame, replace the
int numPages, int numFrames) { page with the one that will not be used for
int frames[numFrames]; // Array to hold the longest time
the frames if (emptyFrame == -1) {
int pageFaults = 0; // Counter for page int farthest = -1;
faults int replaceIndex = -1;

// Initialize frames with -1 (indicating // Find the page that will not be
empty frames) used for the longest time in the future
for (int i = 0; i < numFrames; i++) { for (int j = 0; j < numFrames; j++) {
frames[i] = -1; int k;
} for (k = i + 1; k < numPages;
k++) {
printf("Pages: "); if (frames[j] == pages[k]) {
for (int i = 0; i < numPages; i++) { break;
printf("%d ", pages[i]); }
} }
printf("\n"); // If the page is not found in the
future, replace it
// Process the pages one by one if (k == numPages) {
for (int i = 0; i < numPages; i++) { replaceIndex = j;
int page = pages[i]; break;
int found = 0; }
// Find the page that is used
farthest in the future
if (k > farthest) {
farthest = k;
replaceIndex = j; // Call Optimal page replacement
} function
} optimalPageReplacement(pages,
numPages, numFrames);
// Replace the farthest page
frames[replaceIndex] = page; return 0;
} else { }
// If there is an empty frame, insert
the page in that frame
frames[emptyFrame] = page;
}

// Print the current state of frames 6a


printf("Page fault for page %d ->
Frames: ", page); #include <stdio.h>
for (int j = 0; j < numFrames; j++) { #include <stdlib.h>
if (frames[j] == -1) {
printf("_ "); void FCFS(int requests[], int n, int
} else { initial_head_position) {
printf("%d ", frames[j]); int total_head_movement = 0;
} int current_position =
} initial_head_position;
printf("\n");
} printf("Initial head position: %d\n",
} current_position);
for (int i = 0; i < n; i++) {
printf("Total page faults: %d\n", int movement = abs(requests[i] -
pageFaults); current_position);
} total_head_movement += movement;
current_position = requests[i];
int main() { printf("Moved from %d to %d, head
int pages[MAX_PAGES], numPages, movement: %d\n", current_position -
numFrames; movement, current_position, movement);
}
// Input the number of pages
printf("Enter the number of pages: "); printf("Total head movement: %d\n",
scanf("%d", &numPages); total_head_movement);
}
// Input the page references
printf("Enter the pages: "); int main() {
for (int i = 0; i < numPages; i++) { int requests[] = {98, 183, 41, 122, 14, 124,
scanf("%d", &pages[i]); 65, 67};
} int n = sizeof(requests) /
sizeof(requests[0]);
// Input the number of frames int initial_head_position = 53;
printf("Enter the number of frames: "); FCFS(requests, n, initial_head_position);
scanf("%d", &numFrames); return 0;
}
printf("Total head movement: %d\n",
total_head_movement);
6b }

#include <stdio.h> int main() {


#include <stdlib.h> int requests[] = {98, 183, 41, 122, 14, 124,
65, 67};
void SSTF(int requests[], int n, int int n = sizeof(requests) /
initial_head_position) { sizeof(requests[0]);
int total_head_movement = 0; int initial_head_position = 53;
int current_position = SSTF(requests, n, initial_head_position);
initial_head_position; return 0;
int visited[n]; }
int i;
6c

#include <stdio.h>
for (i = 0; i < n; i++) { #include <stdlib.h>
visited[i] = 0;
} void SCAN(int requests[], int n, int
initial_head_position, int disk_size, char
printf("Initial head position: %d\n", direction) {
current_position); int total_head_movement = 0;
for (i = 0; i < n; i++) { int current_position =
int min_distance = 1000000, initial_head_position;
closest_request = -1;
for (int j = 0; j < n; j++) { // Sorting the requests
if (!visited[j]) { for (int i = 0; i < n - 1; i++) {
int distance = abs(requests[j] - for (int j = i + 1; j < n; j++) {
current_position); if (requests[i] > requests[j]) {
if (distance < min_distance) { int temp = requests[i];
min_distance = distance; requests[i] = requests[j];
closest_request = j; requests[j] = temp;
} }
} }
} }

visited[closest_request] = 1; printf("Initial head position: %d, Moving


int movement = %s\n", current_position, direction == 'L' ?
abs(requests[closest_request] - "Left" : "Right");
current_position);
total_head_movement += movement; if (direction == 'L') {
current_position = for (int i = n - 1; i >= 0; i--) {
requests[closest_request]; if (requests[i] <= current_position) {
printf("Moved to %d, head movement: int movement = abs(requests[i] -
%d\n", current_position, movement); current_position);
} total_head_movement +=
movement;
current_position = requests[i];
printf("Moved to %d, head }
movement: %d\n", current_position, }
movement); }
}
} printf("Total head movement: %d\n",
total_head_movement += total_head_movement);
current_position; // Move back to 0 }
current_position = 0;
int main() {
for (int i = 0; i < n; i++) { int requests[] = {98, 183, 41, 122, 14, 124,
if (requests[i] > current_position) { 65, 67};
int movement = abs(requests[i] - int n = sizeof(requests) /
current_position); sizeof(requests[0]);
total_head_movement += int initial_head_position = 53;
movement; int disk_size = 200;
current_position = requests[i]; char direction = 'R'; // 'L' for left, 'R' for
printf("Moved to %d, head right
movement: %d\n", current_position, SCAN(requests, n, initial_head_position,
movement); disk_size, direction);
} return 0;
} }
} else {
for (int i = 0; i < n; i++) { 6d
if (requests[i] >= current_position) {
int movement = abs(requests[i] - #include <stdio.h>
current_position); #include <stdlib.h>
total_head_movement +=
movement; void C_SCAN(int requests[], int n, int
current_position = requests[i]; initial_head_position, int disk_size) {
printf("Moved to %d, head int total_head_movement = 0;
movement: %d\n", current_position, int current_position =
movement); initial_head_position;
}
} // Sorting the requests
total_head_movement += (disk_size - 1 for (int i = 0; i < n - 1; i++) {
- current_position); // Move to the farthest for (int j = i + 1; j < n; j++) {
right if (requests[i] > requests[j]) {
current_position = disk_size - 1; int temp = requests[i];
requests[i] = requests[j];
for (int i = n - 1; i >= 0; i--) { requests[j] = temp;
if (requests[i] < current_position) { }
int movement = abs(requests[i] - }
current_position); }
total_head_movement +=
movement; printf("Initial head position: %d\n",
current_position = requests[i]; current_position);
printf("Moved to %d, head
movement: %d\n", current_position, // Move right first
movement); for (int i = 0; i < n; i++) {
if (requests[i] >= current_position) { C_SCAN(requests, n, initial_head_position,
int movement = abs(requests[i] - disk_size);
current_position); return 0;
total_head_movement += }
movement;
current_position = requests[i];
printf("Moved to %d, head
movement: %d\n", current_position,
movement);
}
}

// After reaching the rightmost end, jump


to the beginning and move left
total_head_movement += (disk_size - 1 -
current_position); // Move to the farthest
right
current_position = 0;

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


if (requests[i] < current_position) {
int movement = abs(requests[i] -
current_position);
total_head_movement +=
movement;
current_position = requests[i];
printf("Moved to %d, head
movement: %d\n", current_position,
movement);
}
}

printf("Total head movement: %d\n",


total_head_movement);
}

int main() {
int requests[] = {98, 183, 41, 122, 14, 124,
65, 67};
int n = sizeof(requests) /
sizeof(requests[0]);
int initial_head_position = 53;
int disk_size = 200;

You might also like