Exp 5a
Exp 5a
frames[index] = page;
#include <stdio.h> index = (index + 1) % numFrames; //
Circular increment for the index
#define MAX_FRAMES 10
#define MAX_PAGES 20
// 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;
}
#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;
} }
} }
} }
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;