0% found this document useful (0 votes)
27 views5 pages

Parth 9

The document simulates disk scheduling algorithms like FCFS and page replacement algorithms like FIFO and optimal page replacement. It includes code snippets to implement these algorithms and output the number of page faults or disk seeks. The programs take a page/disk reference string as input and simulate the algorithms to count hits and faults.

Uploaded by

papatel4045
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)
27 views5 pages

Parth 9

The document simulates disk scheduling algorithms like FCFS and page replacement algorithms like FIFO and optimal page replacement. It includes code snippets to implement these algorithms and output the number of page faults or disk seeks. The programs take a page/disk reference string as input and simulate the algorithms to count hits and faults.

Uploaded by

papatel4045
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/ 5

[ 2CEIT401 OPERATING SYSTEM]

Practical: 9

AIM-Simulate various Disk Scheduling algorithms and


different types of Page Replacementalgorithms.

Department of Computer Engineering /Information Technology


2CEIT401:Operating Systems Practical-9

Aim: Simulate various Disk Scheduling algorithms and different types of Page
Replacementalgorithms.

1. Write a program to implement FIFO page Replacement algorithm.


#include <stdio.h>
#include <stdbool.h>
#define MAX_CAPACITY 100
int pageFaults(int pages[], int n, int capacity) {
bool inMemory[MAX_CAPACITY] = {false};
int pageFaultsCount = 0;
int pageHitsCount = 0;
int oldestPageIndex = 0;
int memory[capacity];
for (int i = 0; i < capacity; i++) {
memory[i] = -1;
}
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++;
break;
}
}
if (!pageFound) {
inMemory[memory[oldestPageIndex]] = false;
memory[oldestPageIndex] = currentPage;
inMemory[currentPage] = true;
pageFaultsCount++;
oldestPageIndex = (oldestPageIndex + 1) % capacity;
}
}
printf("Number of page hits using FIFO: %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 FIFO: %d\n", pageFaultsCount);
return 0;

Enrollment No: 22012021038


Name:Parth patel
2CEIT401:Operating Systems Practical-9

2.Write a program to implement Optimal Page replacement algorithm.

#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;
}

3. Write a program to implement FCFS Disk Scheduling algorithm.


#include <stdio.h>
#include <math.h>
int size = 8;
void FCFS(int arr[],int head)
{
int seek_count = 0;
int cur_track, distance;
for(int i=0;i<size;i++)
{
cur_track = arr[i];
distance = fabs(head - cur_track);
seek_count += distance;
head = cur_track;
}
printf("Total number of seek operations: %d\n",seek_count);
printf("Seek Sequence is\n");

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


printf("%d\n",arr[i]);
Enrollment No: 22012021038
Name:Parth patel
2CEIT401:Operating Systems Practical-9

}
}
int main()
{
int arr[8] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int head = 50;
FCFS(arr,head);
return 0;
}

Enrollment No: 22012021038


Name:Parth patel

You might also like