Parth 9
Parth 9
Practical: 9
Aim: Simulate various Disk Scheduling algorithms and different types of Page
Replacementalgorithms.
#include <stdio.h>
#include <stdbool.h>
#define MAX_CAPACITY 100
// Function to find the index of the page to be replaced
int findOptimalIndex(int pages[], int n, int memory[], int capacity, int currentIndex) {
int optimalIndex = -1;
int farthest = currentIndex;
for (int i = 0; i < capacity; i++) {
int j;
for (j = currentIndex; j < n; j++) {
if (memory[i] == pages[j]) {
if (j > farthest) {
farthest = j;
optimalIndex = i;
}
break;
}
}
if (j == n) {
return i; // If the page is not found in the future, return this index
}
}
return (optimalIndex == -1) ? 0 : optimalIndex; // If no optimal index found, replace
the first page
}
int pageFaults(int pages[], int n, int capacity) {
int memory[capacity];
int pageFaultsCount = 0;
int pageHitsCount = 0;
for (int i = 0; i < capacity; i++) {
memory[i] = -1; // Initialize memory slots to -1 indicating empty slots
}
for (int i = 0; i < n; i++) {
int currentPage = pages[i];
bool pageFound = false;
for (int j = 0; j < capacity; j++) {
if (memory[j] == currentPage) {
pageFound = true;
pageHitsCount++;
Enrollment No: 22012021038
Name:Parth patel
2CEIT401:Operating Systems Practical-9
break;
}
}
if (!pageFound) {
// Find the optimal index to replace
int indexToReplace = findOptimalIndex(pages, n, memory, capacity, i + 1);
memory[indexToReplace] = currentPage;
pageFaultsCount++;
}
}
printf("Number of page hits using Optimal: %d\n", pageHitsCount);
return pageFaultsCount;
}
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int numPages = sizeof(pages) / sizeof(pages[0]);
int capacity = 4;
int pageFaultsCount = pageFaults(pages, numPages, capacity);
printf("Number of page faults using Optimal: %d\n", pageFaultsCount);
return 0;
}
}
}
int main()
{
int arr[8] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int head = 50;
FCFS(arr,head);
return 0;
}